• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code suddenly stopped working- null pointer  RSS feed

 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been running my code as I have been going on with it all day and all of it has worked fine. I've tested and tested, then I suddenly press my ok button for a null pointer exception to be thrown. I thought perhaps it's something to do with the two new methods I had added so I copy pasted them out and tried again - still got an exception.

I kept pushing back my progress up until the point I knew for sure it was last working - still null pointer. If an item isnt selected from the comboBox when the ok button is clicked it's simply supposed to produce an error message stating the user is to choose an item. It has definately been working as I've been making a test case on it to ensure I knew it was working.

My button code is below:




*EDIT* I moved that piece of code into my other method - that fixed it even though it's been there all afternoon
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you check if comboBoxEnvironments.getSelectedItem() returns null (line 11) after you have already dereferenced (called a method on) the return value (line 9) ?
I suspect in your old version of the code line 9 came somewhere after line 11 - probably in the else block - or it didn't have the toString method call as part of it.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put it into another method that actually uses value. It still happens though if I press my ok button when an item from the comboBox isn't chosen the value of my String value; is null so it throws a null pointer exception.


Could I get round this by using some sort of catch to catch the exception?

I need to use it as whatever object is chosen from the comboBox is stored into String value. value is then compared to line value to see if they're the same.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can always set a empty string to the empty select item. This way when you do comboBox.getSelectedItem() gives your "" rather than null. But then you probably want to prompt error if the combo box is empty string ("") AND null
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like you didn't quite understand Joanne's point. You are already dealing correctly with the case where nothing is selected...but you're doing it too late. The exception has already been thrown by that point. Move the initialisation of the value variable into the block where you know it's safe because you've already checked.

Another different thing - do you realise your else block is empty, and checkEnvironmentExists() gets called regardless?
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matthew - I realised that trying to catch a null pointer exception is a pretty bad idea but I declared it within my class it's literally the first thing I do -> String value;

I know about my else statement thank you I was altering my code at the time and merely forgot to take it out, I was planning on using another method once I had finished it.

I've put it in here because this is where Im comparing the line and the value together. If I move it it then breaks it. I thought using String value = null; would of fixed it as I'm already setting it to something but it makes no difference.

*EDIT* tried removing global String value; and changing the value in my method below to String value = but that didn't work - after it hits my button code it then proceeds to go on to this method to still throw me a null pointer D:


 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still seem to be misunderstanding.
comboBoxEnvironments.getSelectedItem() is what is returning null, so you need to check its value before you use it.
Like this


 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must admit, it's not clear to me what you're actually changing. The bottom line, though, is that this:
will throw an exception every time there is no selected item, because you are trying to call .toString() on null.

Therefore, the first thing you should do, in the event handler, is check if this is the case or not. And if it is, avoid doing anything with the value:
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh Sorry! I feel bad now for not figuring out what you both meant sooner - thank you very much for pointing that out and taking your time to do so.My code works fine now, thank you I appreciate it!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!