1

I'm developing a library management program (it's an assignment from an online course), and I can't solve a nullpointerexception error at runtime.

This is the program so far:

//file Library.java
public class Library {

String address;
Book[] collection;
int collectionCounter = 0;

    // Add the missing implementation to this class
    public static void main(String[] args)
    {
        // Create two libraries
        Library firstLibrary = new Library("10 Main St.");
        Library secondLibrary = new Library("228 Liberty St.");
        // Add four books to the first library
        firstLibrary.addBook(new Book("The Da Vinci Code"));
        firstLibrary.addBook(new Book("Le Petit Prince"));
        firstLibrary.addBook(new Book("A Tale of Two Cities"));
        firstLibrary.addBook(new Book("The Lord of the Rings"));
    }

    //Constructor
    public Library(String libraryName)
    {
        address = libraryName;
        collectionCounter = 0;
    }

    //Methods
    public void addBook(Book newBook)
    {
        System.out.println(this.collectionCounter);
        this.collection[this.collectionCounter] = newBook;
        this.collectionCounter += 1;
    }

And the other .java file, for the Book class:

public class Book {
String title;
boolean borrowed;

    // Creates a new Book
    public Book(String bookTitle) {
        // Implement this method
        title = bookTitle;
        borrowed = false;
    }
    // Marks the book as rented
    public void rented() {
        // Implement this method
        this.borrowed = true;
    }

    // Marks the book as not rented
    public void returned() {
        // Implement this method
        this.borrowed = false;
    }

    // Returns true if the book is rented, false otherwise
    public boolean isBorrowed() {
        // Implement this method
        return this.borrowed;
    }

    // Returns the title of the book
    public String getTitle() {
        return this.title;
    }

    public static void main(String[] arguments) {

        // Small test of the Book class
        Book example = new Book("The Da Vinci Code");
        System.out.println("Title (should be The Da Vinci Code): " + example.getTitle());
        System.out.println("Borrowed? (should be false): " + example.isBorrowed());
        example.rented();
        System.out.println("Borrowed? (should be true): " + example.isBorrowed());
        example.returned();
        System.out.println("Borrowed? (should be false): " + example.isBorrowed());
    }

}

This is the program output:

0
Exception in thread "main" java.lang.NullPointerException
    at Library.addBook(Library.java:59)
    at Library.main(Library.java:14)

I understand the error is caused by the array of books, but I don't really know what to do, I had never seen an object instantiation as an argument for a method. Thanks in advance!

2 Answers 2

5

You must create your array collection in order to be able to put anything into it. Replace

Book[] collection;

by

Book[] collection = new Book[BOOK_COLLECTION_SIZE];

with BOOK_COLLECTION_SIZE being a reasonably high number.

Even better, use a List instead of an array. That way you won't have to guess your library size beforehand:

List<Book> collection = new LinkedList<Book>();

Then addBook can look like this:

public void addBook(Book newBook)
{
    this.collection.add(newBook);
}

and you can get rid of collectionCounter. If you ever really need the number of books, you can get it using this.collection.size().

Sign up to request clarification or add additional context in comments.

4 Comments

When people post an answer one second before you can... exactly what I was going to say xD (apart from that I don't see why you would use LinkedList here instead of ArrayList).
Hey, thanks for the answer, when you say that addBook can look like those four lines of code, do you mean in the case that I opt for the linked list approach or does it work with the array approach too?
@Frank Using add only works, if you choose to go for the List approach.
Ok, thanks, will accept answer as soon as I can, site says it will be in 2 minutes. Cheers!
3

The problem lies in your Library class. It's addBook method uses this.collection, however this was only declared, hence still being null.

public void addBook(Book newBook)
{
    System.out.println(this.collectionCounter);
    this.collection[this.collectionCounter] = newBook;
    this.collectionCounter += 1;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.