when I run the following code I get a NullPointerException..... and have absolutely no idea why (or what it means)
btw, I use ConTEXT to write/compile.....complain to my teacher if u disagree
the textfile contains: (example from assignment)
================
the classes are written as follows:
btw, I use ConTEXT to write/compile.....complain to my teacher if u disagree
Code:
public class TestLibrary{
public static void main(String[] args){
SimpleInput file = new SimpleInput("info.txt");
Library datel = Library.readLibrary(file);
System.out.println(datel.toString());
}
}
the textfile contains: (example from assignment)
Code:
4
Bibliotheek1
Studying 101 3
Vacation 205 1
Cooking 452 4
================
the classes are written as follows:
Code:
public class Book{
//pre: id >= 0, t != "", a >= 0
//post: a Book-object has been created with bookId = id,
// titel = t and number = a
public Book(int id, String t, int a){
setBookId(id);
setTitel(t);
setNumber(a);
}
//post: returns boekId
public int getBookId(){
return bookId;
}
//post: returns titel
public String getTitel(){
return titel;
}
//post: returns number
public int getNumber(){
return number;
}
//pre: id >= 0
//post: has set bookId to id
public void setBookId(int id){
if (id>=0)
bookId = id;
else
System.out.println("Error: bookId variable");
}
//+setTitel(t : String)
//pre: t != ""
//post: set titel to t
public void setTitel(String t){
if (!t.equals(""))
titel = t;
else
System.out.println("Error: titel variable");
}
//pre: a >= 0
//post: set number to a
public void setNumber(int a){
if (a>=0)
number = a;
else
System.out.println("Error: aantal variable");
}
//post: returns String representation of Book
// example: <Book[452681,Bible,3]>
public String toString(){
return "<Book["+getBookId()+","+getTitel()+","+getNumber()+"]>";
}
//pre: other is not null
//post: returns true if (getBoekId() == that.getBoekId()),
public boolean equals(Object other){
Book that = (Book)other;
return (getBookId()==that.getBookId());
}
//pre: s contains a line with data of one book
//post: a Book-object with s' information is returned
public static Book readBook(SimpleInput s){
return new Book(s.nextInt(),s.nextWord(),s.nextInt());
}
// Declare attributes
private int bookId;
private String titel;
private int number;
}
Code:
public class Library{
//post: a Library-object has been created with name nm and capacity n
public Library(String nm, int n){
setName(nm);
data = new Book[n];
}
//post: return name
public String getName(){
return name;
}
//post: return number
private int getNumber(){
return number;
}
//-getBoek(pos : int) : Book
//pre: 0 <= pos < number
//post: if pre; return Book on position pos, else: null
private Book getBook(int pos){
return data[pos];
}
// get place in array of Book b
private int getPlace(Book b){
int i = 0;
while ((i<data.length)&&(!b.equals(data[i])))
i++;
if (i<data.length)
return i;
else
return -1;
}
//post: heeft naam gelijk gemaakt aan nm
private void setName(String nm){
name = nm;
}
//pre: a >= 0
//post: set number to a
private void setNumber(int a){
if (a>=0)
number = a;
else
number = -1;
}
//pre: 0 <= pos <= < n
//post: set (if pre) b on position pos
private void setBook(int pos, Book b){
if ( 0<=pos
&& pos <= number
&& number <= data.length )
data[pos] = b;
}
//post: return true if array contains b, else false
public boolean contains(Book b){
int i = 0;
while ((i<data.length)&&(!b.equals(data[i])))
i++;
return (i!=data.length);
}
//+getBookOnId(id : int) : Bonk
//post: return Book with bookId id if Library contains it
// else returns null
public Book getBookOnId(int id){
int i = 0;
while ((i<data.length)&&(id!=data[i].getBookId()))
i++;
if (i!=data.length)
return data[i];
else
return null;
}
/*
post: if Library doesn't contain Book b yet:
add Book in an empty array-space
else (if) Library already contains Book b:
raise book.number with b.number,
using book.getBookId() == b.getBookId()
*/
public void addOne(Book b){
int place = getPlace(b);
if (place>=0){
data[place].setNumber(b.getNumber()+data[place].getNumber());
}
else {
setBook(number,b);
number++;
}
}
/*
+loneOut(b : Book): boolean
if Library doesn't contain Book b,
or
book.number < b.number
return true
else lower book.number with b.number
and
return true
*/
public boolean loneOut(Book b){
int place = getPlace(b);
if (place<0
|| data[place].getNumber() < b.getNumber()){
return false;
}
else {
int newnumber = data[place].getNumber()-b.getNumber();
data[place].setNumber(newnumber);
return true;
}
}
//+toString() : String
//post: return a String representation of Library
public String toString(){
String res = "";
int i = 0;
while (i < number){
res += data[i].toString() + " ";
}
return res;
}
//+readLibrary(s : SimpleInput) : Library
//pre: a textfile is inserted
//post: return a Library-object containing s' information
public static Library readLibrary(SimpleInput s){
int bookNumber = ((s.nextInt())-1);
String libName = s.nextWord();
Library res = new Library(libName,bookNumber);
for (int i=0; i<bookNumber ;i++){
Book temp = new Book(s.nextInt(),s.nextWord(),s.nextInt());
res.addOne(temp);
}
return res;
}
// Attributezzz
private String name;
private Book[] data;
private int number;
}
Last edited: