0

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
Krishna
  • 165
  • 1
  • 12

2 Answers2

1

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

oxigen
  • 6,263
  • 3
  • 28
  • 37
0

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

MByD
  • 135,866
  • 28
  • 264
  • 277
  • thanks man... your answer helped me in detecting problem in another funtion in series... In present one... bug was different. – Krishna Oct 31 '11 at 17:19