views:

1807

answers:

3

hi friends... a strange problem has cropped up... I checked several websites but couldnt find anything wrong with the code... But it returns NullPointerException...

try{
    SAXParserFactory f = SAXParserFactory.newInstance();
    SAXParser parser = f.newSAXParser();
    XMLReader reader = parser.getXMLReader();
    reader.setContentHandler(handler);
    System.out.println(uri_this);
    reader.parse(new InputSource(url_this.openStream()));// i get nullpointerexception here
   }
   catch(NullPointerException e)
   {
    System.out.println(e);
    }

In the catch statement i have caught NullPointerException and made it print the error...

The ContentHandler is fine coz i have got parsed files for small files but for larger files it seems to not work... Wat must be the problem? Or do i need some buffering for larger files?? Please friends m clueless... and have tried all possibilities except buffering... coz i don know the coding for that...

i figured out 1 thing.. The object of the inputsource is freed before the reader.parse can finish... i don understand y its happening...

The LogCat gives the following(only the erronous part)...

01-06 15:12:27.145: INFO/System.out(319): http://mywebsite.com/products_xml.php?cPath=43
01-06 15:12:31.094: DEBUG/dalvikvm(319): GC freed 4031 objects / 203424 bytes in 189ms
01-06 15:12:31.505: DEBUG/dalvikvm(100): GC freed 3326 objects / 190192 bytes in 283ms
01-06 15:12:34.274: DEBUG/dalvikvm(319): GC freed 1153 objects / 80432 bytes in 146ms
01-06 15:12:34.646: INFO/System.out(319): java.lang.NullPointerException

I'm getting the url "url_this" 4m the constructor as arguments... sorry had a typo error...

+2  A: 

There not enough info to help you here but did you notice that you print out a variable called uri_this in the shell but dereference url to be parsed? So just because the logfile shows http://mywebsite.com/products_xml.php?cPath=43 it does not automatically mean, that the variable url is initialized and also contains that string.

Or it could be that the openStream() method on the url object (whatever type it is) returns null in some conditions and InputSource can not handle being passed null in the constructor. As I said - there is not enough info in the short snipplet to solve your problem.

Cheers.

Valentin
Thanx Valentin. Actually i myself was xonfused on wat to post here. coz i had global definitions and a lot of code in between the snippet i hsve pasted. Thanx 4 ur valuable comments...
JaVadid
A: 

will this help ? http://java.sun.com/docs/books/tutorial/networking/urls/readingURL.html

Alex Volovoy
Thanx Alex Volovoy... that article was great help and i guess it will be very useful later too...
JaVadid
A: 

Hey thanx all of u. I got my mistake. actually in 1 case there was no text between start and end tag... hence the variable i stored text in was null... i called .toString() on that. resulting in an exception - NullPointerException. Thanx all 4 ur help.

JaVadid