Java为什么会出现此错误?线程“main”java.lang.NullPointerException中的异常

时间:2014-09-20 01:55:27

标签: java

这是使用>>>>>>>>行给我错误的代码我已经看过这个帖子Exceptions但是我仍然不明白我需要改变什么:(我是编程的初学者。

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

3 个答案:

答案 0 :(得分:1)

按照逻辑,您将String word初始化为null,然后继续致电word.length()

wordnull,没有length()。因此,NullPointerException

在尝试测量长度之前,将字符串分配给word

String word = "Hello!";

答案 1 :(得分:1)

变量 word 似乎是null,我想你跳过了一条指令,用一个从 fileIn 读取的单词填充它。您的代码应该是这样的:

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

答案 2 :(得分:1)

您将word定义为null。当你说word.length()时,就意味着你在说  null.length()这就是你得到空指针异常的原因。

您应该在执行任何操作之前初始化String变量“word”(使用'。'调用任何字符串方法)

如果您有任何预定义值,请使用其初始化,使用空字符串初始化。

String word =“xyz”; String word =“”;