yay, a JAVA-problem

  • Hey - turns out IRC is out and something a little more modern has taken it's place... A little thing called Discord!

    Join our community @ https://discord.gg/JuaSzXBZrk for a pick-up game, or just to rekindle with fellow community members.

DaTeL

=]DoG[=Food
Oct 23, 2002
2,659
48
Holland
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 :P

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:
A NullPointerException means that you're trying to access a variable which hasn't been created. From what I can observe, there doesn't even seem to be a method called readLibrary() but readBibliotheek() in the Library-class so I'm puzzled how that code can even compile. Or then there is a definition for such a function and it only returns null which would explain why you get the NullPointerException.

Would have to know which line the NullPointerException happens to get more out of it..
 
ohw shit..... I tried to translate my dutch dode into english.....

as Library means Bibliotheek :P

anyway... I compiled/runned the dutch code....so that shouldn't be the problem
btw... I'll run+compile it as soon as my PC is back up :P

edit: and I'v echanged the 'bibliotheek'to library :P should be a proper translation now
 
ok..... now for the error:

Code:
> Executing: <blahblah> TestLibrary

java.lang.NullPointerException
        at Book.equals(Book.java:64)
        at Library.getPlace(Library.java, Compiled Code)
        at Library.addOne(Library.java:88)
        at Library.readLibrary(Library.java, Compiled Code)
        at TestLibrary.main(TestLibrary.java:4)
Exception in thread "main" 
> Execution finished.
 
Last edited:
There is no valid Book-object in data.

Code:
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;
}

The above method tests for equality of the new book and the one in the 'data' array, and the call moves to the Book-class where the equals()-method states:

Code:
public boolean equals(Object other){
      Book that = (Book)other;
      return (getBookId()==that.getBookId());
}

where you cast the 'other' argument into a Book-class and try to access it's method getBookId() which causes the NullPointerException because 'other' is not a valid object, but null.
 
If I'm understanding you right.... the following code wouldn't give me such errors:

Code:
private int getPlace(Book b) {
      int i = 0;
      while ((i<number)&&(!b.equals(data[i])))
         i++;
      if (i<data.length)
         return i;
      else
         return -1;
}

because now it'll only check the filled array-places
or...?
(coz it doesn't :)

if I change that... the following error remains (hey, it's shorter! :D)
Code:
> Executing: <blahblah>TestLibrary

java.lang.NullPointerException
        at Library.addOne(Library.java:90)
        at Library.readLibrary(Library.java, Compiled Code)
        at TestLibrary.main(TestLibrary.java:4)
Exception in thread "main" 
> Execution finished.
 
Last edited:
shit...... while translating from dutch to english.... some line-numbers got changed :D

I just compiled/ran the code in the english one..... results are updated in previous posts... sorry :(
edit: also I noticed the code was translated all-wrong.... forgot alot of vars etc :) they're right now... though the same error/exception applies :P
 
Last edited:
Code:
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++;
      }
}

In there, you only check if "place>=0" which doesn't do what it's supposed to. What you should be checking is "place<number", no?
 
thnx for the help.... all solved now
turned out I didn't need the getPlace() method after-all