-4

So the method is to see if a book exists in the library:

public boolean exists(Book l) {                      
    for (int i = 0; i < index; i++) {
        if (books[i].equals(l)) {
            return true;
        }
    }
    return false;
}

Output:

Exception in thread "main" java.lang.NullPointerException
at Library.exists(Library.java:13)

What am i doing wrong here?

So i got the answer thanks to you guys :) So the problem was that i did not add books inside the library! The method to add books:

public void addBook(Book l) {
    if (l == null) {
        System.out.println("Book is unini..!");
        return;
    }

    if (exists(l)) {
        System.out.println("Book exists!");
        return;
    }

    if (books.length == index) {
        Book[] temp = new Book[books.length + 3];
        for(int i=0;i<books.length;i++){
            temp[i]=books[i];
        }
        books=temp;
    }
    books[index++] = l;

}
Vig Killa
  • 19
  • 6

1 Answers1

0

Three possibilities. books is null or books[i] is null. If there was an error in equals or if l is null you would see it in the StackTrace too.

CHF
  • 264
  • 4
  • 17
  • two only. `l` can be null because it's not dereferenced (there is no `l.something()`) – zapl May 14 '16 at 23:00