• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pantry Class NullPointException Error  RSS feed

 
Sinn Ota
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program runs a NullPointException Error when I run the MixedFruit method.
How do I fix that?


Driver:




Pantry Class:




 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added code tags to your post. It now displays much better, and is easier to read. You can see how to do that yourself at our UseCodeTags page.

Can you post the full message? it shows you the exact line where the NPE is thrown, and what reference is null. The simple answer is to not call methods on null references - but WHY you have a null reference can be a little trickier to find - especially since we do not know where you are getting this.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid this will not sort out your Exception, but …

That select method has some confusing style. Have a look at our suggestions about indentation and braces, in these three links: 1 2 3.
As you have the code it is difficult to tell where the ifs and elses start and end. Look at lines 60 to 65. If you indent them consistently and have braces in the right places, their effect will become crystal clear.

I suggest you consider another structure for your method, rather than multiple ifs. Have you come across switch‑case?
I shall refer you to another style guide. The old Sun style guide; I wish Oracle would update it rather than hiding it. Look at §10.5.2, which warns against writing if(something) return true; else return false;
It says you can simply write return something;
So you can get rid of several lines by writing a single return. I know you have if…false…true, so you would have to invert the test. Try this:-
return selected != null;

Something which might help with your Exception. Avoid setting things to null if you possibly can. Rather than all those nulls in the constructors, why not initialise those fields to point to empty Jars. Can you write
new Jar(0)?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the jars have less than 2 in, mix them all in one Jar? I like it
 
Sinn Ota
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The NullExceptionError runs when I tries to execute the MixedFruit Method for the second time
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sinn Ota wrote:The NullExceptionError runs when I tries to execute the MixedFruit Method for the second time

again, I say:
fred rosenberger wrote:Can you post the full message? it shows you the exact line where the NPE is thrown, and what reference is null. The simple answer is to not call methods on null references - but WHY you have a null reference can be a little trickier to find - especially since we do not know where you are getting this.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sinn Ota wrote:The NullExceptionError runs when I tries to execute the MixedFruit Method for the second time


That doesn't seem particularly surprising, because that method assumes that jar1, jar2 and jar3 are all not null, but the first time you call it you set two of them to null.
 
Sinn Ota
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.NullPointerException
at Pantry.mixedFruit(Pantry.java:104)
at Pantry.spread(Pantry.java:73)
at Exercise1.main(Exercise1.java:43)
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you fix the problem Matt pointed out? If so, post the code that you're now using. If not, start with that.

I note that the "select" method returns a value, but that your code ignores it. Is that intentional?
 
Sinn Ota
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added two extra statement in the if statement

public void mixedFruit()
{

if(jar1.getCapacity() <=2 && jar2 != null && jar2.getCapacity() <= 2 && jar3 !=null && jar3.getCapacity() <= 2)
{
Jam mix = new Jam("Mixed Fruit", "7/4/86", (jar1.getCapacity() +
jar2.getCapacity() + jar3.getCapacity()));
jar1 = mix;
jar2 = null;
jar3 = null;
selected = jar1;

}
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't like it, I am afraid. It looks as if you were covering up the symptoms (the cough, the Exception) and leaving the real problem (the pneumonia, the nulls) untreated.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!