简体   繁体   English

此NullPointerException的来源是什么

[英]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. 好的,所以我正在编写一个Android应用程序,并试图下载位图并将其设置为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.) 日志从不捕获位图为null,或者至少不显示位图(我在堆栈跟踪中看到还有4个错误,但它们从未显示出来,并且我不确定如何扩展它以显示其他错误。)

The NullPointerException comes at the bitmap.add(downloadBitmap(imageURL)); NullPointerException位于bitmap.add(downloadBitmap(imageURL)); line. 线。 So somehow my downloadBitmap function is returning a null result. 所以我的downloadBitmap函数以某种方式返回空结果。 Any ideas? 有任何想法吗?

Edit: I'm not sure if this matters, but the images in the URLs are .jpeg files. 编辑:我不确定这是否重要,但是URL中的图像是.jpeg文件。

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; 编辑2:将其放在注释中,以便我也将其编辑到我的帖子中,将位图声明为全局变量,例如ArrayList<Bitmap> bitmap; This is so I can later use it in my onPostExecute method. 因此,以后可以在onPostExecute方法中使用它。

As you said the error is at line 正如你所说的,错误在行

bitmap.add(downloadBitmap(imageURL)); 

which means culprit is your bitmap variable and not downloadBitmap(imageURL) method. 这意味着罪魁祸首是您的位图变量,而不是downloadBitmap(imageURL)方法。

Also, in your edit you have mentioned that you have declared bitmap as a global variable - ArrayList bitmap; 另外,在编辑中,您提到已将位图声明为全局变量-ArrayList位图;

In order to access(add bitmap onjects to it) this globally declared variable you must initialize it. 为了访问(向其添加位图)此全局声明的变量,您必须对其进行初始化。

In your onCreate do - 在您的onCreate中-

bitmap = new ArrayList<Bitmap>(); 

and the NPE must go. NPE必须走了。

While you are downloading images from the Internet, you should use an async request. 从Internet下载图像时,应使用异步请求。 In downloadBitmap the connection is downloading in another thread, but the main thread has returned bitmap immediately, whether or not the downloading is accomplished. downloadBitmap该连接正在另一个线程中进行下载,但是无论是否完成下载,主线程都会立即返回bitmap

Where did you initialized bitmap ? 您在哪里初始化bitmap As far as I can tell, it is null and you are using that null object, so Null Pointer Exception come out. 据我所知,它为null,并且您正在使用该null对象,因此出现了Null Pointer Exception。 That's from the information you provided. 那是从您提供的信息中得出的。 If the error is occurred inside the function, it's the different matter. 如果错误发生在函数内部,则是另一回事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM