简体   繁体   中英

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

  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.

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

if i > 3 than tags[i] return null. And tags[i].equals throw NPE

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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