java - 解决 Java 中的 NullPointerException 问题 - 认为这是一个初始化问题,但找不到它

标签 java nullpointerexception

我仔细研究了许多其他的 NPE 解决方案,并且尝试实现其他建议,但没有一个与我想要做的完全匹配,而且只会导致更多的 eclipse 错误。我已经编译并尝试从命令行运行,在命令行运行时为我正在运行的应用程序提供了几个字符串。下面是主类,以及包含主类正在使用的方法的类。

具有主要方法的类:

package my.package.ext;



public class WordCounterApp {

    /**
     * @param args
     * Two command line arguments: the first one needs to be in quotes, the string that will be used, the second optional argument
     * is the unique word to be counted (countWord method).
     * @param source 
     * @param word 
     */
    public static void main(String[] args) {
        String source = null;
        String uniqueword = null;
        StringBuilder word = null;
        WordCounter counter = new WordCounter(source, word);
        WordCounter uniqueCounter = new WordCounter(source, uniqueword);
        counter.countWords(source);
        counter.countUniqueWords(source);
        uniqueCounter.countWord(source, uniqueword);

}

}

使用其他方法进行类:

package my.package.ext;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.lang.Character;
import java.lang.StringBuilder;

public class WordCounter {
    public Integer counter = 0;
    public String source;
    public HashSet<String> hashset;
    public StringBuilder word;
    public String uniqueword;

    public WordCounter(String source) {
         counter = new Integer(counter);
    }
    public WordCounter(String source, StringBuilder word) {
         counter = new Integer(counter);
    }
    public WordCounter(String source, String uniqueword) {
        counter = new Integer(counter);
    }
    /**
     *  
     * @param line - the string parameter to get a total word count from.
     */

    public int countWords(String source) {

        boolean word = false;
        int endOfLine = source.length() - 1;
        Integer counter = 0;

        for (int i = 0; i < source.length(); i++) {
            if (Character.isLetter(source.charAt(i)) == true && i != endOfLine) {
                word = true;
            //} else if (Character.charValue(line.charAt(i)) == "-" && i != endOfLine) {
            //  word = true;
            } else if (Character.isLetter(source.charAt(i)) == false && word == true) {
                counter++;
                word = false;
            } else if (Character.isLetter(source.charAt(i)) && i == endOfLine) {
                counter++;
            }
        }
        System.out.println(counter);
        return counter;
    }




/**
 * 
 * @param line - the string parameter that we will return the unique word count from. Randy recommends a HashSet.
 * Put it into a hashset. Hashsets don't allow duplicate elements. Then do a count. 
 */

    public int countUniqueWords(String line) {
        hashset = new HashSet<String>();
        word = new StringBuilder();
        int endOfLine = line.length() - 1;
        boolean isWord = false;
        String stringWord = null;
        Integer counter = 0;

        for (int i = 0; i < line.length(); i++) {
            if (Character.isLetter(line.charAt(i)) == true && i != endOfLine) {
                //System.out.println(i);
                word.append(line.charAt(i));
                isWord = true;
            } else if (Character.isLetter(line.charAt(i)) == false && isWord == true) {
                counter++;
                //System.out.println("Counter is: " + counter);
                stringWord = word.toString();
                //System.out.println("stringWord is now: " + stringWord);
                hashset.add(stringWord);
                //System.out.println(hashset);
                word = new StringBuilder();
                isWord = false;
            } else if (Character.isLetter(line.charAt(i)) && i == endOfLine) {
                counter++;
                stringWord = word.toString();
                hashset.add(stringWord);
            }
        }
        //System.out.println(counter);
        System.out.println("There are " + hashset.size() + " unique words in this string");
        System.out.println("These are the unique words in the string: " + hashset);
        return counter;

    }


/**
 * 
 * @param source - the string the word is to be counted from
 * @param word - the word to be counted
 * 
 */
    public void countWord(String source, String word) {

        String str = source;
        Pattern p = Pattern.compile("\\s"+word+"\\s");
        Matcher m = p.matcher(str);
        int count = 0;
        while (m.find()) {
            count++;
        }
        System.out.println("The word: " + "\"" + word + "\"" + " appears " + count + " times.");
        }

}

我已在此处确定了 NPE 的来源:

public int countWords(String source) {

    boolean word = false;
    int endOfLine = source.length() - 1;  //the source of the NPE is this line
    Integer counter = 0;

因此,我认为我没有正确初始化源。我尝试过类似的事情 WordCounter 源 = new WordCounter()

但是我尝试的每个变体,引入正确的构造函数,都会给我带来其他 eclipse 错误。我似乎无法到达那里,而且我担心我走错了路。我这里可能还有其他搞砸的事情。我也不确定从命令行运行的正确方法,同时传递一些字符串作为参数来提供方法。提前致谢

最佳答案

主方法中的source字符串为null,并且您将其作为参数传递给countWords方法。

 public static void main(String[] args) {
        String source = null;// it is null here
        ..............
        ............
        counter.countWords(source);// passing null here

因此,当您调用时,您的countWords 中就会出现这样的情况

    int endOfLine = source.length() - 1;

由于您的源为 null,它将抛出 NullPointerException。

初始化字符串以消除 NPE。

编辑:如果您想将源代码作为命令行参数传递。

String source =args[0];

并在运行时传递命令行参数。

关于java - 解决 Java 中的 NullPointerException 问题 - 认为这是一个初始化问题,但找不到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13242078/

相关文章:

java - 编写空检查的更简洁的方法(可能没有 IF?)

java - 局部变量变为空(Android)

java - 预订应用程序中的 NullPointerException

java - Android fragment NullPointerException 错误

java简单的画图程序鼠标监听器

java - 尝试替换场景内容时 JavaFx 中的指针异常

Java 8 - 如何将数组列表转换为特定类对象的列表

java - 使用正则表达式匹配单词 '90%'

java - HTTPS 登录? java

java - 使用 HttpClient 获取请求属性