Sign in with
Sign up | Sign in
Your question

Invoking an object`s method inside a for loop.

Last response: in Applications
Share
May 10, 2012 11:39:21 AM

Hey guys. Need your help again.
I have to make a Library that contains books.
In the Library class, a method called mostPagesBook, returns the book with most pages. It compiles, but errors when run:

Exception in thread "main" java.lang.NullPointerException
at Library.mostPagesBook(Library.java:32)
at TEMP.main(TEMP.java:29)
(POINTING AT THE pages = checkBook.getNumPages(); Line)


In Library:
  1. public Book mostPagesBook ()
  2. {
  3. int bookNumber = 0, pages, mostPages = 0, largestBook = 0;
  4. for (Book checkBook : bookList)
  5. {
  6. pages = checkBook.getNumPages();
  7. if (pages > mostPages)
  8. largestBook = bookNumber;
  9. bookNumber++;
  10. }
  11. return bookList[largestBook];
  12. }


in Book :
  1. public int getNumPages ()
  2. {
  3. return numPages;
  4. }


Help is most appreciated.
a b L Programming
May 10, 2012 12:01:23 PM

Look at your bookList. One of the entries in it is a null pointer rather than pointing to an instantiated object.
m
0
l
May 10, 2012 12:53:38 PM

I defined it that way:

  1. public class Library
  2. {
  3. private final int MAX_BOOKS = 150;
  4. private int totalBooks = 0;
  5. private int borrowedBooks;
  6. private Book[] bookList;
  7.  
  8. public Library ()
  9. {
  10. bookList = new Book[MAX_BOOKS];
  11. totalBooks = 0;
  12. borrowedBooks = 0;
  13. }
  14.  
  15. public void addBook (String bookName, String authorName, int numPages)
  16. {
  17. if (totalBooks <= bookList.length)
  18. {
  19. bookList[totalBooks] = new Book (bookName, authorName, numPages);
  20. totalBooks++;
  21. }
  22. else
  23. System.out.println ("Limit reached (150): Library is full.");
  24. }


What should I change?
m
0
l
Related resources
May 10, 2012 1:10:30 PM

also, "mostPages" is never reassigned with the current "pages" value, so you'll just go into that if every time
m
0
l
a b L Programming
May 10, 2012 1:54:56 PM

You could add a simple check inside the loop like this:

  1. if (checkBook != null)
  2. {
  3. do_something();
  4. }


Your best option is to make sure that there are no null pointers. Just from the code I'm looking at here I can only assume that your Book constructor must return null somewhere. Never do this, or you'll have to put null checks everywhere. If you have to, just return a dummy instance. Your mostPagesBook() method should not have to concern itself with whether or not the Book is null.
m
0
l
May 10, 2012 2:20:11 PM

mgwildi said:
also, "mostPages" is never reassigned with the current "pages" value, so you'll just go into that if every time

Yes, I fixed that as you said. Thanks.
m
0
l
May 10, 2012 2:22:37 PM

randoMIZER, I have changed it to use a simple arithmetical count (I`m not sure the null was introduced in the course):
  1. public Book mostPagesBook ()
  2. {
  3. int pages, mostPages = 0, largestBook = 0;
  4.  
  5. for (int check = 0; check < totalBooks; check++)
  6. {
  7. if (bookList[check].isAvailable())
  8. {
  9. pages = bookList[check].getNumPages();
  10. if (pages > mostPages)
  11. {
  12. mostPages = pages;
  13. largestBook = check;
  14. }
  15. }
  16. }
  17. return bookList[largestBook];
  18. }
m
0
l
May 10, 2012 2:36:51 PM

... found the answer. Thanks.
m
0
l
!