Results 1 to 10 of 10
  1. #1
    d0nmin0 is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default Exception in thread "main" java.lang.NullPointerException

    Hi,
    I require help in understanding why I got a null exception for this case.
    I am having problems with this method when null value is passed in.

    I would like to know how to solve this problem. Thanks!

    Java Code:
    public Place addPlace(String placeName, int xPos, int yPos)
    		throws IllegalArgumentException
    	{
    		//if string is null -- placed a check for null but error is still being caught
    		if (placeName.equals("") || placeName == null) {
    			
    			//throw new NullPointerException("Place name is null");
    		}
    		
    		//placeName not valid, return error
    		if (!placeName.matches("[a-zA-Z][a-zA-Z0-9_]*")) {
    			throw new IllegalArgumentException("Place name is not valid: " + placeName);
    		}
    Test code:
    Java Code:
    String s = null;
    		Place p1 = m.addPlace(s,10,11);
    		//Place p2 = m.addPlace("Pb",10,11);
    		
    		System.out.println("toString: \n" + m.toString());
    		System.out.println("getPlaces: " + m.getPlaces());
    		System.out.println("findPlace: " + m.findPlace("pa"));

  2. #2
    sibernewf is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    EDIT:
    - - - - - - - - -
    Sorry, I just tried that and had a NullPointer still as well.

    Just a guess, but can you try:
    if (placeName.equals("") || placeName.equals(null)) {

    I'm not sure if it will work, but being a String, its better to use the .equals() method.

    Good luck
    Last edited by sibernewf; 07-11-2011 at 09:54 AM.

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Whats the error message? You say you have a problem but you don't really describe it. Does it even compile? If it does, what happens when a null value is passed?
    Last edited by Dark; 07-11-2011 at 10:15 AM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    I would imagine you need to check if your placeName is null before you check if it is an empty string

    if(placeName == null || placeName.equals("")){...}

  5. #5
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    I would think '==' is incorrect for seeing if it has a null reference? otherwise it trying to compare two string values by which one is null. What is the string value of null?

    Dark:

    Try This Code as to why this wouldn't work... You can't call the equals method on a String with a null reference. Additionally equals is not for comparing references
    Java Code:
    public class test {
    	public static void main(String[] args){
    		String foo = null;
    		System.out.println(foo.equals(null));
    	}
    
    }
    Last edited by yellowledbet; 07-11-2011 at 10:45 AM.

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    @yellowledbet, Its a logic phrase, he's using an OR comparison so once a condition is met it returns true. If the string doesn't equal "" then it checks if the string is null. Whenever a condition is met in an OR statement it breaks from checking the rest of the conditions and returns true.

    x y result
    true true true
    true false true
    false true true
    false false false

    @sibernewf
    When you use .equals() on null, it throws a null pointer exception if the string is null. Look at this code.
    Java Code:
    String s = null;
    System.out.println(s.equals(null));
    Try running it and see what happens.

    Edit: Ha lol I was just typing without thinking when I posted that, I quickly removed my error. Apparently you refreshed before I removed it.
    Last edited by Dark; 07-11-2011 at 10:23 AM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  7. #7
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    I have no idea what this is in reference too.

    Is it about reversing the tests in the if statement?

    multiple or statements will break after the first true condition is met. Therefore, it would make sense to check for a null reference as the first condition.

    as shown below x never increments nor is the 2nd condition true.

    Java Code:
    int x = 1;
    if(true || (x++ == 45) ){
    			System.out.println(x);
    		}

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Hmm, while I don't see your logic being any more valid then it being reversed and your example is just bad practice. I did a test program and it is giving me a null pointer exception if I check for null after checking for an empty string.

    After actually taking the time to look at it, I'll explain whats happening.

    .equals() is trying to access the value of your string, now if your string has no value then it throws a null pointer exception. So check for the null statement first to avoid trying to check a null string's value. Yellowledbet is correct, though he wasn't very clear in his explanation and it sounded more like a guess IMO.

    If you want to see if try this:
    Java Code:
    String s = null;
    System.out.print(s.equals(""));
    Since s is null, it has no value to be checked by .equals() which results in your nullpointerexception.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  9. #9
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    interesting

  10. #10
    d0nmin0 is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    Hi,
    After swapping the check around, I am finally able to catch the exception. Thanks!
    Initially i thought it was a logic mistake, but i realized that precedence do matter as well.
    Thanks for the tip!

Similar Threads

  1. Replies: 2
    Last Post: 01-10-2010, 01:49 PM
  2. Replies: 6
    Last Post: 08-06-2009, 12:50 PM
  3. Replies: 6
    Last Post: 07-16-2009, 04:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 11:35 AM
  5. Replies: 1
    Last Post: 07-16-2007, 07:32 AM

Posting Permissions

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