![](/img/trans.png)
[英]J2me RecordStore error : java.lang.NullPointerException: 0 - What is wrong in below code
[英]j2me function java.lang.nullpointerexception - what is wrong in this?
昨天晚上,我編寫了此函數來從包含xml數據的字符串中獲取所有標簽,但是在此中有些不正確...請幫助...該函數返回java.lang.NullPointerException
public void parseWebXML(String xd){
int i, j, k = 0;
String tagn, check = "";
int spos, epos;
byte[] len = xd.getBytes();
tags = new String[len.length*3/4];
int nextpos = 0;
for(i=0;i<len.length*3/4;i++){
spos = xd.indexOf("<", nextpos);
epos = xd.indexOf(">", spos);
tagn = xd.substring(spos, epos);
if(i == 0 || i == 1 || i == 2){
if(tagn.indexOf("/") == -1){
tags[k] = "<"+tagn+">";
k +=1;
}else{
continue;
}
}else{
if(tagn.indexOf("/") == -1){
for(j=0;j<tags.length;j++){
if(tags[i].equals(tags[j])){
check = "found";
}else{
check = "notfound";
}
}
if(check.equals("notfound")){
tags[i] = "<"+tagn+">";
k+=1;
}else{
continue;
}
}else{
continue;
}
}
nextpos = epos + 1;
}
}
在調試器模式下執行時看到的錯誤
TRACE: <at java.lang.NullPointerException: 0>, Exception caught in Display class
java.lang.NullPointerException: 0
- httpcon.parseWebXML(), bci=170
tags []只有3個項目。
if(i == 0 || i == 1 || i == 2){
if(tagn.indexOf("/") == -1){
tags[k] = "<"+tagn+">";
k +=1;
如果i> 3大於tag [i],則返回null。 和標簽[i]。等於拋出NPE
您初始化了tag [],但未初始化其每個單元,因此tags[i].equals(tags[j])
可能導致了NPE
( tags[i]
為null)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.