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