java.lang.NullPointerException从错误返回null?

时间:2015-10-11 14:01:47

标签: android okhttp

当我对我的测试代码进行调试时,我在这行中出错了

        Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);

错误java.lang.NullPointerException似乎image空洞

这是我获取图片的代码

public class OkHttpHandler extends AsyncTask<Void, Void, byte[]> {

    private final String IMAGE_URL = "http://bit.ly/1DU2Zka";

    OkHttpClient httpClient = new OkHttpClient();

    @Override
    protected byte[] doInBackground(Void... params) {
        Log.d("e", "dddddddddd");
        Log.d("e", IMAGE_URL);

        Request.Builder builder = new Request.Builder();
        builder.url(IMAGE_URL);

        Request request = builder.build();

        try {

            Response response = httpClient.newCall(request).execute();

            System.out.println(response);
            Log.d("e", "FFFFFF response");

            return response.body().bytes();
        } catch (Exception e) {
            Log.d("e", "rrrrrrr");
            e.printStackTrace();
            Log.d("e", "rrrrrrr");

        }

        return null;
    }
}

这是正确的return null;

并且像这样我在mainactivity

中获取图像
    public void downloadImage() {
        OkHttpHandler handler = new OkHttpHandler();

        byte[] image = new byte[0];

        try {

            image = handler.execute().get();
            Log.d("e", "hhhh handler");
System.out.print(image);
            if (image != null && image.length > 0){
                Log.isLoggable("e",image.length);
                Log.d("e", "ddddddd entered the try");
                Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
                imageView.setImageBitmap(bitmap);
  //              txtBytes.setText("Total bytes downloaded: " + image.length);
            }

        } catch (Exception e) {
            Log.d("e", "rrrrrr error");

            e.printStackTrace();
//            txtBytes.setText("Hmm sorry, something went wrong!");
        }

    }

为什么我会得到nullpointerexception?

logcat的

10-11 16:55:22.724  27814-27814/com.justedhak.www.i D/e﹕ ddddddd entered the try
10-11 16:55:22.739  27814-27814/com.justedhak.www.i D/e﹕ rrrrrr error
10-11 16:55:22.739  27814-27814/com.justedhak.www.i W/System.err﹕ java.lang.NullPointerException
10-11 16:55:22.739  27814-27814/com.justedhak.www.i W/System.err﹕ at com.justedhak.www.i.MainActivity.downloadImage(MainActivity.java:82)
10-11 16:55:22.739  27814-27814/com.justedhak.www.i W/System.err﹕ at com.justedhak.www.i.MainActivity.onCreate(MainActivity.java:54)
10-11 16:55:22.744  27814-27814/com.justedhak.www.i W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5389)
10-11 16:55:22.744  27814-27814/com.justedhak.www.i W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
10-11 16:55:22.744  27814-27814/com.justedhak.www.i W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
10-11 16:55:22.744  27814-27814/com.justedhak.www.i W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
10-11 16:55:22.744  27814-27814/com.justedhak.www.i W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:163)
10-11 16:55:22.744  27814-27814/com.justedhak.www.i W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
10-11 16:55:22.744  27814-27814/com.justedhak.www.i W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
10-11 16:55:22.744  27814-27814/com.justedhak.www.i W/System.err﹕ at android.os.Looper.loop(Looper.java:157)
10-11 16:55:22.744  27814-27814/com.justedhak.www.i W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5335)
10-11 16:55:22.744  27814-27814/com.justedhak.www.i W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
10-11 16:55:22.744  27814-27814/com.justedhak.www.i W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
10-11 16:55:22.744  27814-27814/com.justedhak.www.i W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
10-11 16:55:22.744  27814-27814/com.justedhak.www.i W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
10-11 16:55:22.744  27814-27814/com.justedhak.www.i W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

我猜测问题是你的主程序中的图像分配(在你进入try块之前)。你只保留一个字节,这可以吗?

然后你尝试将图像传递给一个似乎产生问题的字节。也许Java只是将图片的第一个字节带到您的变量图像。这就是你传递if条件的原因,但是如果你试图创建一个位图则会失败。

祝你好运

答案 1 :(得分:1)

如果没有正确初始化某些内容,则会显示

空指针异常。检查OnCreate方法,您是否正确初始化了所有内容?