0

我正在运行一个连接到设备的程序,该设备又向我发送数据。空指针异常的奇怪情况

数据以字符串形式出现,然后相应地处理它以尝试使用预准备语句将其存储到我的数据库中。

我收到一个空指针异常,似乎来自准备的语句。我认为这是问题,因为当我评论代码时异常消失。也许我不明白准备好的陈述,以及我认为我做的。有什么建议吗?

这里是我的大部分代码:

String[] dataIn = new String[100]; 
int i, channel, state, theStamp, temp, temp2; 
char first, second; 
String[] toBinary = new String[100]; 
StringBuilder sb = new StringBuilder(40); 

static String hexToBin(String s) { 
    return new BigInteger(s, 16).toString(2); 
} 

public void run() { 
    // Run as long as the thread is alive 
    while (this.threadAlive) { 
     try { 
     // Attempt to connect to the device. 
     if (super.connectToDevice()) { 
      // Send the data command to the device. If it is a test device, send the TIME command, otherwise send the DATA command 
      if (this.testDevice) 
      out.println(COMMAND_TIME); 
      else 
      out.println(COMMAND_DATA); 

      // Read and store the data returned from the device 
      String data = in .readLine(); 



      //******// 
      System.out.println("The data: " + data); 

      //Splitting data into array 
      String criteria = " "; 
      if (data != null) { 
      dataIn = data.split(criteria); 
      toBinary[0] = hexToBin(dataIn[0]); 
      System.out.println("This is dataIn[0] before binary " + dataIn[0]); //testing 
      System.out.println("This is toBinary[0] " + toBinary[0]); //testing 
      temp = 0; 
      temp2 = 1; 
      for (i = 1; i < dataIn.length; i++) { 
       if (i % 2 == 0) { 
       toBinary[i/2] = hexToBin(dataIn[i]); 
       System.out.println("Binary in String: " + toBinary[i/2]); 
       try { 
        PreparedStatement ps = this.db.getConnection().prepareStatement(PreparedStatementStatics.INSERT_C1_DATA); 

        for (int j = 3; j < 27; j++) { 
        first = toBinary[temp].charAt(j); 
        second = toBinary[temp2].charAt(j); 

        System.out.println("Checkpoint first/second achieved."); 
        System.out.println("Temp = " + temp); 
        System.out.println("Temp2 = " + temp2); 

        if (first == second) { 
         sb.append(0); 
        } else { 
         sb.append(1); 
        } 
        //System.out.print("sb appends: " + sb); 
        } 
        temp++; 
        temp2++; 
        System.out.println("sb turns out to be " + sb); 
        channel = sb.indexOf("1", 0); 
        state = toBinary[0].charAt(channel); 
        System.out.println("Change occurred in channel " + channel); 
        sb.setLength(0); 

        ps.setInt(1, channel); 
        ps.setInt(2, state); 


        ps.addBatch(); 

        ps.executeBatch(); 
        ps.close(); 
        ps = null; 

       } catch (NullPointerException ex) { 

        System.out.println("Null Pointer Exception: Not enough data this time through"); 
       } catch (StringIndexOutOfBoundsException ex) { 
        System.out.println("Out of Bounds Exception"); 
       } catch (SQLException e) { 
        System.out.println("Something is wrong with SQL"); 
        e.printStackTrace(); 
       } 
       } else { 
       System.out.println("This should be a timestamp: " + dataIn[i]); 
       } 
      } 
      } 

UPDATE

这里是我的控制台日志,当我运行代码:

The data: 5ABFE000 2556923 52BFE000 2556b6a 5ABFE000 2556dfb 4ABFE000 2556f8e 4AB7E000 25570da 4ABFE000 2557225 4ABDE000 255739e 4ABFE000 255765a 4ABFA000 2557844 4ABF2000 2557959 4ABFA000 2557b36 4ABFE000 2557bc2 4ABFA000 2557d3f 4ABF8000 2557dc3 4ABFC000 2557f90 4ABFE000 2558013 4AB7E000 25582fa 4AA7E000 2558487 4AB7E000 25586f0 4ABFE000 2558772 
This is dataIn[0] before binary 5ABFE000 
This is toBinary[0] 1011010101111111110000000000000 
This should be a timestamp: 2556923 
Binary in String: 1010010101111111110000000000000 
Null Pointer Exception: Not enough data this time through 
This should be a timestamp: 2556b6a 
Binary in String: 1011010101111111110000000000000 
Null Pointer Exception: Not enough data this time through 
This should be a timestamp: 2556dfb 
Binary in String: 1001010101111111110000000000000 
Null Pointer Exception: Not enough data this time through 
This should be a timestamp: 2556f8e 
Binary in String: 1001010101101111110000000000000 
Null Pointer Exception: Not enough data this time through 
This should be a timestamp: 25570da 
Binary in String: 1001010101111111110000000000000 
Null Pointer Exception: Not enough data this time through 
This should be a timestamp: 2557225 
Binary in String: 1001010101111011110000000000000 
Null Pointer Exception: Not enough data this time through 
This should be a timestamp: 255739e 
Binary in String: 1001010101111111110000000000000 
Null Pointer Exception: Not enough data this time through 
This should be a timestamp: 255765a 
Binary in String: 1001010101111111010000000000000 
Null Pointer Exception: Not enough data this time through 
This should be a timestamp: 2557844 
Binary in String: 1001010101111110010000000000000 
Null Pointer Exception: Not enough data this time through 
+0

我看到了一批,我看到你关闭PreparedStatement ....哪一行是抛NPE? – 2014-09-22 17:44:15

+2

请发布您的堆栈跟踪或错误日志,以便它变得更易于理解或解码。 – User27854 2014-09-22 17:50:42

+0

与PreparedStatement相关的任何代码行似乎都会抛出它。当我注释掉这些代码行时,我的代码会一直执行。 – KS7X 2014-09-22 17:51:08

回答

0

能否请你打破报表如下图所示,如果可能请发布您的完整代码。至少是分贝的一部分。

以简化的方式写入。它更容易调试你的情况。

Connection con=DriverManager.getConnection(DB_URL,USER,PASS); 
    String sql = "Your Query"; 
    PreparedStatement ps= conn.prepareStatement(sql); 
    ps.setXXX;--> binding values 
    ps.setXXX;