-8

I am writing a ADnroid app, and when i pass a string at this point, I get a nullpointer ...Does someone know why?

Code

        Elements titles = doc.select("h3 a");
        String[] titlelist = null;
        Bitmap[] bitmaps = null;
        outputText = "";
        int i = 0;
        for (Element title : titles) {
            titlelist[i] = title.text(); // <--- here comes the NullPointer
            i = i + 1;
        }

Log:

08-18 17:17:54.522  26470-26470/de.m4lik.somesampleapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: de.m4lik.somesampleapp, PID: 26470
java.lang.NullPointerException
        at de.m4lik.somesampleapp.MainActivity$TheTask.onPostExecute(MainActivity.java:95)
        at de.m4lik.somesampleapp.MainActivity$TheTask.onPostExecute(MainActivity.java:68)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        ...

The doc.select part is jsoup code where i select headings from a website.

Malik
  • 878
  • 2
  • 9
  • 23

3 Answers3

3

titlelist is not initialized.

You need to initialize it prior to assigning Strings to it :

String[] titlelist = new String[titles.size()]; // assuming Elements has a size() method
Eran
  • 387,369
  • 54
  • 702
  • 768
0

You have not initialized your titlelist array, therefore referencing an element of that array will throw a NullPointerException.

Initialize it with:

String[] titlelist = new String[titles.size()];
Mena
  • 47,782
  • 11
  • 87
  • 106
0

Your variable titlelist is defined with a null value. Accessing an element with

titlelist[i]

is therefore producing the NPE. As you are only setting the array elements you probably wanted to declare this variable as

int length = calculateLength(titles); // you must implement this method yourself
String[] titlelist = new String[length];
Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66