引发致命异常AsyncTask#3(由nullpointerexception引起)

时间:2013-08-30 14:37:38

标签: android json android-asynctask nullpointerexception

我试图获取数据来设置edittext框但是当我启动活动致命异常asynctask(由NullPointer Exception引起)在我的“GetStudentDetails”类的doinbackground中抛出..

logcat的: -

08-30 14:15:49.412: E/AndroidRuntime(1655): FATAL EXCEPTION: AsyncTask #3
08-30 14:15:49.412: E/AndroidRuntime(1655): java.lang.RuntimeException: An error occured while executing doInBackground()
08-30 14:15:49.412: E/AndroidRuntime(1655):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-30 14:15:49.412: E/AndroidRuntime(1655):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
08-30 14:15:49.412: E/AndroidRuntime(1655):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
08-30 14:15:49.412: E/AndroidRuntime(1655):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
08-30 14:15:49.412: E/AndroidRuntime(1655):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-30 14:15:49.412: E/AndroidRuntime(1655):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-30 14:15:49.412: E/AndroidRuntime(1655):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-30 14:15:49.412: E/AndroidRuntime(1655):     at java.lang.Thread.run(Thread.java:856)
08-30 14:15:49.412: E/AndroidRuntime(1655): Caused by: java.lang.NullPointerException
08-30 14:15:49.412: E/AndroidRuntime(1655):     at job.placed.placemeant.StudentEditProfile$GetStudentDetails.doInBackground(StudentEditProfile.java:126)
08-30 14:15:49.412: E/AndroidRuntime(1655):     at job.placed.placemeant.StudentEditProfile$GetStudentDetails.doInBackground(StudentEditProfile.java:1)
08-30 14:15:49.412: E/AndroidRuntime(1655):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-30 14:15:49.412: E/AndroidRuntime(1655):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-30 14:15:49.412: E/AndroidRuntime(1655):     ... 4 more

studenteditprofile.class包含GetStudentDetails类: -

public class StudentEditProfile extends Activity implements
    OnItemSelectedListener {

EditText enrollment, ten, twelve, aggregate, backlog, pendingbacks;
protected int mPos;
protected String mSelection;
Button btnSave;

Spinner spinnerBranch;

protected ArrayAdapter<CharSequence> mAdapter;

// Progress Dialog
private ProgressDialog pDialog;
SessionManager session;

// JSON parser class
JSONParser jsonParser = new JSONParser();

// single product url
private static final String url_student_detials = "http://192.168.0.2:80/placemeant/getstudent.php";

// url to update product
private static final String url_update_student = "http://192.168.0.2:80/placemeant/update_student.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";

private static final String TAG_EMAIL = "email";
private static final String TAG_ENROLLMENT = "enrollment";
private static final String TAG_POSTS = "posts";
private static final String TAG_TEN = "ten";
private static final String TAG_TWELVE = "twelve";
private static final String TAG_AGGREGATE = "aggregate";
private static final String TAG_BACKLOG = "backlog";
private static final String TAG_PENDINGBACKS = "pendingbacks";
private static final String TAG_BRANCH = "branch";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editme);

    // save button
    btnSave = (Button) findViewById(R.id.bEDITSubmit);

    spinnerBranch = (Spinner) findViewById(R.id.EDITspinnerBranch);

    this.mAdapter = ArrayAdapter.createFromResource(this,
            R.array.branch_array,
            android.R.layout.simple_spinner_dropdown_item);
    spinnerBranch.setAdapter(mAdapter);

    spinnerBranch.setOnItemSelectedListener(this);

    // Getting complete student details in background thread
    new GetStudentDetails().execute();

    // save button click event
    btnSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // starting background task to update product
            new SaveStudentDetails().execute();
        }
    });

}

/**
 * Background Async Task to Get complete product details
 * */
class GetStudentDetails extends AsyncTask<String, String, String> implements
        OnItemSelectedListener {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(StudentEditProfile.this);
        pDialog.setMessage("Loading student details. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Getting product details in background thread
     * */
    protected String doInBackground(String... params) {

        // updating UI from Background Thread

        // Check for success tag
        int success;
        try {
            HashMap<String, String> user = session.getUserDetails();

            String post_useremail = user.get(SessionManager.KEY_EMAIL);
            // Building Parameters
            List<NameValuePair> params1 = new ArrayList<NameValuePair>();
            params1.add(new BasicNameValuePair("email", post_useremail));

            // getting product details by making HTTP request
            // Note that product details url will use GET request
            JSONObject json = jsonParser.makeHttpRequest(
                    url_student_detials, "POST", params1);

            // check your log for json response
            Log.d("Getting Student Details", json.toString());

            // json success tag
            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                // successfully received product details
                JSONArray studentObj = json.getJSONArray(TAG_POSTS); // JSON
                                                                        // Array

                // get first company object from JSON Array
                final JSONObject setdetails = studentObj.getJSONObject(0);

                runOnUiThread(new Runnable() {
                    public void run() {
                        enrollment = (EditText) findViewById(R.id.etEDITEnroll);

                        ten = (EditText) findViewById(R.id.etEDIT10);
                        twelve = (EditText) findViewById(R.id.etEDIT12);
                        aggregate = (EditText) findViewById(R.id.etEDITAggre);
                        backlog = (EditText) findViewById(R.id.etEDITBackLog);
                        pendingbacks = (EditText) findViewById(R.id.etEDITBackpending);
                        spinnerBranch = (Spinner) findViewById(R.id.EDITspinnerBranch);

                        // display product data in EditText
                        try {
                            enrollment.setText(setdetails
                                    .getString(TAG_ENROLLMENT));
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        try {
                            ten.setText(setdetails.getString(TAG_TEN));
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        try {
                            twelve.setText(setdetails.getString(TAG_TWELVE));
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        try {
                            aggregate.setText(setdetails
                                    .getString(TAG_AGGREGATE));

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        try {
                            backlog.setText(setdetails
                                    .getString(TAG_BACKLOG));

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        try {
                            pendingbacks.setText(setdetails
                                    .getString(TAG_PENDINGBACKS));

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        try {
                            String setspin = setdetails
                                    .getString(TAG_BRANCH);

                            int spinnerPosition = mAdapter
                                    .getPosition(setspin);
                            spinnerBranch.setSelection(spinnerPosition);

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                });
            } else {

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once got all details
        pDialog.dismiss();
    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
}

/**
 * Background Async Task to Save product Details
 * */
class SaveStudentDetails extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(StudentEditProfile.this);
        pDialog.setMessage("Saving details ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Saving product
     * */
    protected String doInBackground(String... args) {

        enrollment = (EditText) findViewById(R.id.etEDITEnroll);

        ten = (EditText) findViewById(R.id.etEDIT10);
        twelve = (EditText) findViewById(R.id.etEDIT12);
        aggregate = (EditText) findViewById(R.id.etEDITAggre);
        backlog = (EditText) findViewById(R.id.etEDITBackLog);
        pendingbacks = (EditText) findViewById(R.id.etEDITBackpending);
        spinnerBranch = (Spinner) findViewById(R.id.EDITspinnerBranch);
        // getting updated data from EditTexts
        String enroll = enrollment.getText().toString();

        String tenpercent = ten.getText().toString();
        String tewlvepercent = twelve.getText().toString();
        String aggregatesem = aggregate.getText().toString();
        String totalbacklog = backlog.getText().toString();
        String totalpending = pendingbacks.getText().toString();
        String branchselected = spinnerBranch.getSelectedItem().toString();
        HashMap<String, String> usere = session.getUserDetails();

        String postuseremail = usere.get(SessionManager.KEY_EMAIL);
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair(TAG_EMAIL, postuseremail));
        params.add(new BasicNameValuePair(TAG_ENROLLMENT, enroll));

        params.add(new BasicNameValuePair(TAG_TEN, tenpercent));
        params.add(new BasicNameValuePair(TAG_TWELVE, tewlvepercent));
        params.add(new BasicNameValuePair(TAG_AGGREGATE, aggregatesem));
        params.add(new BasicNameValuePair(TAG_BACKLOG, totalbacklog));
        params.add(new BasicNameValuePair(TAG_PENDINGBACKS, totalpending));
        params.add(new BasicNameValuePair(TAG_BRANCH, branchselected));

        // sending modified data through http request
        // Notice that update product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_update_student,
                "POST", params);

        // check json success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully updated
                Intent i = getIntent();
                // send result code 100 to notify about product update
                setResult(100, i);
                finish();
            } else {
                // failed to update details
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product updated
        pDialog.dismiss();
    }
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

}

1 个答案:

答案 0 :(得分:2)

SessionManager session对象为空。它似乎是你忘记实例化的一些自定义对象。