Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method only working once

 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is part of the missing loadApplication, sorry I thought I had copied it.



 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic