• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java file - Anyone willing to look at my java file and see what the probelm is

 
leila la
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a java code, I get no errors when I run the program, however when once I try to click the buttons in my program, I get errors in the console below and I'm not sure what they mean.
I have been trying to figure it out for 3 days...no luck!
Anyone Please help,
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the code here. Also post full details of the errors.
 
leila la
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay
 
leila la
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I hit run, My GUI and calculations and everything work fine, BUT when I click on the Visualize button I get a Java Lang Null pointer exception on the first line. I don't know what it means and Im not sure what to do about it. All my naming and everything is correct, as I have double checked.

 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the details link says, it's incredibly helpful to include the error message with the stack trace, copied and pasted exactly as it appears to you. From what you've told us so far, we can only suggest that either jcomboBox_apple hasn't be initiated properly and/or the getSelectedItem() is returning null.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that you should never compare strings using "=="; that's what the "equals" method is for.
 
leila la
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Its really long. And what do you mean it returns null? I have not included the colors..or i don't understand :/
 
leila la
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when I do put a single = sign, I get errors
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which line is 336? Which object is null?

I didn't say to use "=", I said to use the "equals" method.
 
leila la
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (jcomboBox_LastTank.getSelectedItem() == "Blue")
{
g2dImg.setPaint(Color.blue);


Thats the line at 336
 
Winston Gutkowski
Bartender
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
leila la wrote:if (jcomboBox_LastTank.getSelectedItem() == "Blue")
{
g2dImg.setPaint(Color.blue);

Thats the line at 336

OK, and you're getting an NPE. Since "Blue" can't possibly be null, and it doesn't matter if getSelectedItem() returns a null (the expression will simply be false), the problem must be with jcomboBox_LastTank.

Your task: find out why it's null.

Also: Listen to Ulf and get rid of that '=='; he talketh sense.

Winston
 
leila la
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply y'all.

Ok I shall remove the == sign, but what do i put in that place? the word "equals" or just one equal sign? If it is just one equal sign, i get an error.
 
Winston Gutkowski
Bartender
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
leila la wrote:Ok I shall remove the == sign, but what do i put in that place? the word "equals" or just one equal sign? If it is just one equal sign, i get an error.

No, you put a call to the equals() method, which is how you should ALWAYS compare objects. ie:
if ( X.equals(Y) ) ...

Winston

PS: A little tip for you - If one of the objects is a String literal, it's often a good idea to put it first, ie:
if ( "Blue".equals(jcomboBox_LastTank.getSelectedItem()) )
rather than
if ( jcomboBox_LastTank.getSelectedItem().equals("Blue") )
because a literal can't possibly be null.
 
leila la
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I have used the equal method, I still get the same error at the same line
 
Winston Gutkowski
Bartender
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
leila la wrote:Okay I have used the equal method, I still get the same error at the same line

And I explained what the problem was two posts back.

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
leila la wrote:But when I do put a single = sign, I get errors
And when you use a == sign, you get different errors. You need to learn what the different operators mean, rather than guessing what will get you out of compiler errors.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic