1

I keep getting this error

NullPointer

08-16 22:55:46.360: ERROR/AndroidRuntime(11047): Caused by: java.lang.NullPointerException
  08-16 22:55:46.360: ERROR/AndroidRuntime(11047):     at com.fttech.htmlParser.releaseInfo.onCreate(releaseInfo.java:62)
08-16 22:55:46.360: ERROR/AndroidRuntime(11047):     at  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-16 22:55:46.360: ERROR/AndroidRuntime(11047):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)

Its pointing to my Element here

    Element paragraph = overview.select("p").last();

i am using this to retrieve the article

    try {
        doc = Jsoup.connect(url).get();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    if(doc == null){
        Toast.makeText(this, "Couldnt retrieve game info", Toast.LENGTH_SHORT);
    }
    else{

    // Get the overview div
    Element overview = doc.select("div#object-overview").last();
yoshi24
  • 3,147
  • 8
  • 45
  • 62
  • overview may be null or overview.select("p") may be null. is that possible you to log html string and see? – Sudar Nimalan Aug 17 '11 at 05:12
  • I logged them for some it works PERFECT but for example http://pc.gamespy.com/pc/robot-entertainment-project-1-untitled/ this link gives a error at // Element overview = doc.select("div#object-overview").last(); When i try and log it. – yoshi24 Aug 17 '11 at 05:38
  • Also this link too http://xbox360.gamespy.com/xbox-360/street-fighter-iii-third-strike-online-edition/ – yoshi24 Aug 17 '11 at 05:40

1 Answers1

1

Everytime you look for an element with select("") your calling last() in a chain which assumes it will always find atleast 1 element, in the situation that there is no say "p" in the document, that is when you will encounter a crash.

It's just simple NullPointerExceptions, you need to learn to code defensively:

// If you believe overview could be null
if(overview != null){
    ArrayList<Element> paragraphs = overview.select("p"); // Whatever type select(String) returns
    Element lastParagraph = null;
    if(paragraphs != null){
         lastParagraph = paragraphs.last();
    } else {
     // Deal with not finding "p" (lastParagraph is null in this situation)
    }

   // Continue with lastParagraph 

} else {
  // Deal with overview being null
}

Number 1 Java Error (scroll down)

Also you shouldn't really wrap your code with a catch all Exception, try to catch each exception and deal with them individually.

Lookup the API for your get() method Jsoup get() (eclipse tells you this anyway) It throws IOException, so you should just catch this.

  try {
        doc = Jsoup.connect(url).get();
    } catch (IOException e) {
        Log.e("Tag", "Jsoup get didn't get a document", e);
    } 

Number 5 Java Error (scroll down)

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • Thanks! I'll try this in a few and mark your answer correct when I do. Also thanks for the link. Its helpful. And also what and how should I be cstching for these and which exceptions? – yoshi24 Aug 17 '11 at 12:30
  • I was forgetting to log the error in the catch. how could I loop the try to loop till it gets it? or this may be uneccessary, either it wasn't fetchd because of bad service or a server is down. – yoshi24 Aug 17 '11 at 12:52
  • Number 5 in that link I showed you is about your catch block :-). Yes if it wasn't fetched either inform the user with a message and a retry button, or use a while(doc==null && retry<5) or similar loop. – Blundell Aug 17 '11 at 13:18