空指针异常的奇怪情况

时间:2014-09-22 17:38:13

标签: java database nullpointerexception prepared-statement stringbuilder

我正在运行一个连接到设备的程序,而该设备又会向我发送数据。

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

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

以下是我的大部分代码:

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

更新

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

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

1 个答案:

答案 0 :(得分:0)

如果可能的话,请你打破如下所示的陈述,请发布完整的代码。至少是db部分。

以简化的方式编写它。在您的情况下更容易调试。

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