manikanta g manikanta g - 4 months ago 69
Android Question

NullPointerException: Attempt to invoke virtual method AlertDialog.setTitle(java.lang.CharSequence) on a null object reference

Error


04-01 10:37:47.077 2310-2326/zonup.asyc D/Create Response﹕
{"success":"false","msg":"Please enter email!!!"} 04-01 10:37:47.084

2310-2310/zonup.asyc D/AndroidRuntime﹕ Shutting down VM 04-01
10:37:47.084 2310-2310/zonup.asyc E/AndroidRuntime﹕ FATAL
EXCEPTION: main
Process: zonup.asyc, PID: 2310
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.AlertDialog.setTitle(java.lang.CharSequence)' on a
null object reference
at zonup.asyc.MainActivity$SignmeUp.onPostExecute(MainActivity.java:122)
at zonup.asyc.MainActivity$SignmeUp.onPostExecute(MainActivity.java:70)
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:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

public class MainActivity extends ActionBarActivity {


protected String stmail;
protected EditText editText;
protected Button button;
AlertDialog alertDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText) findViewById(R.id.editText);
stmail=editText.getText().toString();
button=(Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new SignmeUp().execute();
}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
class SignmeUp extends AsyncTask<String, String, String> {

private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();

@Override
protected void onPreExecute() {
super.onPreExecute();
runOnUiThread(new Runnable() {
@Override
public void run() {
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Thanks for your Sign up...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
}
});
}

@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub

// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", stmail));



// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest("http://api.php","GET", params);

// check log cat fro response
Log.d("Create Response", json.toString());

// check for success tag
try {
String success = json.getString("success");

if (stmail != null & success == "true") {
Toast.makeText(MainActivity.this, "Youre Email Posted..", Toast.LENGTH_SHORT).show();
} else {

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

return null;
}

@SuppressWarnings("deprecation")
protected void onPostExecute(String file_url) {
// dismiss the dialog once done

editText.setText("");
alertDialog.setTitle("Info");

alertDialog.setMessage("You have been subscribed o the ");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions

}
});

alertDialog.show();

}

}


}

Answer

Rather using AlertDialog use AlertDialog.Builder. As AlertDialog is deprected. Also you need to show progressdialog too.

 @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Thanks for your Sign up...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

In onPostExecute method dismiss the progressbar. Also Use AlertDialog.Builder.

 protected void onPostExecute(String file_url) {
        // dismiss the dialog once done

     if(pDialog.isShowing()){
       pDialog.dismiss();
      }
        editText.setText("");
      AlertDailog.Builder builder = new AlertDialog.Builder(Activity.this);
             builder.setText("");
             builder.setMessege("");
             builder.create().show();
        });