• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting null pointer exception  RSS feed

 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should the piece of code below set prevent the comboBox from being used? All it does at the minute return a null pointer exception. See im using the same window but I have an if statement so if a condition is true then it will change certain aspects of the window. I want it to be true if it equals export which is does and it's populated fine but when I try and hide it if the condition equals import it returns a null pointer, anyone any idea why?

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that causes a NullPointerException, it's because comboBoxEnv is null. Are you sure you've initialised that variable?
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
comboBoxEnv is probably null. Post more of your code (in code tags) for more help.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try and use it - it is populated with what is should be and the code below is what I am using to populate it - I just get the null issue when I attempt to disable the comboBox so it can't be used.



I've even checked that it is populated by using this piece of code to print them out to console
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the full stack trace of your NullPointerException?
Also where is the code in your first post in relation to the code above?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the combo box a field or is it the same as that local variable you showed earlier?
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell - It's just the code I had in the previous post

How do you print the full stack trace exactly? Are you referring to all the errors that appear in the console?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds to me that what is likely to be happening is that you have two comboBoxEnv variables. Which I think is what Campbell was getting at. For example, if you declared comboBoxEnv as a field in the class.

In your second set of code you declare comboBoxEnv on line 1 as a local variable. If you have a field called comboBoxEnv, then you now have two variables with the same name, and only one of them has been initialised. If you're original line of code is then referring to the other variable, then it is still null.

So it's all to do with the scope of the variables, which we can't determine from one line!
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I had it at the top of my code like so:


I was using that when setting visible - I'm guessing thats why it didn't work as it doesnt actually have anything in it Whether as my other one actually does......


Would that work?




that now references my declared comboBox at the top of my code - whether as before it did not



*EDIT* Yayyyyy! I got it to work! Thanks so much for pointing out my mistake! Completely forgot I had declared it at the top of my code then hadn't even done anything with it! I can move on with my next task now!:P
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Hampton wrote:@Campbell - It's just the code I had in the previous post

How do you print the full stack trace exactly? Are you referring to all the errors that appear in the console?


Th following code produces an exception:

The output in the console is:
Exception in thread "main" java.lang.RuntimeException: All gone wrong
at stuff.TestThings.stackTraceThing(TestThings.java:9)
at stuff.TestThings.main(TestThings.java:5)

That's the full error message and stack trace (the flow of code that caused the problem), which shows us exactly where the exception occurred.
I just wanted to check it was actually thrown from that line, and not internal to the combo box...but it seems that wasn't needed.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Dave for your reply! My stack trace was huge so I wasn't 100% on whether you were referring to that or not - I didn't want to print 30 lines of code to find out I'd wasted your time!

I'll keep that in mind for future though as now I know - so thank you!
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have that combo box as a field as well, you will have problems if you shadow it with a local variable.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell what do you mean by this - would you mind giving an example please?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!