rAyso7 rAyso7 - 1 month ago 5
Android Question

Android Caused by: java.lang.NullPointerException

I Edited it now...I'm check the layout and the initialization but still the same error..is it maybe eclipse error?

public class ActivityOwner extends Activity implements OnClickListener {

EditText ownerUser, ownerPass;
private Button btnLogin;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();

private static final String OWNER_LOGIN_URL = "http://192.168.2.5/idot/owner_login.php";

// ---JSON element ids from response of php script
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";

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

ownerUser = (EditText) findViewById(R.id.owner_login_username);
ownerPass = (EditText) findViewById(R.id.owner_login_password);

TextView tvLinkToRegister = (TextView) findViewById(R.id.link_to_register);
tvLinkToRegister.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
Intent registrationForm = new Intent(ActivityOwner.this,
ActivityOwnerRegister.class);
startActivity(registrationForm);
}
});

btnLogin = (Button) findViewById(R.id.btnOwnerLogin);
btnLogin.setOnClickListener(this);
}

@Override
public void onClick(View v) {
new AttemptLogin().execute();
}

class AttemptLogin extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ActivityOwner.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

@Override
protected String doInBackground(String... args) {

// Check for success tag
int success;
String username = ownerUser.getText().toString();
String password = ownerPass.getText().toString();

try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("owner_username", username));
params.add(new BasicNameValuePair("owner_password", password));

Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(OWNER_LOGIN_URL,
"POST", params);

// check your log for json response
Log.d("Login attempt", json.toString());

// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
// save user data
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(ActivityOwner.this);
Editor edit = sp.edit();
edit.putString("owner_username", username);
edit.commit();

Intent i = new Intent(ActivityOwner.this,
ActivityOwnerSuccessLogin.class);
pDialog.dismiss();
finish();
startActivity(i);

return json.getString(TAG_MESSAGE);
} else {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String result) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (result != null) {
Toast.makeText(ActivityOwner.this, result, Toast.LENGTH_SHORT)
.show();
}
}

}
}


------LOGCAT-------

11-14 21:30:52.517: E/AndroidRuntime(18929): Caused by: java.lang.NullPointerException
11-14 21:30:52.517: E/AndroidRuntime(18929): at ph.idot.owner.ActivityOwner$AttemptLogin.doInBackground(ActivityOwner.java:88)

Answer

A null pointer exception on a line of the form A.B.C means that something on the left-hand side of a dot is null. In this example :

  • either A is null;
  • or A.B is null.

There can be no other reason. So, as textView.getText() never returns null, the only possibility is that user is null.