我正在运行一个程序,它连接到一个设备,它反过来给我发送数据。
数据以字符串的形式输入,然后进行相应的处理,尝试使用准备好的语句将其存储到数据库中。
我捕捉到一个空指针异常,它似乎来自准备好的语句。我认为这是个问题,因为当我注释代码时,异常就会消失。也许我并不像我想的那样理解事先准备好的陈述。有什么建议吗?
以下是我的大部分代码:
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发布于 2014-09-22 18:19:28
如果可能的话,请把你的完整代码贴出来。至少分贝部分。
以一种简化的方式写。在您的情况下,调试更容易。
Connection con=DriverManager.getConnection(DB_URL,USER,PASS);
String sql = "Your Query";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setXXX;--> binding values
ps.setXXX;https://stackoverflow.com/questions/25979974
复制相似问题