Coova
Coova

Reputation: 1858

NullPointerException in AsyncTask Interface

I am looking to Implement an AsyncTask, and also implement an interface with the AsyncTask to transfer the User Object that is created to a new Activity that is launched after the AsyncTask is .execute().

The issue is seems to be caused by the interface. I have no issues with the database sending/receiving data.

Am I not declaring something that is in result causing the NullPointerException? Does anyone see anything that is implemented incorrectly?

CREATEUSER

    public static class CreateUser extends AsyncTask<User, Void, User> {

    Context mContext;

    public interface CreateUserInterface {

        public void getCreatedUser(User user);

        public void getUserCreateErrorMessage(String error);

    }

    CreateUserInterface callback;

    public CreateUser(Context context) {
        // TODO Auto-generated constructor stub
        this.mContext = context;
        // callback = (CreateUserInterface) context;
    }

    @Override
    protected User doInBackground(User... params) {
        ObjectMapper mapper = new ObjectMapper(); // create once, reuse
        User user = params[0];
        String url = ROUTE_USER_CREATE;
        HttpPost httppost = new HttpPost(url);
        HttpClient httpclient = new DefaultHttpClient();
        String UserJSONResponse = null;

        try {
            String jsonString = mapper.writeValueAsString(user);
            StringEntity m_stringEntity = new StringEntity(jsonString);

            httppost.setEntity(m_stringEntity);
            httppost.addHeader("Content-type", "application/json");

            HttpResponse postResponse = httpclient.execute(httppost);

            UserJSONResponse = EntityUtils.toString(postResponse
                    .getEntity());
            user = mapper.readValue(UserJSONResponse, User.class);

        } catch (JsonProcessingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return user;
    }

    @Override
    protected void onPostExecute(User result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        //User user = new User();
        Log.e("USERNAME", result.getUser_name());
        callback.getCreatedUser(result);

    }
}

Here is the activity that I am trying to retrieve the User from, and implements the Interface

public class UserFragment extends Fragment implements OnClickListener, UserREST.CreateUser.CreateUserInterface {

UserREST.CreateUser createUserAsyncTask;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup content_frame,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_user, content_frame, false);
    createUserAsyncTask = new UserREST.CreateUser(getActivity());

    Spinner spinner = (Spinner) view.findViewById(R.id.spinnerUserChoice);
    // Create an ArrayAdapter using the string array and a default spinner
    // layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            getActivity(), R.array.user_choice_array,
            android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);

    setHasOptionsMenu(true);
    return view;

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Add your menu entries here
    // inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.user_navigation, menu);
    super.onCreateOptionsMenu(menu, inflater);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
    case R.id.action_createUser:
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getFragmentManager();
        Fragment mCreateUserFragment = new CreateUserFragment();
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, mCreateUserFragment).commit();

    }
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {

    default:
        break;

    }

}

@Override
public void getCreatedUser(User user) {
    // TODO Auto-generated method stub
    Log.e("USERNAME", user.getUser_name()); 

}

@Override
public void getUserCreateErrorMessage(String error) {
    // TODO Auto-generated method stub

}

STACK TRACE:

    10-23 14:16:08.265: E/AndroidRuntime(463): FATAL EXCEPTION: main
10-23 14:16:08.265: E/AndroidRuntime(463): Process: com.e.main, PID: 463
10-23 14:16:08.265: E/AndroidRuntime(463): java.lang.NullPointerException
10-23 14:16:08.265: E/AndroidRuntime(463):  at com.e.rest.UserREST$CreateUser.onPostExecute(UserREST.java:166)
10-23 14:16:08.265: E/AndroidRuntime(463):  at com.e.rest.UserREST$CreateUser.onPostExecute(UserREST.java:1)
10-23 14:16:08.265: E/AndroidRuntime(463):  at android.os.AsyncTask.finish(AsyncTask.java:632)
10-23 14:16:08.265: E/AndroidRuntime(463):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-23 14:16:08.265: E/AndroidRuntime(463):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
10-23 14:16:08.265: E/AndroidRuntime(463):  at android.os.Handler.dispatchMessage(Handler.java:102)
10-23 14:16:08.265: E/AndroidRuntime(463):  at android.os.Looper.loop(Looper.java:136)
10-23 14:16:08.265: E/AndroidRuntime(463):  at android.app.ActivityThread.main(ActivityThread.java:5105)
10-23 14:16:08.265: E/AndroidRuntime(463):  at java.lang.reflect.Method.invokeNative(Native Method)
10-23 14:16:08.265: E/AndroidRuntime(463):  at java.lang.reflect.Method.invoke(Method.java:515)
10-23 14:16:08.265: E/AndroidRuntime(463):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)

Upvotes: 0

Views: 254

Answers (1)

Coova
Coova

Reputation: 1858

Here is what I changed to stop the issue.

Inside CreateUser

        public CreateUser(Context context) {
        // TODO Auto-generated constructor stub
        this.mContext = context;
        callback = (CreateUserInterface) context;
    }

and the NavigationActivity I was using

public class NavigationActivity extends ActionBarActivity implements UserREST.CreateUser.CreateUserInterface

and its associated methods

    @Override
public void getCreatedUser(User user) {
    // TODO Auto-generated method stub
    Log.e("USERNAME", user.getUser_name()); 

}

@Override
public void getUserCreateErrorMessage(String error) {
    // TODO Auto-generated method stub

}

Upvotes: 1

Related Questions