0

I'm trying to adapt two tutorial, to learn Volley.

I have error with showing a progress dialog.

This is my code:

public class MainActivity extends Activity {

    final static String MARS_WEATHER_RECENT = "http://marsweather.ingenology.com/v1/latest/";
    private static String TAG = MainActivity.class.getSimpleName();

    // Progress dialog
    private ProgressDialog pDialog;

    private TextView debug_container;
    private TextView degrees;

    // temporary string to show the parsed response
    private String jsonResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        debug_container = (TextView) findViewById(R.id.debug_container);
        degrees = (TextView) findViewById(R.id.degrees);

        loadMarsWeatherData();

        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);

    }

    private void loadMarsWeatherData() {

        showpDialog();
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                MARS_WEATHER_RECENT, (String)null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());

                try {
                    // Parsing json object response
                    // response will be a json object
                    response = response.getJSONObject("report");
                    String min_degree = response.getString("min_temp");

                    degrees.setText(min_degree);

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
                hidepDialog();
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                // hide the progress dialog
                hidepDialog();
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq);
    }





    private void showpDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hidepDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
}

Error is

    [...]
    FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.webtemplum.crazyweather/com.webtemplum.crazyweather.MainActivity}: java.lang.NullPointerException
[...]
used by: java.lang.NullPointerException
            at com.webtemplum.crazyweather.MainActivity.showpDialog(MainActivity.java:222)
            at com.webtemplum.crazyweather.MainActivity.loadMarsWeatherData(MainActivity.java:56)
            at com.webtemplum.crazyweather.MainActivity.onCreate(MainActivity.java:46)

(Stripped out some error line, If I understood right the error is in showpDialog).

Of course, If I comment the showpDialog app runs.

Same thing (app runs) if I comment all block in loadWeatherData and leave only

private void loadMarsWeatherData() {

        showpDialog();
        hidepDialog();
}

Thank you very much.

sineverba
  • 5,059
  • 7
  • 39
  • 84

2 Answers2

3

Look at this segment of code:

loadMarsWeatherData();

pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);

You're calling loadMarsWeatherData before instantiating pDialog. However, in loadMarsWeatherData you're calling pDialog:

showpDialog();

will call:

private void showpDialog() {
    if (!pDialog.isShowing())
        pDialog.show();
}

You need to change the order - first instantiate pDialog and then call the method:

pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);

loadMarsWeatherData();
Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
1

You are calling loadMarsWeatherData() before declaring the Progress Dialog which returns a null. Just put the loadMarsWeatherData() after declairing the Progress Dialog.

 pDialog = new ProgressDialog(this);
 pDialog.setMessage("Please wait...");
 pDialog.setCancelable(false);

 loadMarsWeatherData();

And you are all done!

Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50