Ozon
Ozon

Reputation: 13

Android ProgressDialog returned null

I am trying to make a progress Dialog when catching data using JSON. but it always returned a null. Here is the code :

class NetWork extends AsyncTask<Void, Void, ArrayAdapter<?>> {

        private ProgressDialog pd;

        public void Network() {
            try {
                ServiceStations service = new ServiceStations();
                service.load(WorkshopActivity.this.getApplicationContext());
            } catch (IOException e) {
                // do nothing
                e.printStackTrace();
            } catch (JSONException e) {
                // do nothing
                e.printStackTrace();
            }
            pd = new ProgressDialog(WorkshopActivity.this);
            pd.setMessage("Load");
            pd.setCancelable(false);
            pd.setCanceledOnTouchOutside(false);

        }

        @Override
        public ArrayAdapter<?> doInBackground(Void... params) {


            @SuppressWarnings({ "unchecked", "rawtypes" })
            final ArrayAdapter<?> areaAdapter = new ArrayAdapter(
                    WorkshopActivity.this, android.R.layout.simple_list_item_1,
                    ServiceStations.getAreaList());

            return areaAdapter;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            try {
                pd.show(); // this returns null
            } catch (NullPointerException e) {
                // TODO: handle exception
                e.printStackTrace();
            }

        }

        @Override
        public void onPostExecute(ArrayAdapter<?> areaAdapter) {
            /*
             * if (dialog.isShowing()) { dialog.dismiss(); }
             */
            list = (ListView) findViewById(R.id.listWorkshopCity);
            list.setAdapter(areaAdapter);
            adapter = areaAdapter;
        }

    }

here is the stacktrace :

07-15 04:34:15.006: W/System.err(3393): java.lang.NullPointerException
07-15 04:34:15.006: W/System.err(3393):     at com.jatismobile.iklaim.activities.WorkshopActivity$NetWork.onPreExecute(WorkshopActivity.java:74)
07-15 04:34:15.026: W/System.err(3393):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
07-15 04:34:15.026: W/System.err(3393):     at android.os.AsyncTask.execute(AsyncTask.java:534)
07-15 04:34:15.036: W/System.err(3393):     at com.jatismobile.iklaim.activities.WorkshopActivity.onCreate(WorkshopActivity.java:102)

I've been trying to search the internet, yet I have found the solution to this Any help will be apreciated. thanks..

Upvotes: 1

Views: 593

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

You have this

 public void Network()

Constructor does not have a return type

It should be

 public Network()
 {
      // rest of the code
 }    

You initialize your progressdialog in a method which is not called. You do not call method Network any where in your class. Hence NullPointerException

Upvotes: 2

Related Questions