views:

101

answers:

2

correct me if I'm wrong.

If str has a character such as "" in it then running:

str.toLowerCase(Locale.English);

throws a null pointer exception. That's the behavior I'm seeing.

So what's the deal here? What's going on? It isn't specified that toLowerCase throws a null pointer exception.

Is there an easy way to get around this? I need the str to be lower case to be able to use a case insensitive contains check with another string, but I need the string to contain those characters for it to be displayed correctly.

What would you say is the most efficient solution if there is no "easy way?"

+2  A: 

Rather than converting to lower case, I would suggest trying the String object's equalsIgnoreCase() method.

Although, as the comments do say, there is no reason for the toLowerCase() function to be throwing a null pointer exception unless a parameter is null. I would either use a debugger to check the state of str or wrap the toLowerCase() function call in an if statement and if str is null, do something (like throw an exception, print a statement, log a message) and not execute the function call.

Thomas Owens
+1  A: 

Hmm, it works for me:

String upper = new String("asdCSD4rSDFSDFS•••••••••XCj");
String lower = upper.toLowerCase(Locale.ENGLISH);
System.out.println("Lower " + lower);

Lower asdcsd4rsdfsdfs•••••••••xcj
Jon