简体   繁体   中英

Jsoup library ,trying to make an Android app which reads data from HTML site

I am facing with an problem which i couldnt fix it for whole day :S, btw the problem is when I try to parse an html site and if there are data in that html site it works fine, but when there is no data that time it hangs and shows me an error in my LogCat where it says NullPointer, for ex: here we have two links one which has data and the other one which doesnt have data:

1st link: http://wap.nastabuss.se/its4wap/QueryForm.aspx?hpl=Resecentrum+(V%C3%A4xj%C3%B6 ) 2nd link: http://wap.nastabuss.se/its4wap/QueryForm.aspx?hpl=Ulfsn%C3%A4s+(Hallands+l%C3%A4n )

So the error shows here in case when i try to display data from the 2nd link which is empty.

        Element table = doc.select("table[class=tableMenuCell]").first();
    Iterator<Element> ite = table.select("td[align=right]").iterator();
    Iterator<Element> destinacions = table.select("td[align=left]").iterator();

and the rest of code after these lines is just while statement and i add the data in arraylist which works fine when there is data on that link.

At the midle line code ate "ite" line is showing the error.

So how can I make any if statement or smth which checks if there is no data at all in that site to not check and just to dipslay some msgbox that there is no data.

Thanx, I would really appreciate if someone can help me this case.

You need to check for not null

if(table != null) 
{ 
Iterator<Element> ite = table.select("td[align=right]").iterator();
 Iterator<Element> destinacions = table.select("td[align=left]").iterator();
 }

Because your second link has no data your table is null, any operation on null throws nullpointer exception

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