0

So I have a set of different strings made if a certain key is pressed using keyPressed(), but some of those strings i want to convert to characters and tried doing so like this:

char keyChar = keyChanged.charAt(0);

except now i get a nullpointerexpression. if it matters, keyChanged would be a 1 letter string like "r".

Muhammad Shahab
  • 119
  • 1
  • 1
  • 4
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Yosef Weiner Dec 04 '15 at 13:08

2 Answers2

0

You can avoid a NullPointerException like this:

if (keyChanged != null){   
  char keyChar = keyChanged.charAt(0); 
}
Andrea Dusza
  • 2,080
  • 3
  • 18
  • 28
0

Solved it, It was pretty stupid on my part, I just set the original keyChanged variable as a character instead.

Muhammad Shahab
  • 119
  • 1
  • 1
  • 4