I am having trouble with a project that is used to create a simple library database. The code does compile but during runtime, it throws a nullPointerException. It points to addBook in my main method.
Here is my code:
public class Library {
Book[] books;
int numBooks;
String address;
static String hours;
void addBook(String bookTitle) { //This where I am having trouble populating my array
books[numBooks] = new Book(bookTitle);
numBooks++;
}
public boolean borrowBook(String Title) { //Marks the book as borrowed
books[numBooks] = new Book(Title);
return books[numBooks].borrowed(); //refers to a method in another class I created
}
public boolean returnBook(String Title) { //Marks the book as returned
books[numBooks] = new Book(Title);
return books[numBooks].returned();
}
public Library(String street) { //creates new street address
street = address;
}
public String printAddress() { //returns the street address when referenced
return address;
}
public Book printAvailableBooks() { //supposed take a book out of the list if borrowed
for(numBooks = 0; numBooks < 10; numBooks++) {
if (numBooks>0) {
System.out.println("Were sorry there are no books avalible at this library");
}
else if(books[numBooks].borrowed() == false) { //references a varible in the Book class
System.out.println("We are sorry this book is checked out."); //this line is supposed to be java.lang.string
}
else if(books[numBooks].borrowed() == true) {
System.out.println(books[numBooks]);
}
continue;
}
return books[numBooks];}
static String printOpeningHours() {
hours = "We are open from 9-5.";
return hours;
}
public static void main(String[] args){
Library firstLibrary = new Library("10 Main St."); // Create two libraries
Library secondLibrary = new Library("228 Liberty St.");
firstLibrary.addBook("The Da Vinci Code");// This is where I am getting my exception
firstLibrary.addBook("Le Petit Prince");
firstLibrary.addBook("A Tale of Two Cities");
firstLibrary.addBook("The Lord of the Rings");
System.out.println("Library hours:"); // Print opening hours and the addresses
printOpeningHours();
System.out.println();
System.out.println("Library addresses:");
firstLibrary.printAddress();
secondLibrary.printAddress();
// Try to borrow The Lords of the Rings from both libraries
System.out.println("Borrowing The Lord of the Rings:");
firstLibrary.borrowBook("The Lord of the Rings");
firstLibrary.borrowBook("The Lord of the Rings");
secondLibrary.borrowBook("The Lord of the Rings");
// Print the titles of all available books from both libraries
System.out.println("Books available in the first library:");
System.out.println(firstLibrary.printAvailableBooks());
System.out.println("Books available in the second library:");
System.out.println(secondLibrary.printAvailableBooks());
// Return The Lords of the Rings to the first library
System.out.println("Returning The Lord of the Rings:");
firstLibrary.returnBook("The Lord of the Rings");
// Print the titles of available from the first library
System.out.println("Books available in the first library:" + firstLibrary.printAvailableBooks());
}
}
How can I change my addBook method, so that it will populate my array?