I am trying to close app if there is no Internet Connection Available. I know there is a question which I already exist but I did exactly same and it seems that my app works fine before registration but after I edit text in nickname field it throws null pointer error, if I do this on opening again after editing text Here take a look :
I have registered on my app and in drawer I have nickname which is editable so I did like this :
pname = (EditText) findViewById(R.id.pname);
profimg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show();
}
});
pname.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pname.setInputType(0x0000006);
pname.setCursorVisible(true);
}
});
pname.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
pname.setCursorVisible(false);
}
else{
pname.setCursorVisible(false);
}
}
});
pname.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event!=null && event.getKeyCode() != KeyEvent.KEYCODE_ENTER || actionId != EditorInfo.IME_ACTION_DONE ) {
return false;
}
else if(actionId==EditorInfo.IME_ACTION_DONE || event==null || event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
pname.setCursorVisible(false);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(pname.getWindowToken(), 0);
return false;
}
return false;
}
});
pname.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
sp.edit().remove("personName").apply();
name= s.toString();
sp.edit().putString("personName",name).apply();
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("nickname", name));
params.add(new BasicNameValuePair("mobile", callNo));
JSONObject json = jsonParser.makeHttpRequest(Config.URL_Info, "POST",
params);
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Status Updated", json.getString(TAG_MESSAGE));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
if(path1) {
String value = sp.getString("personName", name);
pname.setText(value);
}
The error is where I am setting the text. I am checking for connection availability at the starting after setcontentview(layout) and the function is just after onCreate method.
Error Log :
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONObject.getInt(java.lang.String)' on a null object reference
at .afterTextChanged(PlaceCallActivity.java:206)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7942)
at android.widget.TextView.setText(TextView.java:4079)
at android.widget.TextView.setText(TextView.java:3928)
at