Getting NullPointerException while displaying in edittext Android

时间:2015-06-15 15:01:28

标签: android nullpointerexception

I'm getting NullPointerException while consuming Soap response and printing on Activity.I saw several posts regardinng this error but nothing served my purpose. Here is my Activity code:

public class StatsActivity extends Activity {

        EditText Comm, trans, cards, shops, active, inactive;
        private String totcomm;
        private String tot_trans;
        private String Totcards;
        private String totshops;
        private String totactive;
        private String totinactive;

        SoapObject response = null;
        WSInterface wsInterface = new WebService();

        @Override


protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Comm = (EditText) findViewById(R.id.Comm);
            trans = (EditText) findViewById(R.id.trans);
            cards = (EditText) findViewById(R.id.cards);
            shops = (EditText) findViewById(R.id.shops);
            active = (EditText) findViewById(R.id.active);
            inactive = (EditText) findViewById(R.id.inactive);

            bundle = getIntent().getExtras();


            new SoapAccessTask().execute();
        }

        public class SoapAccessTask extends AsyncTask<String,SoapObject , SoapObject> {


            @TargetApi(Build.VERSION_CODES.GINGERBREAD)
            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                dist = bundle.getString("district");

                builder = new AlertDialog.Builder(StatsActivity.this);
                progressDialog = new ProgressDialog(StatsActivity.this);
                progressDialog.setMessage("Processing Data...");
                progressDialog.setCancelable(false);
                progressDialog.setTitle("Please Wait");

                progressDialog.show();


            }

            @Override
            protected SoapObject doInBackground(String... params) {
                response = wsInterface.getTodayStats(dist);
                return response;
            }

            @Override
            protected void onPostExecute(SoapObject soapObject) {
                super.onPostExecute(soapObject);
                System.out.println("in post execute" + soapObject.toString());
              // got soap response here
                        progressDialog.dismiss();
                    }
                        Totcards=j.getProperty(0).toString();
                        totactive=j.getProperty(1).toString();
                        totcomm=j.getProperty(2).toString();
                        totinactive=j.getProperty(3).toString();
                        totshops=j.getProperty(4).toString();
                        tot_trans=j.getProperty(5).toString();

                    }

Till here it is working... but after this line I get null pointer exception

            cards.setText(totactive);
            Comm.setText(totcomm);
            inactive.setText(totinactive);
            shops.setText(totshops);
            trans.setText(tot_trans);


        }else {
        builder.setMessage("Could not reach the Server. Please try again.").setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent mainActivity = new Intent(getBaseContext(),MainActivity.class);
                startActivity(mainActivity);
                StatsActivity.this.finish();
            }
        }).create().show();
    }

    }
}

I'm getting exactly at the line where I'm setting values in layout. Would anybody help me out to get rid of this? This is the error log

06-15 13:46:07.094    1435-1435/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.nic.eposapp, PID: 1435
    java.lang.NullPointerException
            at com.example.nic.eposapp.StatsActivity$SoapAccessTask.onPostExecute(StatsActivity.java:140)
            at com.example.nic.eposapp.StatsActivity$SoapAccessTask.onPostExecute(StatsActivity.java:76)
            at android.os.AsyncTask.finish(AsyncTask.java:632)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

错误在这里..

的setContentView(R.layout.activity_main);

它应该是我们设置值的布局(在我的情况下是statsactivity)。因为我是一个新手......这可能是@selvin的一个愚蠢的错误。 但它不应该标记nullpointer异常,如果它标记了相关的xml布局错误可能会更好