Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3806844
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:53:52+00:00 2026-05-19T14:53:52+00:00

I am getting a null pointer when I run this piece of code. Its

  • 0

I am getting a null pointer when I run this piece of code. Its self-explanatory , two classes are used to model a Book and a Library.

I would appreciate it if someone can tell me where I am going wrong.

public class Library {


    private String address;
    private final static String workinghours = "9AM to 5PM";
    private Book[] bookcollection = new Book[6];
    private static int numberofbooks = 0;

    public Library(String libraryaddress) {
        address = libraryaddress;
    }

    public static void printOpeningHours() {
        System.out.println(workinghours);
    }

    public void addBook(Book newaddition) {
        bookcollection[numberofbooks] = newaddition;
        numberofbooks++;
    }

    public void printAddress() {
        System.out.println(address);
    }

    public void borrowBook(String bookname) {
        for (int i = 0; i < bookcollection.length; i++) {

            if (bookcollection[i].getTitle().equals(bookname)&&(!(bookcollection[i].isBorrowed()))) 
            {
                bookcollection[i].borrowed();
                break;
            }

        }

    }

    public void returnBook(String bookname) {
        for (int i = 0; i < bookcollection.length; i++) {

            if (bookcollection[i].getTitle().equals(bookname)) {
                bookcollection[i].returned();
                break;
            }

        }

    }

    public void printAvailableBooks() {
        for (int i = 0; i < bookcollection.length; i++) {

            if (!(bookcollection[i].isBorrowed())) {
                System.out.println(bookcollection[i].getTitle());
            }

        }

    }

    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"));

        // Print opening hours and the addresses
        System.out.println("Library hours:");
        printOpeningHours();
        System.out.println();

        System.out.println("Library addresses:");
        firstLibrary.printAddress();
        secondLibrary.printAddress();
        System.out.println();

        // 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");
        System.out.println();*/

        // Print the titles of all available books from both libraries
        System.out.println("Books available in the first library:");
        firstLibrary.printAvailableBooks();
        System.out.println();
        System.out.println("Books available in the second library:");
        secondLibrary.printAvailableBooks();
        System.out.println();

        // 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");
        System.out.println();

        // Print the titles of available from the first library
        System.out.println("Books available in the first library:");
        firstLibrary.printAvailableBooks();
    }
}

 

//Library uses objects of class book as members  

public class Book {

    String title;
    boolean borrowed;

    // Creates a new Book
    public Book(String bookTitle) {

        title = bookTitle;
        borrowed = false ;
    }

    // Marks the book as rented
    public void borrowed() {

        borrowed = true;
    }

    // Marks the book as not rented
    public void returned() {

        borrowed = false;
    }

    // Returns true if the book is rented, false otherwise
    public boolean isBorrowed() {

        return ((borrowed) ? true : false);

    }

    // Returns the title of the book
    public String getTitle() {

        return 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.borrowed();
        System.out.println("Borrowed? (should be true): "
                + example.isBorrowed());
        example.returned();
        System.out.println("Borrowed? (should be false): "
                + example.isBorrowed());
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:53:53+00:00Added an answer on May 19, 2026 at 2:53 pm

    Try changing all loops that look like

    for (int i = 0; i < bookcollection.length; i++) {
    

    to

    for (int i = 0; i < numberofbooks; i++) {
    

    As it stands now, you will try to do bookcollection[i].getTitle() on an entry in the books-array that has not yet been assigned a value (that is, still contains null).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm getting a weird null pointer exception in the last line of this code:
I am getting NULL pointer exception currently, when I run this thread public void
I reworked the code to my notification class. Its spitting an Null pointer Exception
Hi i am reading inbox using service but i m getting null pointer exception
I'm getting a Null pointer access: The variable 'numbers' can only be null at
I'm getting a null pointer on line 15...below (Session session = HibernateUtil.getSessionFactory.getCurrentSession();) public static
I am getting a null pointer exception report in the android developer console. I
I am getting crash reports on android.widget.ListView lv; lv.removeFooterView(v) The error is null pointer
I am getting null-exception throw error in the following code. What can be the
IN JAVA CODE IN JSP as below i m getting null value for fieldnoOfRecords.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.