Note that there are some explanatory texts on larger screens.

plurals
  1. PONullPointerException parsing with JSoup
    primarykey
    data
    text
    <p>With the following code,</p> <pre><code>Document doc = null; doc = Jsoup.parse(page): String myexample = "My example"; Elements myexamples = doc.select("div.yourOptions p"); if (myexamples != null &amp;&amp; myexamples.first().ownText() != null) myexample = myexamples.first().ownText(); </code></pre> <p>everything seems to be good for 99.9% of my application's users.</p> <p>But I received a few StackTraces with a NullPointerException at (the equivalent of) line 7. StackTrace :</p> <pre><code>java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:278) at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) at java.util.concurrent.FutureTask.setException(FutureTask.java:124) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) at java.util.concurrent.FutureTask.run(FutureTask.java:137) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) at java.lang.Thread.run(Thread.java:856) Caused by: java.lang.NullPointerException at com.tomtom.joe.mobile.Suivi$BackgroundAsyncTask.doInBackground(Suivi.java:369) at com.tomtom.joe.mobile.Suivi$BackgroundAsyncTask.doInBackground(Suivi.java:1) at android.os.AsyncTask$2.call(AsyncTask.java:264) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) ... 4 more </code></pre> <p>How could I be protected against NullPointerException ? The content of "myexamples.first().ownText()" varies with users ! But I guess "myexamples.first().ownText()" could be null...</p> <p>Example :</p> <pre><code>&lt;div class="yourOptions"&gt;&lt;div class="cockpitContent resizeHeight1"&gt; &lt;h2 class="Title"&gt;Title&lt;/h2&gt; &lt;p&gt;Options&lt;/p&gt; &lt;p&gt;Options 2&lt;/p&gt; &lt;/div&gt;&lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload