Results 1 to 7 of 7
  1. #1
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default Whats wrong with this tiny method?

    I am getting a null pointer exception with this:

    Java Code:
    	 private int getRadioValue() {
    		 boolean b;
    		 b = 	radio1.isSelected();
    		 
    		   if(b==true)
    	            return 1;
    	        else
    	            return 2;
    		}
    I can post the entire program if you want, its about 220 lines...

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    'radio1' is null.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  4. #4
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Thanks!
    Was calling it in the constructor before I created it...

    It should be obvious...
    < 10th day of Java, nothing is obvious... total newb :)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by N00Bie View Post
    Thanks!
    Was calling it in the constructor before I created it...


    < 10th day of Java, nothing is obvious... total newb :)
    It will become obvious then. It was the only reference variable in the code above so it must be the one causing the problem. But next time, you should be able to know which line causes the error and then you will know in a second which variable is null.

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    You weren't asking about this, but consider this alternative code:
    Java Code:
    	 private int getRadioValue() {
    		if (radio1.isSelected())
    			return 1;
    		else
    			return 2;
    	}
    And since you're just getting started, get in the habit of doing this:

    Java Code:
    	 private int getRadioValue() {
    		if (radio1.isSelected()) [COLOR="Blue"]{[/COLOR]
    			return 1;
    		[COLOR="Blue"]}[/COLOR] else [COLOR="Blue"]{[/COLOR]
    			return 2;
    		[COLOR="Blue"]}[/COLOR]
    	}
    Those extra braces will save you some tear-your-hair-out frustration someday, when you add an extra line of code and can't figure out why it breaks everything.

    -Gary-

  7. #7
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Fubarable, ok - but the errors still confuse me as they dont say wahts wrong in plain English. Will get used to it with expereince I guess.

    gcalvin,
    Good advise! I'm coming from PHP and I usually do that...just took a shortcut today ;)

Similar Threads

  1. Whats wrong with my code
    By zit1343 in forum New To Java
    Replies: 9
    Last Post: 01-24-2011, 02:06 AM
  2. whats wrong
    By atenv in forum New To Java
    Replies: 6
    Last Post: 06-15-2010, 02:55 PM
  3. whats is wrong with this app??
    By mrajan in forum New To Java
    Replies: 4
    Last Post: 06-09-2010, 11:56 PM
  4. What is wrong with my tiny code?
    By Znote in forum New To Java
    Replies: 5
    Last Post: 11-07-2009, 05:42 PM
  5. Cannot understand whats wrong
    By Lehane_9 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 08:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •