如下所示的功能有什么问题。我在调试器中运行此代码时发现的唯一错误是“ java.lang.NullPointerException:0”。我浪费了最后几个小时来弄清楚“是什么导致了这个错误”,有人请帮忙。
public String[] addRECORD(String mydata){
String[] output = null;
try {
RecordStore rs = null;
String sb = null;
RecordStore.openRecordStore(REC_STORE, true);
if (mydata.equals("Logged")) {
byte[] recData = new byte[5];
int len;
for(int i = 1; i <= rs.getNumRecords(); i++){
if(rs.getRecordSize(i) > recData.length){
recData = new byte[rs.getRecordSize(i)];
}
len = rs.getRecord(i, recData, 0);
sb += new String(recData, 0, len);
}
if (sb != null) {
output[0] = "rexists";
output[1] = sb.trim();
} else {
output[0] = "notlogged";
output[1] = sb.trim();
}
}else{
byte[] rec = mydata.getBytes();
try{
rs.addRecord(rec, 0, rec.length);
}catch (Exception e){}
output[0] = "radded";
output[1] = mydata;
}
rs.closeRecordStore();
} catch (RecordStoreException ex) {
responder(ex.getMessage());
}
return output;
}
最佳答案
如果传递给函数的字符串为null,则第一个Null指针异常可能会出现在此处
if (mydata.equals("Logged")) {
将此更改为
if ("Logged".equals(myData)) {
第二个Null指针异常可能会在这里出现,您无法在空对象上调用函数
for(int i = 1; i <= rs.getNumRecords(); i++){
初始化rs
第三个NUll指针异常可以在这里出现
output[0] = "rexists";
output[1] = sb.trim();
因为数组输出未初始化,所以将数组初始化为
String [] output = new String[2]