Java 读取txt文件时出现NullPointerException

Java 读取txt文件时出现NullPointerException,java,debugging,nullpointerexception,Java,Debugging,Nullpointerexception,下面是我编写的一个程序的一部分,该程序遍历一个文件,查找并比较两个不同的数字 它在每一行中搜索该行等于[Device | Sensors | Checksum]的起始点。然后它查找第一次和第二次出现的值,获取该行上的十六进制数,将其转换为long类型。如果这两个长数字相等,我们就好了,否则我们就报告一个问题 这是我正在使用的文件的摘录 Address(18) = 0x0053 Page(18) = Sensor Value(19) = 0x1 Desc(19) = used bytes from

下面是我编写的一个程序的一部分,该程序遍历一个文件,查找并比较两个不同的数字

它在每一行中搜索该行等于[Device | Sensors | Checksum]的起始点。然后它查找第一次和第二次出现的值,获取该行上的十六进制数,将其转换为long类型。如果这两个长数字相等,我们就好了,否则我们就报告一个问题

这是我正在使用的文件的摘录

Address(18) = 0x0053
Page(18) = Sensor
Value(19) = 0x1
Desc(19) = used bytes from high priority task
Address(19) = 0x0053
Byte_No(19) = 2
Page(19) = Sensor
;end of section [Device|Sensors|Stack]
[Device|Sensors|Checksum]
Address(18) = 0x0053
Page(18) = Sensor
Value(2) = 0xE0A64F36
Address(18) = 0x0053
Page(18) = Sensor
Default(19) = 0x00
Value(3) = 0xE0A64F36
Page(18) = Sensor
Desc(19) = used bytes from high priority task
;end of section [Device|Sensors|Checksum]
尽管我已经试着调试它,但我找不到这段代码到底出了什么问题。我用过的环看起来很好

try {

 do {//read line then check if line is "[Device|Sensors|Checksum]" WHILE line isn't ";end of section [Device|Sensors|Checksum]"                    

  line = reader.readLine();//Reads all lines up to "[Device|Sensors|Checksum]"

  if (line.equals("[Device|Sensors|Checksum]")) { //IF line is "[Device|Sensors|Checksum]" then  
                                                  //(check if line is "Value") ELSE read next line                    
   do {// DO read line and (check if line is "Value") WHILE line doesn't contain "Value"

     line = reader.readLine();//Reads all line up to first "Value"

     if (line.contains("Value")) { // IF line is "Value" then retrieve necessary value
                                   // ELSE read next line

      Hex1 = line.split("=")[1].trim();
      l1 = Long.parseLong(Hex1.substring(2), 16);
      JFrame frame1 = new JFrame("JOptionPane showMessageDialog example");
      JOptionPane.showMessageDialog(frame1, "Value has hex number\n " + Hex1 + "\n\n and integer\n" + l1 + "\n");

      System.out.println("Here's the line read  " + line);
      System.out.println("Here's the hex number from the line  " + Hex1);
      System.out.println("Here's the integer from the hex number  " + l1 + "\n");

      break;
     }
    } while (!"Value".contains(line = reader.readLine()));//while2 


   line = reader.readLine();  //Reads the line AFTER the first "Value" has been found

    do {//  Read line and check if line has "Value" WHILE line doesn't contain "Value"

     line = reader.readLine();//Reads all lines up to second "Value"

     if (line.contains("Value")) { // IF line is "Value" then retrieve necessary value
                                   // ELSE read next line

      Hex2 = line.split("=")[1].trim();
      l2 = Long.parseLong(Hex2.substring(2), 16);
      JFrame frame2 = new JFrame("JOptionPane showMessageDialog example");
      JOptionPane.showMessageDialog(frame2, "Value has hex number\n " + Hex2 + "\n\n and integer\n" + l2 + "\n");

      System.out.println("Here's the line read  " + line);
      System.out.println("Here's the hex number from the line  " + Hex2);
      System.out.println("Here's the integer from the hex number  " + l2 + "\n");

      break;

     }
    } while (!"Value".contains(line = reader.readLine()));
   }
  } while (!";end of section [Device|Sensors|Checksum]".equals(line = reader.readLine())); 
 if (l1 == l2) {
   JOptionPane.showMessageDialog(null, "Both checksum values ARE equal!");
   int status1 = 0;
   System.exit(status1);
 } else {
    JOptionPane.showMessageDialog(null, "Both checksum values NOT equal");
    int status2 = 0;
    System.exit(status2);
  }

} catch (IOException e) {
   System.out.println("IO Exception. Could not read file!");
}
编辑

NPE的输出和堆栈跟踪为

run:
Here's the line read  Value(2) = 0xE0A64F36
Here's the hex number from the line  0xE0A64F36
Here's the integer from the hex number  3768995638

Exception in thread "main" java.lang.NullPointerException
at robertskostalproject.checksumFinder.HexFinder(checksumFinder.java:32)
at robertskostalproject.checksumGUI.askDirectory(checksumGUI.java:43)
at robertskostalproject.RobertsKostalProject.main(RobertsKostalProject.java:18)
BUILD STOPPED (total time: 11 seconds)

由于您尚未指定在第二个while循环中的何处获得NPE,因此我仅引用:

返回:包含行内容的字符串,不包括任何行终止字符,如果已到达流的结尾,则返回null


此外,您使用的是一个frame2变量,我们还没有看到它被初始化。

以下是我现在使用的。谢天谢地,没有太多的变化,而且很有效

class checksumFinder {

public static long l1 = 1;
public static long l2 = 0;
//Default to not equal

String HexFinder(JFileChooser inFileName, String line, String Hex1, String Hex2)
throws FileNotFoundException, IOException {

    File file = inFileName.getSelectedFile();
    BufferedReader reader = new BufferedReader(new FileReader(file));

    try {

      line = reader.readLine();//Read all lines up to "[Device|Sensors|Checksum]"

      do {//Read line then check if line is "[Device|Sensors|Checksum]" WHILE isn't ";end of section [Device|Sensors|Checksum]"                    

        line = reader.readLine();

        if (line.equals("[Device|Sensors|Checksum]")) { //IF line is "[Device|Sensors|Checksum]" then  
                                                        //(check if line is "Value") ELSE read next line                    
          do {// DO read line and check line is "Value" WHILE line doesn't contain "Value"

            if (line.contains("Value")) { //IF line is "Value" then retrieve necessary value
                                          // ELSE read next line

              Hex1 = line.split("=")[1].trim();
              l1 = Long.parseLong(Hex1.substring(2), 16);
              JFrame frame1 = new JFrame("JOptionPane showMessageDialog example");
              JOptionPane.showMessageDialog(frame1, "Value has hex number\n " + Hex1 + "\n\n and integer\n" + l1 + "\n");

              System.out.println("Here's the line read  " + line);
              System.out.println("Here's the hex number from the line  " + Hex1);
              System.out.println("Here's the integer from the hex number  " + l1 + "\n");

              break;
            }
          } while (!"Value".contains(line = reader.readLine()));//while2   

          line = reader.readLine();  //Read the line AFTER first "Value" has been found

          do {//Read line and check if line has "Value" WHILE line doesn't contain "Value"

           if (line.contains("Value")) { // IF line is "Value" then retrieve necessary value
                                        // ELSE read next line

             Hex2 = line.split("=")[1].trim();
             l2 = Long.parseLong(Hex2.substring(2), 16);
             JFrame frame2 = new JFrame("JOptionPane showMessageDialog example");
             JOptionPane.showMessageDialog(frame2, "Value has hex number\n " + Hex2 + "\n\n and integer\n" + l2 + "\n");

             System.out.println("Here's the line read  " + line);
             System.out.println("Here's the hex number from the line  " + Hex2);
             System.out.println("Here's the integer from the hex number  " + l2 + "\n");

             break;
           }
         } while (!"Value".contains(line = reader.readLine()));//while3

         while (!";end of section [Device|Sensors|Checksum]".equals(line)){
            line = reader.readLine();
         }
       }
     } while (!";end of section [Device|Sensors|Checksum]".equals(line)); //while1

     if (l1 == l2) {
       JOptionPane.showMessageDialog(null, "Both checksum values ARE equal!");
       int status1 = 0;
       System.exit(status1);
     } else {
       JOptionPane.showMessageDialog(null, "Both checksum values NOT equal");
       int status2 = 0;
       System.exit(status2);
     }

 } catch (IOException e) {
      System.out.println("IO Exception. Could not read file!");
 }
   return null;
}

}

你真的想直接跳过[Device | Sensosrs | Checksum]后面的那一行吗?此外,代码缩进严重也无济于事,您没有告诉我们异常是在哪里发生的,文件是什么样子的,或者在不调试时会发生什么。请阅读“请不要宣誓”,即使是标题中使用的非常温和的宣誓。和值。containsvariable?始终发布异常的完整和准确堆栈跟踪。供将来参考,如果您发布了关于NullPointerException的帖子,请告诉我们NPE是在哪一行抛出的,而不仅仅是在这段代码+1中-readLine中的null最有可能是OP NPE问题的原因。即使实际原因是其他原因,这也是一个潜在原因。。。取决于输入流中的内容。