2

I am geting a null pointer exception when i try to run this method, the goal is to populate the booklist object array but not over 3 objects. The error occurs when i set booklist[0] = b

private Book [] booklist;
public boolean borrowBook(Book b)
{
    if(booklist == null)
    {
        booklist[0] = b;
        System.out.println(this.name+" has successfully borrowed "+b);
        return true;
    }
    if(booklist.length < 3)
    {
        booklist[booklist.length] = b;
        System.out.println(this.name+" has successfully borrowed "+b);
        return true;
    }
    System.out.println(this.name+" has reached the borrowing limit! Return those books "+this.name);
    return false;
6
  • 4
    You need to initialize booklist. Commented Oct 1, 2014 at 2:06
  • I am not that knowledgeable with object arrays, can i initialize it and have my borrowBook method work the way it is? Commented Oct 1, 2014 at 2:08
  • 2
    of course if booklist == null you cannot access booklist[0] Commented Oct 1, 2014 at 2:08
  • default datatype is null for objects Commented Oct 1, 2014 at 2:08
  • what would i put in place of booklist[0] = b then Commented Oct 1, 2014 at 2:09

5 Answers 5

4

You need ArrayList instead of array

ArrayList<Book> booklist = new ArrayList<Book>();

public boolean borrowBook(Book b){
    if(booklist.size() == 0){
        booklist.add(b);
        System.out.println(this.name+" has successfully borrowed "+b);
        return true;
    }
    if(booklist.size() < 3){ //I'm not sure what you are trying to achieve here
        booklist.add(booklist.size(), b);
        System.out.println(this.name+" has successfully borrowed "+b);
        return true;
    }

    System.out.println(this.name+" has reached the borrowing limit! Return those books "+this.name);
    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

3

Try this.

private Book [] booklist;

public boolean borrowBook(Book b) {
    if (booklist == null) {
        booklist = new Book[3];
        booklist[0] = b;
        return true;
    }
    for (int i = 0; i < booklist.length; i++) {
        if (booklist[i] == null) {
            booklist[i] = b;
            return true;
        }
    }
    return false;
}

public static void main(String[] args) {
    Book caller = new Book();
    System.out.println(caller.borrowBook(new Book()));
    System.out.println(caller.borrowBook(new Book()));
    System.out.println(caller.borrowBook(new Book()));
    System.out.println(caller.borrowBook(new Book()));
}

1 Comment

Thanks for this! , Other answers were all right , but my teacher expects me to use just a normal array for this project. Thanks everyone , and thank you wittakarn
1

Likewise, @August said. Before line booklist[0] = b;

You must initial by using booklist = new Book[3];

3 Comments

Would my method still work at not letting it store more than 3 books into this array?
how can i check how many non-null objects are in booklist. By initializing it to length 3 , if(booklist.length < 3) will not work. what way can i check
I post my new answer, you can entertain. Good luck.
1

Java arrays are not dynamically sized, it's possible to write a method to do it. For example,

private static Book[] addBook(Book[] arr, Book b) {
    int newLen = (arr == null ? 1 : arr.length + 1);
    Book[] dest = new Book[newLen];
    System.arraycopy(arr, 0, dest, 0, arr == null ? 0 : arr.length);
    dest[newLen - 1] = b;
    return dest;
}

Then you could use it with something like

public boolean borrowBook(Book b) {
    if (booklist == null || booklist.length < 3) {
        booklist = addBook(booklist, b);
        System.out.println(this.name + " has successfully borrowed " + b);
        return true;
    }
    System.out.println(this.name + " has reached the borrowing limit! "
            + "Return those books " + this.name);
    return false;
}

Alternatively, you could just allocate an array of size n or use a collection like an ArrayList.

Comments

1

Initializing booklist will fix the NullPointerException, but your method's logic will still be flawed. It appears that you want to append a new book to the array of books checked out, whereas your method will only set the last element in the array.

To fix this, you could use a counter that stores the index of the last book inserted. Or, I suggest using a growable array such as ArrayList, which supports appending new elements with ArrayList#add.

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.