Java 在读取文件中的下一行和前一行时,我得到一个空指针异常

Java 在读取文件中的下一行和前一行时,我得到一个空指针异常,java,nullpointerexception,java.util.scanner,file-read,Java,Nullpointerexception,Java.util.scanner,File Read,我知道在逐行读取文件时读取下一行和上一行的方法就像 String prev = null; String curr = null; String next = null; Scanner sc = new Scanner(new File("thefile.txt")); while(sc.hasNextLine()) { prev = curr; curr = next; next = sc.nextLine(); 但是在第一次迭代中,我得到了一个空指针异常,因为

我知道在逐行读取文件时读取下一行和上一行的方法就像

String prev = null;
String curr = null;
String next = null;

Scanner sc = new Scanner(new File("thefile.txt"));

while(sc.hasNextLine()) {
    prev = curr;
    curr = next;
    next = sc.nextLine();
但是在第一次迭代中,我得到了一个空指针异常,因为我必须处理当前元素

这有什么解决办法

    Scanner sc = new Scanner(file);

    String curr = null ;
    String next = null ;


      while (sc.hasNextLine()) {

            curr = next;
            next =  sc.nextLine();
            if(curr.length() > 0 && !curr.equals(". O O")) {
                lines+= curr.substring(0, curr.indexOf(" ")) + " ";

                for(String pat : patStrings)
                {
                // System.out.println(pat) ;
                    Pattern minExp = Pattern.compile (pat);
                    Matcher minM = minExp.matcher(curr);
                    while(minM.find())
                    {
                        String nerType = minM.group(2);
                        if(nerType.contains("B-NE_ORG")){
                            if (next.contains("I-NE_ORG"))
                                orgName+= minM.group(1)+" ";


当您进入while循环时,如果文件有两行或更多行,则可以根据您的代码开始处理。

我不确定这是否有帮助,但需要交换curr=next的位置;和next=sc.nextLine; 您的解决方案:

curr = next;
next =  sc.nextLine();
我的建议是:

next =  sc.nextLine();
curr = next;

您很可能需要先阅读两行,才能正确设置变量

String prev = null;
String curr = null;

Scanner sc = new Scanner(new File("thefile.csv"));

if (sc.hasNextLine()) {
    curr = sc.nextLine();
}

while (sc.hasNextLine()) {
    prev = curr; 
    curr = sc.nextLine();
}
如果您一次需要三行,那么您应该实际阅读三行,然后处理它们

StringBuilder sb = new StringBuilder();
while (sc.hasNextLine()) {
    sb.setLength(0); // clear the stringbuilder

    String line = sc.nextLine(); 
    if (line.isEmpty()) continue; // if you want to skip blank lines
    else sb.append(line).append("\n");

    for (int i = 1; i < 3 && sc.hasNextLine(); i++) {
        line = sc.nextLine(); 
        sb.append(line).append("\n");
    }
    String[] lines = sb.toString().trim().split("\n");
    if (lines.length == 3) {
        String prev2 = lines[0];
        String prev1 = lines[1];
        String curr = lines[2];

        doStringThings(prev2, prev1, curr);
    }
}

开始时,curr和next都为null。在第一次迭代中,指定curr=next,因此curr仍然为null。然后curr.length抛出nullpointer异常…感谢您能理解我到底想要什么@意外发现,快乐编码!:那咖喱永远是下一个。谢谢!我可以做两行,因为我需要检查当前和未来的元素。在您的代码中有当前和以前的。我可以把它们换成当前和下一行,对吗?你必须读另外一行才能得到上一行、当前行和下一行。我认为我的第二个解决方案会更好。尽管您需要使用代码的第二个版本对行执行长度检查,但在声明字符串生成器的方式上做了一些微小的修改。但使用此选项,我的文件将从第3行开始读取,而不是从第1行开始读取。而sc.hasNextLine{StringBuilder sb=new StringBuilder;for int i=0;i<3&&sc.hasNextLine;i++{ifsc.nextLine.length>0&&sc.nextLine!=null sb.appendsc.nextLine.append\n;}String[]elements=sb.toString.split\n;prev=elements[0];curr=elements[1];next=elements[2];sc.nextLine不能为null,因为if语句。我认为它不能为空?如果它确实是CSVI,至少你的CSV不应该有空行。我没有读取CSV文件。它是一个.txt排序,中间有空行。
StringBuilder sb = new StringBuilder();
while (sc.hasNextLine()) {
    sb.setLength(0); // clear the stringbuilder

    String line = sc.nextLine(); 
    if (line.isEmpty()) continue; // if you want to skip blank lines
    else sb.append(line).append("\n");

    for (int i = 1; i < 3 && sc.hasNextLine(); i++) {
        line = sc.nextLine(); 
        sb.append(line).append("\n");
    }
    String[] lines = sb.toString().trim().split("\n");
    if (lines.length == 3) {
        String prev2 = lines[0];
        String prev1 = lines[1];
        String curr = lines[2];

        doStringThings(prev2, prev1, curr);
    }
}