While I am validating EditText for login I am Getting NullPointerException
public boolean onMenuItemClick( MenuItem item )
{
if(item.getTitle().equals("Login"))
{
final String Usname;
final String Pswd;
Status=(TextView)findViewById(R.id.tvStatus);
Uname=(EditText)findViewById(R.id.etLUsername);
Pwd=(EditText)findViewById(R.id.etLPassword);
LayoutInflater li = LayoutInflater.from(MainActivity.this);
View promptsView = li.inflate(R.layout.login, null);
//Usname=Uname.getText().toString();
//Pswd=Pwd.getText().toString();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
// set dialog message
alertDialogBuilder
.setCancelable(true)
.setPositiveButton("Login",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
if(Uname!=null&&Pwd!=null){
new Asynctask_Login().execute();
}
else if(Pwd!=null){
Status.setText("No Username");
Status.setVisibility(View.VISIBLE);
}
else if(Uname!=null){
Status.setText("No Password");
Status.setVisibility(View.VISIBLE);
}
else{
Status.setText("No Username and Password");
Status.setVisibility(View.VISIBLE);
}
}
})
.setNegativeButton("Register",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
try {
Intent tvi = new Intent("com.takebestloan.action.REGISTER");
startActivity(tvi);
} catch (Exception e) {
// TODO: handle exception
}
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.setView(promptsView,0,0,0,0);
alertDialog.show();
}else if(item.getItemId()==R.id.mRegister)
{
try {
Intent tvi = new Intent("com.takebestloan.action.REGISTER");
startActivity(tvi);
} catch (Exception e) {
// TODO: handle exception
}
}else if(item.getTitle().equals("Nearby ATM"))
{
//Toast.makeText( MainActivity.this, "Comming soon..", Toast.LENGTH_SHORT ).show();
try {
Intent in = new Intent(Intent.ACTION_VIEW,Uri.parse("https://maps.google.co.in/maps?q=atm&z=16"));
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK& Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
in.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
startActivity(in);
} catch (Exception e) {
// tm.showToast(R.string.tm_gmap_not_detected);
e.printStackTrace();
}
}
else if(item.getTitle().equals("Nearby Banks"))
{
//Toast.makeText( MainActivity.this, "Comming soon..", Toast.LENGTH_SHORT ).show();
try {
Intent in = new Intent(Intent.ACTION_VIEW,Uri.parse("https://maps.google.co.in/maps?q=bank&z=16"));
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK& Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
in.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
startActivity(in);
} catch (Exception e) {
// tm.showToast(R.string.tm_gmap_not_detected);
e.printStackTrace();
}
}
else if(item.getTitle().equals("Home Loans"))
{
try {
Intent tvi = new Intent(getApplicationContext(),HL.class);
startActivity(tvi);
} catch (Exception e) {
// TODO: handle exception
}
}
else if(item.getTitle().equals("Loan Against Property"))
{
try {
Intent tvi = new Intent(getApplicationContext(),LAP.class);
startActivity(tvi);
} catch (Exception e) {
// TODO: handle exception
}
}
else
{
Toast.makeText( MainActivity.this, "Romba NOT", Toast.LENGTH_SHORT ).show();
}
return true;
}
} );
menu.show();
}
});
I have tried all methods in Check if EditText is empty.
Logcat:
06-17 18:34:04.019: E/AndroidRuntime(24618): FATAL EXCEPTION: main
06-17 18:34:04.019: E/AndroidRuntime(24618): java.lang.NullPointerException
06-17 18:34:04.019: E/AndroidRuntime(24618): at com.takebestloan.IFSC.MainActivity$5$1.onMenuItemClick(MainActivity.java:241)
06-17 18:34:04.019: E/AndroidRuntime(24618): at com.takebestloan.mainMenu.HoneycombHelper$1.onMenuItemClick(HoneycombHelper.java:52)
06-17 18:34:04.019: E/AndroidRuntime(24618): at android.widget.PopupMenu.onMenuItemSelected(PopupMenu.java:142)
06-17 18:34:04.019: E/AndroidRuntime(24618): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
06-17 18:34:04.019: E/AndroidRuntime(24618): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
06-17 18:34:04.019: E/AndroidRuntime(24618): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
06-17 18:34:04.019: E/AndroidRuntime(24618): at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:156)
06-17 18:34:04.019: E/AndroidRuntime(24618): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
06-17 18:34:04.019: E/AndroidRuntime(24618): at android.widget.AbsListView.performItemClick(AbsListView.java:1394)
06-17 18:34:04.019: E/AndroidRuntime(24618): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3024)
06-17 18:34:04.019: E/AndroidRuntime(24618): at android.widget.AbsListView$1.run(AbsListView.java:3830)
06-17 18:34:04.019: E/AndroidRuntime(24618): at android.os.Handler.handleCallback(Handler.java:605)
06-17 18:34:04.019: E/AndroidRuntime(24618): at android.os.Handler.dispatchMessage(Handler.java:92)
06-17 18:34:04.019: E/AndroidRuntime(24618): at android.os.Looper.loop(Looper.java:137)
06-17 18:34:04.019: E/AndroidRuntime(24618): at android.app.ActivityThread.main(ActivityThread.java:4517)
06-17 18:34:04.019: E/AndroidRuntime(24618): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 18:34:04.019: E/AndroidRuntime(24618): at java.lang.reflect.Method.invoke(Method.java:511)
06-17 18:34:04.019: E/AndroidRuntime(24618): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
06-17 18:34:04.019: E/AndroidRuntime(24618): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
06-17 18:34:04.019: E/AndroidRuntime(24618): at dalvik.system.NativeStart.main(Native Method)
The error comes at line Usname=Uname.getText().toString();
and app force closes
There are two possibilities as to why you are getting a null pointer exception. Firstly
Uname=(EditText)findViewById(R.id.etLUsername);
may actually be assigning Uname with null if the viewedLUsername
can not be found.Secondly,
Uname.getText()
could be returning null, and so when you calltoString()
it causes a null pointer exception. You should add some error checking