aspose file tools*
The moose likes Beginning Java and the fly likes Java file - Anyone willing to look at my java file and see what the probelm is Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java file - Anyone willing to look at my java file and see what the probelm is" Watch "Java file - Anyone willing to look at my java file and see what the probelm is" New topic
Author

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

leila la
Greenhorn

Joined: Apr 26, 2012
Posts: 19
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

Joined: Oct 13, 2005
Posts: 39791
    
  28
Please post the code here. Also post full details of the errors.
leila la
Greenhorn

Joined: Apr 26, 2012
Posts: 19
okay
leila la
Greenhorn

Joined: Apr 26, 2012
Posts: 19
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

Joined: Oct 24, 2010
Posts: 563
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.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42599
    
  65
Also note that you should never compare strings using "=="; that's what the "equals" method is for.


Ping & DNS - my free Android networking tools app
leila la
Greenhorn

Joined: Apr 26, 2012
Posts: 19


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

Joined: Apr 26, 2012
Posts: 19
But when I do put a single = sign, I get errors
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42599
    
  65
Which line is 336? Which object is null?

I didn't say to use "=", I said to use the "equals" method.
leila la
Greenhorn

Joined: Apr 26, 2012
Posts: 19
if (jcomboBox_LastTank.getSelectedItem() == "Blue")
{
g2dImg.setPaint(Color.blue);


Thats the line at 336
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8196
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
leila la
Greenhorn

Joined: Apr 26, 2012
Posts: 19
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

Joined: Mar 17, 2011
Posts: 8196
    
  23

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

Joined: Apr 26, 2012
Posts: 19
Okay I have used the equal method, I still get the same error at the same line
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8196
    
  23

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

Joined: Oct 13, 2005
Posts: 39791
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java file - Anyone willing to look at my java file and see what the probelm is