Calling a dlete method from the Book class from the main method gives a NullPointerException. Works fine when running the delete method from within the Main class.
Exception in thread "main" java.lang.NullPointerException at algorithms/algorithms.Main.main(Main.java:20)
What am I missing?
Main:
private static Book book;
public static void main(String[] args) {
Book[] bookArray = {new Book(123, "Book1"),
new Book(321, "Book2"),
new Book(456, "Book3"),
new Book(654, "Book4"),
new Book(789, "Book5")};
System.out.println(bookArray.length);
bookArray = book.removeBook(bookArray, 456);
System.out.println(bookArray.length);
}
Book class method (left out attributes, getters and setters):
public Book[] removeBook(Book[] books, int findISBN) {
Book[] newArray = new Book[books.length - 1];
int j = 0;
for (int i = 0; i < books.length; i++) {
if(books[i].getISBN() != findISBN) {
newArray[j] = books[i];
j++;
}
}
return newArray;
}