aspose file tools*
The moose likes Beginning Java and the fly likes method only working once Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method only working once" Watch "method only working once" New topic
Author

method only working once

Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Ok, so the first time I call the method it works, the second time it throws a NullPointerException.

Here is the run



Here is the main class



And here is the Application class:
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
when you are putting the first element in array ? you will get that much software records, what you put in array. So, if app[nth] is null, invoking a method will certainly complain.
Stefan Brandenberger
Greenhorn

Joined: Aug 31, 2009
Posts: 12
Hi Mike

My guess is, that getNextApplication () always returns null. But to verify, I'd like to see the code of method loadApplications () that's called in the parameterless Constructor of Application.

Stefan
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Your getNextApplication method is in fact working. It returns whatever it finds, which is null. Now it would appear to be reading the elements of an array. Remember that an array of any reference type is implicitly filled with null references. If you don't explicitly fill the array with real references, you will eventually get one of the nulls back; when you get to the main method and try to something with that null the JVM says
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Here is part of the missing loadApplication, sorry I thought I had copied it.



Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
ok, you are saying first time. getNextApplication() returns an Application object initialized by parameterized constructor and does not initialize your apps array (not even a single value). i value at that time is 0. so apps[0] is null. Avoid reassigning or workaround it.
Stefan Brandenberger
Greenhorn

Joined: Aug 31, 2009
Posts: 12
On line 5 of class Software you create a new Application with the parameterless Constructor. So in this instance, the Application-array apps holds 4 instances of Application, created in your loadApplications()-method. On line 7 you call getNextApplication() on it, which returns the first entry of the array. Note, that this Application was created with the second constructor (new Application("Aquatint","1.1","Stick Software","","Mike","crocodiles","http://www.sticksoftware.com/","","");). Therefore it's apps-array is empty, because you don't call loadApplications() in that constructor.

So app on line 10 is null. Bingo.

One mistake, you make, is to reuse the variable app again and again. That way you call getNextApplication() always on a new instance of Application. Try this:


...but don't try it more than 4 times .

By the way: what is the intention to hold a list of Applications in Application-class? Shouldn't it rather be a class ApplicationPortfolio or so, to hold the Applications?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: method only working once
 
Similar Threads
Code review
Ok, applauded a little too soon...sort problem
Writeing / Reading ArrayList
Best way to serialize an object?
Array Objects - sorting?