3

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;
                }
2
  • 1
    And which is line 25 ? Commented Apr 1, 2014 at 15:26
  • This line isn't doing what you think it is: letters[i].valueOf(chars[i]); Commented Apr 1, 2014 at 15:31

5 Answers 5

3

The line

letters[i].valueOf(chars[i]);

does nothing. If you change it to

letters[i] = String.valueOf(chars[i]);

you will set all the elements in letters to the corresponding characters. That should fix your NullPointerException.

You can also save a lot of code by using a bit of regex trickery:

import javax.swing.JOptionPane;

public class CountVowels
{
    public static void main(String[] args)
    {
        String input = JOptionPane.showInputDialog("Please enter a sentence, and I will count the vowels.");
        String[] letters = input.split("(?<=.)(?=.)");

        int count = 0;

        for(int i = 0; i < letters.length; i++)
        {
            if(letters[i].matches("[aeiou]"))
            {
                count++;
            }
        }
        System.out.println("There are " + count + " vowels in the string + " + input);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try using this instead:

letters[i] = "" + chars[i];

Also, you don't need to go through all the hassle of creating new arrays going from String to chars to String. Read this question for more information about accessing each character from a String (so you can just check the letters in the thingy object without creating more arrays)

Get string character by index - Java

1 Comment

Thanks for the info, this will definitely save me some time
1

Try This :

      import javax.swing.JOptionPane;
      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[] c=thingy.toCharArray();
     for(int i = 0; i < c.length; ++i)
     {
        if(c[i]=='a' || c[i]=='e' || c[i]=='i' || c[i]=='o' || c[i]=='u')
        {
            ++count;
         }
      }
    System.out.println("There are " + count + " vowels in the string + " + thingy);
   }
 }

output : enter image description here

Comments

0

Array letters is empty. It contains null values.

3 Comments

for(int i = 0; i < letters.length; ++i) { letters[i].valueOf(chars[i]); } Didn't this assign the values?
@Beninemm No, it didn't.
No. You can check it in debug mode.
0

There is a mis in your code. When you manipulate with String, the return string should be taken for furthur processing.. See docs

letters[i] = String.valueOf(chars[i]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.