0

I'm making a library program that keeps a record of any books that are in the collection and how many copies of each book there are. Here is the code

import java.util.Arrays;
import java.util.Scanner;
public class Library{
  static String title;
  static String author;
  static int id;
  static int copies;
  static String date;
  static Book[] database = new Book[100];
  static int count=0;

  public static void main(String[] args){
    int i;
    Scanner s = new Scanner(System.in);
    do{
      addBook();
      System.out.println("would you like to add another book?");
      i=s.nextInt();
    }while(i == 0);
    database[0].viewDetails();
    database[1].viewDetails();
    checkingOut();
  }
  public static void addBook(){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter the title of the book you want to add to the collection");
    title=s.nextLine();
    System.out.println("Enter the author of the book you want to add to the collection");
    author=s.nextLine();
    System.out.println("Enter the publishing date of the book you want to add to the collection");
    date=s.nextLine();
    System.out.println("Enter the ID number of the book you want to add to the collection");
    id=s.nextInt();
    System.out.println("Enter the the number of copies that will be added into the collection");
    copies=s.nextInt();

    Book Book1 = new Book(date, author, copies, id, title);
    database[count] = Book1;
    count++;
  }
  public static void checkingOut(){
    boolean found=false;
    int idSearch;
    int i=0;
    Scanner s = new Scanner(System.in);
    System.out.println("Enter the ID number of the book you want to check out");
    idSearch=s.nextInt();
    while(i<database.length && found!=true){
      if(database[i].getIdentificationNumber() == idSearch){
        found = true;
      }
      i++;
    }
    if(found==true){
      database[i].checkOut();
      System.out.println("There are "+database[i].getNumberCopies()+" copies left");
    }
    else{System.out.println("There is no book with that ID number!");}
  }
}

I'm getting a null pointer exception on line 55 of my checking out method, I can't figure out why. Please let me know if you can spot what it is any help would be greatly appreciated.

4
  • 2
    Which line throws the NPE? Commented Nov 2, 2013 at 18:19
  • line 57.....I'm having another problem with my program though. In my add book method is there a way to make new book objects? by that I mean each time I run the method to make book1, then the 2nd time book2, then book3..... and so on. Because right now when I try to add a new book it's basically just resetting the information that I had made for the previous book. Commented Nov 2, 2013 at 20:28
  • How do I know which line is line 57? Do you expect me to count 57 lines of code? Could you just not put some form of marker or tell us what is on that specific line? Commented Nov 2, 2013 at 22:00
  • sorry, it's basically the while loop in the checkingOut method, something isn't working right with it im not sure how to fix it. If I create for example 3 book objects and place them in an array I can only use the checkingOut method with the first object in the array, I get a null pointer exception when I try to access the other objects. Commented Nov 3, 2013 at 16:07

3 Answers 3

1

if(found=true) will be always executed because the expression of the assignment returns the assigned value, and this will cause database[i].checkOut(); to be executed where it shouldn't.

You should write:

if(found)

That's why we avoid writing == when we compare booleans. It's enough to write if(someBoolean).

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

Comments

0

found == true, not found = true

Comments

0

should be if(found==true)

instead of = use == as = will always evaluate as true.

2 Comments

Thanks guys that was a silly error, can't believe I couldn't spot that.
I'm having another problem with my program though. In my add book method is there a way to make new book objects? by that I mean each time I run the method to make book1, then the 2nd time book2, then book3..... and so on. Because right now when I try to add a new book it's basically just resetting the information that I had made for the previous book.

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.