简体   繁体   中英

What's the source of this NullPointerException

OK, so I'm writing an Android app, and am trying to download a Bitmap and set it as an ImageView. The code is below for the relevant parts:

    private class GetContactInfo extends AsyncTask<String, Void, ContactInfo[]> {

    @Override
    protected ContactInfo[] doInBackground(String... url) {
        // Instantiate what is needed
        URL json = null;

        //Set the JSON URL
        try {
            json = new URL(url[0]);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }

        // Use Jackson library to read out the data from the contacts page
        try {
            contacts = mapper.readValue(json, ContactInfo[].class);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //Add everything into the bitmap ArrayList
        for (int i = 0; i < contacts.length; i++) {
            String imageURL = contacts[i].getSmallImageURL();

            // Download the Bitmap and add it to the ArrayList
            try {
                bitmap.add(downloadBitmap(imageURL));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // Return statement
        return contacts;
    }

public Bitmap downloadBitmap(String imageURL) throws IOException {

    URL url = new URL(imageURL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.connect();
    InputStream stream = connection.getInputStream();
    Bitmap bitmap = BitmapFactory.decodeStream(stream);
    if (bitmap == null) {
        Log.e("Null", "Bitmap null");
    }

    return bitmap;
}

The log never catches that bitmap is null, or at least it doesn't show it (I see in the stack trace that there are 4 more errors, but they never show up and I'm not sure how to expand it to show the other errors.)

The NullPointerException comes at the bitmap.add(downloadBitmap(imageURL)); line. So somehow my downloadBitmap function is returning a null result. Any ideas?

Edit: I'm not sure if this matters, but the images in the URLs are .jpeg files.

Edit 2: Put this in the comments so I will edit it into my post as well, bitmap is declared as a Global Variable like so ArrayList<Bitmap> bitmap; This is so I can later use it in my onPostExecute method.

As you said the error is at line

bitmap.add(downloadBitmap(imageURL)); 

which means culprit is your bitmap variable and not downloadBitmap(imageURL) method.

Also, in your edit you have mentioned that you have declared bitmap as a global variable - ArrayList bitmap;

In order to access(add bitmap onjects to it) this globally declared variable you must initialize it.

In your onCreate do -

bitmap = new ArrayList<Bitmap>(); 

and the NPE must go.

While you are downloading images from the Internet, you should use an async request. In downloadBitmap the connection is downloading in another thread, but the main thread has returned bitmap immediately, whether or not the downloading is accomplished.

Where did you initialized bitmap ? As far as I can tell, it is null and you are using that null object, so Null Pointer Exception come out. That's from the information you provided. If the error is occurred inside the function, it's the different matter.

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