简体   繁体   English

j2me函数java.lang.nullpointerexception-这有什么问题?

[英]j2me function java.lang.nullpointerexception - what is wrong in this?

Yesterday Night, i wrote this function to fetch all the tags from string containing xml data but something is not correct in this... Pls help... this function is returning java.lang.NullPointerException 昨天晚上,我编写了此函数来从包含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;
      }
  }

error that i saw while executing in debugger mode 在调试器模式下执行时看到的错误

TRACE: <at java.lang.NullPointerException:   0>, Exception caught in Display class
java.lang.NullPointerException:   0
 - httpcon.parseWebXML(), bci=170

tags[] has only 3 items. tags []只有3个项目。

if(i == 0 || i == 1 || i == 2){
            if(tagn.indexOf("/") == -1){
                tags[k] = "<"+tagn+">";
                k +=1;

if i > 3 than tags[i] return null. 如果i> 3大于tag [i],则返回null。 And tags[i].equals throw NPE 和标签[i]。等于抛出NPE

您初始化了tag [],但未初始化其每个单元,因此tags[i].equals(tags[j])可能导致了NPEtags[i]为null)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM