java - StringReader 从 String.Split 输出中给出错误

标签 java string split stringreader

我在使用 StringReader 时遇到异常。我在创建对象时解析的字符串是通过 String.split 生成的,它给了我 NullPointerException。任何建议如何解决这个问题?

代码如下:

public static void main(String[] args) throws IOException {
    // TODO code application logic here
    int jmldoc = 5;
    Hashmap hashsentences[][] = new Hashmap[5][100];
    Docreader reader = new Docreader();
    System.out.println(reader.doc[1]);

    for (int i = 0; i < jmldoc; i++) {
        int j = 0;
        while (reader.sentences[i][j] != null) {
            System.out.println(reader.sentences[i][j]);
            j++;              
            String temp=reader.sentences[i][j];
            StringReader h = new StringReader(temp);

        }
    }


}

和文档生成器类

    public class Docreader {

    public String sentences[][]=new String[5][100];
    public String doc[]=new String[5];

    public Docreader() throws IOException{

    this.readdoc();
    this.splittosentence();

    }

   public void readdoc() throws IOException {
        for (int i = 0; i < 5; i++) {
            String temp = new String();
            temp = Docreader.readFile("datatrain/doc" + (i + 1) + ".txt");
            this.doc[i] = temp;
        }

    }

    public void splittosentence() {


        for (int i = 0; i < 5; i++) {
            String temp[];
            temp = doc[i].split("\\.");
            for(int j=0;j<temp.length;j++){
            sentences[i][j]=temp[j];

            }

        }

    }

    private static String readFile(String fileName) throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append("\n");
                line = br.readLine();
            }
            return sb.toString();
        }
    }
}

异常:

Exception in thread "main" java.lang.NullPointerException
at java.io.StringReader.<init>(StringReader.java:50)
at stki_d_8_final.STKI_D_8_Final.main(STKI_D_8_Final.java:45)

Java 结果:1

当我检查 StringReader 类中的第 50 行时,它包含

this.length = s.length();

最佳答案

这部分代码:

while (reader.sentences[i][j] != null) {
    System.out.println(reader.sentences[i][j]);
    j++;              
    String temp=reader.sentences[i][j];
    StringReader h = new StringReader(temp);
}

你正在使用 j++ 所以 j 的值增加 1,然后你有这个代码:

String temp=reader.sentences[i][j];

由于尚未评估数组中的这个新条目是否与 null 不同,它可能包含一个 null 值,该值分配给 temp 并因此使用 null 值作为参数初始化 StringReader

解决它的方法是在使用它构建StringReader 之后增加j。此外,在当前形式下,如果 reader.sentences[i] 的所有值都不为 null,此代码也可能抛出 ArrayIndexOutOfBoundsException。这将是上面代码的解决方案:

while (j < reader.sentences[i].length && reader.sentences[i][j] != null) {
    System.out.println(reader.sentences[i][j]);
    String temp=reader.sentences[i][j];
    StringReader h = new StringReader(temp);
    j++;
}

关于java - StringReader 从 String.Split 输出中给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27772856/

相关文章:

java - 将 System.err.println 的输出重定向到文件而不更改代码

java - 如何将字符添加到特定索引中的字符串?

c# - 无法通过 1 个测试用例的同构字符串检查的简单解决方案

python - 使用特定字符在 Python 中拆分字符串

java - 骰子滚动循环

java - 另一个 java 通用问题

java - 如何将日期转换为int数组?

clojure:根据值的序列划分序列

Java 到 XSD 或 XSD 到 Java

ios - 对于 NSDate 变量来说,字符串插值是不好的做法吗?