简体   繁体   中英

Java Why do I get this error? Exception in thread “main” java.lang.NullPointerException

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

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

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++) {
               ...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM