-3

I'm trying to make a simple function that grabs an image from an http connection. It was working before, now it's throwing some errors. I didn't make any changes to the code. Also the Input stream isn't null. It's returning expected data.

D/skia: ---- read threw an exception
D/skia: --- SkAndroidCodec::NewFromStream returned null
I/ERROR: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference

This is my Code for reference inside of an async task:

@Override
protected Bitmap doInBackground(String... url){
    try {
        InputStream in = openHttpConnection(url[0]);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeStream(in, null, options);
        in.close();
        return bitmap;
    } catch (IOException e) {
        Log.i("ERROR", e.toString());
        return null;
    }
}

private static InputStream openHttpConnection(String urlString) throws IOException {
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))
        throw new IOException("Not an HTTP connection");

    try {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();
        response = httpConn.getResponseCode();
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();
        }
        httpConn.disconnect();
    } catch (Exception e) {
        Log.i("ERROR", e.toString());
        throw new IOException("Error connecting");
    }
    return in;
}
Wnanjo
  • 15
  • 4
  • *It was working before* then revert changes ... I bet on `httpConn.disconnect();` before you even start reading the input stream – Selvin Apr 27 '17 at 09:02
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Christopher Apr 27 '17 at 09:03
  • The Null object is a static class from code that I in no way have access to. For clarity The input stream is not coming back null. – Wnanjo Apr 27 '17 at 09:12
  • It has been said before `httpConn.disconnect();`. Remove that. It can never have worked before. – greenapps Apr 27 '17 at 09:31

1 Answers1

0
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeStream(in, null, options);

After this statement bitmap is null. It will always be if you ask decodeStream to just decode the bounds.

Later you try to compress that null bitmap with `bitmap.Compress().

It does not matter if that would be in a static function.

Just dont call it if bitmap==null.

greenapps
  • 10,799
  • 2
  • 13
  • 19
  • Bitmap Compress is called from DecodeStream I don't call it. – Wnanjo Apr 27 '17 at 09:21
  • No. Not at all. And it gets called later when you try to use the bitmap. You did not tell what you want to do with the downloaded image. Saving to file? Please confirm that bitmap== null and that you blindly use a null object/pointer. – greenapps Apr 27 '17 at 09:25
  • You are right about this, but I still can't understand why my http request is failing here then? I'll probably scrap this for something like Picasso in the Future. – Wnanjo Apr 27 '17 at 10:57
  • You http request is not failing at all. What makes you think so? The bitmap is null because you just decode the bounds. – greenapps Apr 27 '17 at 11:00