0

This is the code that it is giving me the error for on the line with >>>>>>> I've already looked at this thread Exceptions but I still do not understand what I need to change :( I am a total beginner to programming.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class manyVowels {

  public static final String wordList = "words.txt";

  public static void main(String[] args) {
        Scanner fileIn = null;
        try {
            //locate and open file
            fileIn = new Scanner(new FileInputStream("words.txt"));
        } catch (FileNotFoundException e) {
            //if the file cannot be found, the program prints the message and quits
            System.out.println("File not found. ");
            System.exit(0);
        }
        String word = null;
        if (fileIn.hasNext()) //if there is another word continue
        {

            String finalWord = null; // defines the word with most consecutive vowels
            int maxVowels = 0;//sets initial value to 0
            while (fileIn.hasNext()) {
                // for each word in the file
                int vowels = 0;
/*error here-->*/ for (int i = 0; i < word.length() && i < word.length() - maxVowels + vowels; i++) {
                    // for each character in the word, and exit early if the word is not long enough to beat maxVowels
                    if (hasVowels(word.charAt(i))) {
                        // consonants reset this to 0
                        vowels++;
                    } else {
                        // reached the end of the word so check if the count is higher than maxVowels
                        if (vowels > maxVowels) {
                            maxVowels = vowels;
                            finalWord = word;
                        }
                        vowels = 0;
                    }
                }
                // comparing vowels to maxVowels
                if (vowels > maxVowels) {
                    maxVowels = vowels;
                    finalWord = word;
                }
            }

            //seek vowels
            word = fileIn.next();
            for (int i = 0; i < word.length(); i++) {
                if
                        ((word.charAt(i) == 'A')
                        || (word.charAt(i) == 'E')
                        || (word.charAt(i) == 'I')
                        || (word.charAt(i) == 'O')
                        || (word.charAt(i) == 'U')
                        || (word.charAt(i) == 'a')
                        || (word.charAt(i) == 'e')
                        || (word.charAt(i) == 'i')
                        || (word.charAt(i) == 'o')
                        || (word.charAt(i) == 'u')) {
                    //prints the final word with the most consecutive vowels
                    System.out.println("The word with the most consecutive vowels is: " + word);
                    System.exit(0);
                }

            }
        }
    }

    private static boolean hasVowels(char charAt) {
        throw new UnsupportedOperationException("Inserted by template."); //NetBeans generated method
    }
}
Community
  • 1
  • 1
spoilmealwayz
  • 69
  • 1
  • 8
  • 1
    See http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it ; you have String word = null; and then later: word.length() - which means you are calling the length() method on a variable that has a null value. – Brad Peabody Sep 20 '14 at 01:59
  • When you get the exception there is a stack trace that tells you exactly where the exception is. That statement has a null pointer somewhere in it. This is an incredibly easy problem to debug. – Hot Licks Sep 20 '14 at 02:07
  • thank you. I've looked at this already, but since it was a little different from what I am trying to accomplish (reading words from a file) I am finding it difficult to directly apply the corrections to my file. I am confused :( – spoilmealwayz Sep 20 '14 at 02:07
  • @HotLicks it showed me where the problem lies. It is because I asked for length after setting the word value to null. the problem is, I need the file to read words from a text file, so I'm not sure what value to put on "word." I just tried "" but the program does not stop running – spoilmealwayz Sep 20 '14 at 02:09
  • Hey are you in the same class as the person in this question? http://stackoverflow.com/questions/25858770/finding-a-word-with-the-most-consecutive-vowels-in-a-text-file-using-java/25859256#25859256. I gave a full code response, maybe take a look at it. Edit: upon looking further (esp the comments) you are using my code – nmore Sep 20 '14 at 04:14
  • @nmore very likely, but I am not sure. Thank you for the assistance. I really want to try to come up with your solution on my own so I understand the process. It helps to see the final product though, because then I can tweak mine to have the right methods once I understand how they are used in your solution. – spoilmealwayz Sep 21 '14 at 18:48

3 Answers3

1

Following the logic, you initialize the String word to null, and then proceed to call word.length().

word, being null, has no length(). Thus, the NullPointerException.

Assign a string to word before attempting to measure its length.

String word = "Hello!";
slezica
  • 63,258
  • 20
  • 91
  • 152
  • Now i understand...the reason why I set it to null was because it will be reading a list of words from a file, so I could not assign it a specific word :-/ Instead, I took out null and made it into "" but the program is still running...not sure. – spoilmealwayz Sep 20 '14 at 02:05
  • You should rethink the flow so that by the time `word.length()` is called, `word` already has a meaningful value. Read from the file first, ask questions later – slezica Sep 20 '14 at 02:08
  • Assign something to it before you enter your for loop? – MarGar Sep 20 '14 at 02:08
1

The variable word seems to be null, I think you skipped an instruction in which you fill it with a word read from fileIn. Your code should read something like this:

     while (fileIn.hasNext()) {
            // for each word in the file
            word = fileIn.next();
            int vowels = 0;
            for (int i = 0; i < word.length() && i < word.length() - maxVowels + vowels; i++) {
               ...
1

you defined word as a null. When you say word.length() it means you are saying null.length() that's why you are getting null pointer exception.

You should initialize String variable "word" before doing any operation( calling any string method by using '.' )

If you have any predefined value, initialize with that else initialize with empty string.

String word = "xyz" ; String word = "" ;

Moni
  • 433
  • 3
  • 9