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?

You need to initialize the array before you can use it.

books = new Book[MAX_BOOKS];

where MAX_BOOKS would be whatever initial size you wanted to make the array.

thanks alot! I used that and it solved my problem with the exception.

I have ran into another problem. Whenever I run the program nothing is printed out when try to run the printAvailibleBooks() method.

Here is my code so far:

public class Library {
	Book[] books = new Book[20];;
	int numBooks = 0;
	String address;
	static String hours;
	boolean borrowed = false;
	
	
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;
}

void printAvailableBooks() { //supposed take a book out of the list if borrowed
	while(numBooks < 5) {
	if (numBooks < 0) {
		System.out.println("Were sorry there are no books avalible at this library");
	break;
	}
	else if(books[numBooks].borrowed() == true) { //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
		numBooks++;
		continue;
	}

	else if(books[numBooks].borrowed() == false) {
	System.out.println(books[numBooks]);
	numBooks++;
	continue;			
	}
 
	
			}
	} 

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(hours);

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:");
firstLibrary.printAvailableBooks();


System.out.println("Books available in the second library:");
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(); 
 
	} 

}

I have tried a lot (for and while loops), all it does is either go into a continuous loop, or print this book is checked out for all the books.

How can I modify my printAvailibleBooks method? Also, how can I modify it so that it will print out the book title instead of the reference in the array?

Thank You

This article has been dead for over six months. Start a new discussion instead.