编程语言
首页 > 编程语言> > java – StringReader从String.Split输出中给出错误

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

作者:互联网

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

解决方法:

在这部分代码中:

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不同,因此它可能包含一个空值,该值被赋值给temp,从而初始化StringReader,并将null值作为参数.

解决它的一种方法是在使用它构建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,string,split,stringreader
来源: https://codeday.me/bug/20190728/1560220.html