Invoking an object`s method inside a for loop.

ZKR

Honorable
Mar 12, 2012
35
0
10,580
0
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:
Code:
public Book mostPagesBook ()
	{
		int bookNumber = 0, pages, mostPages = 0, largestBook = 0;
		for (Book checkBook : bookList)
		{	
			pages = checkBook.getNumPages();
			if (pages > mostPages)
			largestBook = bookNumber;
			bookNumber++;												
		}	
		return bookList[largestBook];	
	}
in Book :
Code:
	public int getNumPages ()
	{
		return numPages;
	}
Help is most appreciated.
 

ZKR

Honorable
Mar 12, 2012
35
0
10,580
0
I defined it that way:

Code:
public class Library
{
	private final int MAX_BOOKS = 150;
	private int totalBooks = 0;
	private int borrowedBooks;	
	private Book[] bookList;
	
	public Library ()
	{
		bookList = new Book[MAX_BOOKS];
		totalBooks = 0;
		borrowedBooks = 0;
	}
	
	public void addBook (String bookName, String authorName, int numPages)
	{
		if (totalBooks <= bookList.length)
		{
			bookList[totalBooks] = new Book (bookName, authorName, numPages);
			totalBooks++;
		}
		else 
			System.out.println ("Limit reached (150): Library is full.");
	}
What should I change?
 

randomizer

Distinguished
Moderator
Jul 24, 2006
880
0
19,260
62
You could add a simple check inside the loop like this:

Code:
if (checkBook != null)
{
    do_something();
}
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.
 

ZKR

Honorable
Mar 12, 2012
35
0
10,580
0

Yes, I fixed that as you said. Thanks.
 

ZKR

Honorable
Mar 12, 2012
35
0
10,580
0
randoMIZER, I have changed it to use a simple arithmetical count (I`m not sure the null was introduced in the course):
Code:
public Book mostPagesBook ()
	{
		int pages, mostPages = 0, largestBook = 0;
		
		for (int check = 0; check < totalBooks; check++)
		{	
			if (bookList[check].isAvailable())
			{
				pages = bookList[check].getNumPages();
				if (pages > mostPages)
				{
					mostPages = pages;
					largestBook = check;	
				}
			}											
		}	
		return bookList[largestBook];	
	}
 
Thread starter Similar threads Forum Replies Date
K Apps General Discussion 2
N Apps General Discussion 2
A Apps General Discussion 10
A Apps General Discussion 1
S Apps General Discussion 4
H Apps General Discussion 1
Y Apps General Discussion 1
DavidJT Apps General Discussion 5
D Apps General Discussion 3
B Apps General Discussion 2
C Apps General Discussion 1
L Apps General Discussion 1
E Apps General Discussion 5
T Apps General Discussion 1
U Apps General Discussion 3
V Apps General Discussion 1
P Apps General Discussion 2
L Apps General Discussion 1
harrystam Apps General Discussion 8
A Apps General Discussion 2
Similar threads
Solved! Is there a program to clean & organize 1,000’s of saved photos?
How do i transfer any PC game to another PC? (friend`s)
Torrent speed doesn't go over 1 MB/s
Xbox 360 S but no controller and i can not buy one. please someone Tell me how to configer my android device into a wired 'con
Hello, I did exactly as you described, it worked, but the gadget does not update automatically. Can you help me? Tks S. Lorber
can i run autodesk maya 's any version on dell inspiron 15 5559 ?
write a program to create a frame by creating an object to the subclass of an frame class
<object> code in HTML
I just got an iphone 7 and the copy/paste doesn't work the same as my 5 S - how do I copy and paste text messages on iphone 7?
color grading software for s-log
HTC 626(s) and Facebook Storage Issue
I have Word 2007. When I press '1', for example, '12' appears and when I press 'a' or 's', 'as' appears.
Removing the "S" in HTTPS so I can use internet.
Android using tons of data in background for system updates
Samsung Kies backup
A S G and U keys do not work on Facebook- fine everywhere else
Google talk authentication error with "error retrieving information from the server" in google play.
I purchased the 1099-misc software kit from staples and attempted to print out my 1099's and nothing will print. when i selec
Can I Turn My Galaxy S5 Into a Universal Remote?
Why can't I transfer from onedrive to a new folder in my computer. I have documents I want to save and since HP is no longer s

ASK THE COMMUNITY