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

555 views Asked by At

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
2

There are 2 answers

1
MByD On BEST ANSWER

You initialized tags[], but not each of its cells, so tags[i].equals(tags[j]) probably caused the NPE (tags[i] is null).

0
oxigen On

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