-3

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)
Alex P.
  • 30,437
  • 17
  • 118
  • 169
sanjana
  • 9
  • 7
  • Did you mean this: Comm.setText(totcomm); ? – blueware Jun 15 '15 at 15:06
  • print your stack trace – Krupal Shah Jun 15 '15 at 15:07
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Jun 15 '15 at 15:12
  • use your brain(or at least, try to) ... assumption: shop is null(with debbuger you can be sure) ... question why? never asigned - not true ... obviously , `findViewById(R.id.shops)` returns `null` ... why ... the only cause can be: there is no such view on given layout ... solved ... also asked bazillion times [example](http://stackoverflow.com/questions/30842769/app-crashes-when-opening-please-help-me) – Selvin Jun 15 '15 at 15:16
  • @Selvin, If your comment answers this question, please post it as answer. Thanks for helping others – blueware Jun 15 '15 at 15:21
  • debug or print "totactive" value ... it could be null – ASP Jun 15 '15 at 15:54
  • @ASP and? https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/TextView.java#L3923 `TextView.setText(null)` is valid(will not throw an exception) ... so (obviously) `totactive` can be null – Selvin Jun 15 '15 at 15:56
  • Dear selvin,i printed all values nothing is shown null...all are getting printed.......i'm getting error only while setting edit text values. – sanjana Jun 15 '15 at 17:58

1 Answers1

0

The error is here..

setContentView(R.layout.activity_main);

it should be the layout(in my case statsactivity) where we are setting values.Since i'm a newbie ...it might be a stupid mistake assaid by @selvin. But it should not flag nullpointer exception it could have been better had it flagged relevant xml layout error

sanjana
  • 9
  • 7