J2me RecordStore 错误:java.lang.NullPointerException:0 - 下面的代码有什么问题
下面所示的功能有什么问题吗?我在调试器中运行此代码时发现的唯一错误是“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;
}
Is there anything wrong in function shown below. The only bug that I found while running this code in debugger is "java.lang.NullPointerException: 0". I wasted my last many hours in figuring out "what is causing this error", Somebody pls help.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个空指针异常可能出现在这里,如果传递给函数的字符串为空,
请将其更改为
if ("Logged".equals(myData)) {
第二个空指针异常可能出现在此处,您无法调用函数在空对象上
初始化 rs
第三个 NUll 指针异常可能会出现,
因为数组输出未初始化,将数组初始化为
String [] output = new String[2]
First Null pointer exception can come here, if string passed to the function is null
change this to
if ("Logged".equals(myData)) {
Second Null pointer Exception can come here, you cannot invoke function on null object
Initialize rs
Third NUll pointer exception can come here
because array output is not initialized, initialize array as
String [] output = new String[2]
改变这个:
到这个:
Change this:
To this: