I am trying to count the number of vowels in the string. The problem is that it compiles correctly, but when I run it the command window says "Exception in thread 'main' java.lang.NullPointerException at CountVowels.main(CountVowels.java:25).
import javax.swing.JOptionPane;
public class CountVowels
{
public static void main(String[] args)
{
String thingy = JOptionPane.showInputDialog("Please enter a sentence, and I will count the vowels.");
int count = 0;
char[] chars = new char[thingy.length()];
String[] letters = new String[thingy.length()];
for(int i = 0; i < thingy.length(); ++i)
{
chars[i] = thingy.charAt(i);
}
for(int i = 0; i < letters.length; ++i)
{
letters[i].valueOf(chars[i]);
}
for(int i = 0; i < chars.length; ++i)
{
if(letters[i].equals("a") || letters[i].equals("e") || letters[i].equals("i") || letters[i].equals("o") || letters[i].equals("u"))
{
++count;
}
}
System.out.println("There are " + count + " vowels in the string + " + thingy);
}
}
What causes this, how can I fix it, and how can I prevent it from happening again in the future?
line 25 is my if statement:
if(letters[i].equals("a") || letters[i].equals("e") || letters[i].equals("i") || letters[i].equals("o") || letters[i].equals("u"))
{
++count;
}