【问题标题】:StringReader gives error from String.Split outputStringReader 从 String.Split 输出中给出错误
【发布时间】:2015-01-05 03:00:30
【问题描述】:

使用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);

        }
    }


}

和 docreader 类

    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();

【问题讨论】:

  • 使用完整的堆栈跟踪发布您的异常。
  • @PM77-1 我不知道该怎么做
  • 是的,您确实知道,因为您已经这样做了。

标签: java string split stringreader


【解决方案1】:

在这部分代码中:

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,因此用@987654329 初始化StringReader @value 作为参数。

解决它的一种方法是在使用它构建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++;
}

【讨论】:

  • 但是有一个问题,我们不检查 reader.sentences[i][j] != null 元素是否为 null 吗?那么为什么我们需要再次重新检查?
  • @KickButtowski 假设ij 的值都为0。 while 条件检查while (reader.sentences[0][0] != null) 并通过此测试,然后j++ 执行,temp 的初始化将是String temp = reader.sentences[0][1],之前没有评估过,并且可能包含null 值。此外,在当前形式下,此代码也可能抛出 ArrayIndexOutOfBoundsException。这里最简单的解决方案是使用另一个for 循环来控制j 的值,而不是while
【解决方案2】:

在这部分代码中:

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

您适当地确保 reader.sentences[i][j] != null,然后您增加 j (j++) 并进行空检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 2022-01-20
    • 2021-02-11
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多