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
    }
}
6
  • 1
    See stackoverflow.com/questions/218384/… ; 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. Commented Sep 20, 2014 at 1: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. Commented Sep 20, 2014 at 2: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 :( Commented Sep 20, 2014 at 2: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 Commented Sep 20, 2014 at 2:09
  • Hey are you in the same class as the person in this question? stackoverflow.com/questions/25858770/…. I gave a full code response, maybe take a look at it. Edit: upon looking further (esp the comments) you are using my code Commented Sep 20, 2014 at 4:14

3 Answers 3

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!";
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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
Assign something to it before you enter your for loop?
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++) {
               ...

Comments

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 = "" ;

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.