Beninemm
Beninemm

Reputation: 33

How do I fix Null Pointer Exception?

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;
                }

Upvotes: 3

Views: 212

Answers (5)

Benjamin
Benjamin

Reputation: 2286

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

Upvotes: 1

The Guy with The Hat
The Guy with The Hat

Reputation: 11132

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);
    }
}

Upvotes: 3

AJJ
AJJ

Reputation: 3628

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]);

Upvotes: 0

jlewkovich
jlewkovich

Reputation: 2775

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

Upvotes: 1

PetrS
PetrS

Reputation: 1110

Array letters is empty. It contains null values.

Upvotes: 0

Related Questions