0

Here is the code so far I am trying but it is showing me error:

URL url = null;
try {
    url = new URL("http://wap.nastabuss.se/its4wap/QueryForm.aspx?hpl=Teleborg+C+(V%C3%A4xj%C3%B6)");
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println("1");
Document doc = null;
try {
    System.out.println("2");
    doc = Jsoup.parse(url, 3000);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println("3");
Element table = doc.select("table[title=Avgångar:]").first();
System.out.println("4");
Iterator<Element> it = table.select("td").iterator();

//we know the third td element is where we wanna start so we call .next twice
it.next();
it.next();
while(it.hasNext()){
  // do what ever you want with the td element here
System.out.println(it.next());
  //iterate three times to get to the next td you want. checking after the first
  // one to make sure
  // we're not at the end of the table.
  it.next();
  if(!it.hasNext()){ 
    break;
  }
  it.next();
  it.next();
}

It prints System.out.println("3");

then it stops in this line

Element table = doc.select("table[title=Avgångar:]").first();

How can i solve this problem,

Thanks

6
  • whats the worst thing is I cant even print anything whatever, I would really appreciate if someone could have helped me just to print something then i will try my best to specify my query. Commented Oct 23, 2011 at 1:35
  • What's the error you're getting? Commented Oct 23, 2011 at 2:01
  • well, as i told it goes till this line System.out.println("3"); then it stucks and in logcat it says java.lang.nullpointerexception my question is does this JSoup works for android as well??? Commented Oct 23, 2011 at 2:16
  • JSoup works fine on Android. What exactly do the line in your logcat say about the nulpointerexception Commented Oct 23, 2011 at 2:19
  • is there anything which i have to put in Manifest.xml something, bc it is saying unbale to start activity ComonentInfo....NullPointerException Commented Oct 23, 2011 at 2:19

2 Answers 2

1

It looks like the website you're trying to parse the HTML from has an error and doesn't have any tables on it. This is what's causing the null pointer exception. doc.select("table[title=Avgångar:]") isn't returning an element and then you're trying to call a method on it. To prevent this error from happening again, you could do something like this:

Elements foundTables = doc.select("table[title=Avgångar:]");
Element table = null;
if(!foundTables.isEmpty()){
  table = tables.first();
}

Now, if any table was found, the table variable won't be null. You'll just have to alter the code to adapt in case no tables are found.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah you are right Kurtis, now there is not buss which comes to university :P, its too late and since the website is generating table automatically whenever there is a nextbuss. I will try it later, or with any other site btw thanks from ur support, u are really helpful.
i tried this site nseindia.com/content/equities/niftysparks.htm and i changed to Element table = doc.select("table[class=niftyd]").first(); Iterator<Element> it = table.select("td[width=65]").iterator(); Still the same problem :S
at least i shouldnt have here any problem :P, is it working for u this example??? where is my mistake i am wondering.
@SamuelLund, You're welcome. If you find my answer helpful, please vote it up and/or accept it. I've modified my answer to help expand on dealing with the null pointer exception.
0

You're not checking the result of doc.select() before calling .first(). If there are no elements in the document that match the specified query, doc.select() could return null. Then you are calling .first() on a null pointer which, of course, will throw an exception. There is no table tag with the title you have specified in the document that you are using in your example. So, the result is not surprising.

1 Comment

I tried to get the doc.select() before calling the .first but still I get some errors. Can you please tell me exactly what is the problem. or it might help me a lot if u would have writen me some code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.