So ist die Methode, um zu sehen, ob ein Buch in der Bibliothek vorhanden ist:Warum habe ich eine NullPointerException?
public boolean exists(Book l) {
for (int i = 0; i < index; i++) {
if (books[i].equals(l)) {
return true;
}
}
return false;
}
Ausgang:
Exception in thread "main" java.lang.NullPointerException
at Library.exists(Library.java:13)
Was mache ich falsch hier?
So bekam ich die Antwort dank euch :) Also das Problem war, dass ich keine Bücher in der Bibliothek hinzugefügt habe! Die Methode, Bücher hinzuzufügen:
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;
}
Ihr Array enthält null Elemente, weil Sie nichts hinzugefügt haben? –
Eines Ihrer Bücher im Array ist null. –
oder sogar 'Bücher' selbst ist null. – zapl