I am trying to create a login page. My research has given me the following code, but I keep getting a java.lang.NullPointerException at line 29. I have tried to research the answer myself but have just ended up very confused. Can someone help me understand what I am missing please?
LOGINTESTERACTIVITY class
package com.b00512756.Logintester;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LogintesterActivity extends Activity {
EditText txtUserName;
EditText txtPassword;
Button btnLogin;
Button btnCancel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtUserName=(EditText)this.findViewById(R.id.txtUname);
txtPassword=(EditText)this.findViewById(R.id.txtPwd);
btnLogin=(Button)this.findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){
Toast.makeText(LogintesterActivity.this, "Login Successful",Toast.LENGTH_LONG).show();
} else{
Toast.makeText(LogintesterActivity.this, "Invalid Login",Toast.LENGTH_LONG).show();
}
}
});
}
}
main.xml
<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:text="User Name: "
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText
android:text=""
android:id="@+id/txtUname"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
</TableRow>
<TableRow>
<TextView
android:text="Password: "
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText
android:text=""
android:id="@+id/txtPwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true">
</EditText>
</TableRow>
<TableRow>
<Button
android:text="Cancel"
android:id="@+id/btnCancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Login"
android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</TableRow>
</TableLayout>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.b00512756.Logintester"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".LogintesterActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Error Log:
04-29 15:36:41.570: WARN/dalvikvm(690): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): FATAL EXCEPTION: main
04-29 15:36:41.590: ERROR/AndroidRuntime(690): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.b00512756.Logintester/com.b00512756.Logintester.LogintesterActivity}: java.lang.NullPointerException
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.os.Looper.loop(Looper.java:123)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at java.lang.reflect.Method.invoke(Method.java:521)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at dalvik.system.NativeStart.main(Native Method)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): Caused by: java.lang.NullPointerException
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at com.b00512756.Logintester.LogintesterActivity.onCreate(LogintesterActivity.java:29)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-29 15:36:41.590: ERROR/AndroidRuntime(690): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
Many thanks,
Richy
Run it in a debugger. Set a breakpoint at line 29, and inspect the values of your variables at that point. Work out which one is null that shouldn’t be, then look back to where it was set, and try to work out why it might have been set to null.
Also, try commenting out line 29, and see what happens.
My suspicion is your layout isn’t loading correctly for some reason, but I’m not sure what that reason would be. Might be helpful for you to see what it looks like.