Вопрос

I'm getting a crash report on an app in Google Play. I use an asynctask to get an image, it works on every device I tested but there are some users having problems with it. It's really rare, but I need to debug and fix this. This is the doInBackground method:

public class MetaTask extends AsyncTask<Void, Void, Void> {
    URL imageurl;

    @Override
    protected Void doInBackground(Void... params) {
        try {
            doc = Jsoup.connect(data).get();
            Elements meta = doc.select("meta[property=og:image]");
            for (Element element : meta) {
                extracted = element.attr("content");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }

        try {
            imageurl = new URL(extracted);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            bitmap = BitmapFactory.decodeStream(imageurl.openConnection()
                    .getInputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

The nullpointer seems to be on this line(271):

bitmap = BitmapFactory.decodeStream(imageurl.openConnection()
                .getInputStream()); 

I'm thinking about for some reason the variable imageurl not beeing an actual url, the only thing I can think of is checking the string variable extracted for containing "http" and "jpg" and if these are not present storing this information using Flurry.

Before the asynctask is executed a check is performed for availability of an internet connection, so that can't be causing this.

Can anyone think of something else causing this? Here is my complete stacktrace record of the crash reports:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856) 
Caused by: java.lang.NullPointerException
at        com.ddroid.photosaveinstagram.MainActivity$MetaTask.doInBackground(MainActivity.java:271)
at com.ddroid.photosaveinstagram.MainActivity$MetaTask.doInBackground(MainActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more
Это было полезно?

Решение

What if you get an exception in:

try {
            imageurl = new URL(extracted);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Then your imageurl will be null. So, here:

try {
            bitmap = BitmapFactory.decodeStream(imageurl.openConnection()
                    .getInputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

you will get NullPointerException because imageurl is only declared but not initialised. So change this at the top:

URL imageurl=null

Hope this works. Best of luck.

Другие советы

Just for the sake of completeness:

In your code you are ignoring exceptions which you shoulddn't do if you need to work with values obtained within the try/catch block! Instead wirting better code is better and avoids such situations.

public class MetaTask extends AsyncTask<Void, Void, Void> {
    URL imageurl;

    @Override
    protected Void doInBackground(Void... params) {
        try {
            doc = Jsoup.connect(data).get();
            Elements meta = doc.select("meta[property=og:image]");
            for (Element element : meta) {
                extracted = element.attr("content");
            }

            imageurl = new URL(extracted);
            bitmap = BitmapFactory.decodeStream(imageurl.openConnection()
                    .getInputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();

            // Notify user that an IO error occured 
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

            // Notify user that the URL is invalid
        } catch (Exception e) {
            // Ignore all other errors... probably not a good idea unless you know what you do 
        }

        return null;
    }
}

This code will exit as soon as an Exception occurs and within the try/catch block it's assumed that everything works well.

You should learn how to properly handle exceptions, instead of ignoring them because Eclipse refuses to compile your code and you add an auto-try/catch block via IDE helper methods.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top