Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Blog Reader Android App Getting Data from the Web Moving Work from the Main Thread to an AsyncTask

Error: Missing return statement when declaring doInBackground

When declaring doInBackground using the following code:

private class CustomAsyncTask extends AsyncTask<Object, Void, String> {

    @Override
    protected String doInBackground(Object... arg0) {
            String responseString = "";  
    }
    }

which keeps returning this error:

./com/example/MainListActivity.java:20: error: missing return statement
    }
    ^
1 error

I've been tweaking things that make sense in my mind to fix this but with no luck. The line 20 being referred to is the second from last }.

Am I missing something obvious?

2 Answers

Hi Alistair,

When you declared CustomAsyncTask you explicitly defined a return type for your method i.e. String. In Android thus Java, when you define a return type for a method, well you have to return the same type of value.

So in your case

private class CustomAsyncTask extends AsyncTask<Object, Void, String> {

    @Override
    protected String doInBackground(Object... arg0) {
            String responseString = "";  

            return someStringValue;  // you will have to return a String value. If you don't you get the error
    }
  }

Hope this helps

Thank you, I knew it would be something obvious!