Results 1 to 10 of 10
- 07-11-2011, 08:04 AM #1
Member
- Join Date
- May 2008
- Posts
- 12
- Rep Power
- 0
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!
Test code: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); }
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"));
- 07-11-2011, 08:47 AM #2
Member
- Join Date
- Mar 2011
- Posts
- 44
- Rep Power
- 0
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 luckLast edited by sibernewf; 07-11-2011 at 08:54 AM.
- 07-11-2011, 08:59 AM #3
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 09: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.
- 07-11-2011, 09:05 AM #4
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
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("")){...}
- 07-11-2011, 09:12 AM #5
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
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 09:45 AM.
- 07-11-2011, 09:21 AM #6
@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.
Try running it and see what happens.Java Code:String s = null; System.out.println(s.equals(null));
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 09: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.
- 07-11-2011, 09:31 AM #7
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
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); }
- 07-11-2011, 09:45 AM #8
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:
Since s is null, it has no value to be checked by .equals() which results in your nullpointerexception.Java Code:String s = null; System.out.print(s.equals(""));- 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.
- 07-11-2011, 10:14 AM #9
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
interesting
- 07-11-2011, 12:27 PM #10
Member
- Join Date
- May 2008
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Exception in thread "main" java.lang.NullPointerException
By Borysator in forum New To JavaReplies: 2Last Post: 01-10-2010, 12:49 PM -
Exception in thread "main" java.lang.NullPointerException
By syarizma in forum Advanced JavaReplies: 6Last Post: 08-06-2009, 11:50 AM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 6Last Post: 07-16-2009, 03:30 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 1Last Post: 07-16-2009, 10:35 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks