I’m trying to implement a Log In system from my Android application to a database stored in MS SQL Server 2008. Connection to the server works, the problem comes when I call logIn method for users to connect on connection helper class to compare password matching.
This is the function that is executed when log in button is clicked:
public void onLogInButtonClicked(View v){
final EditText login = (EditText) findViewById(R.id.login);
final EditText password = (EditText) findViewById(R.id.password);
String userName = login.getText().toString();
String pass = password.getText().toString();
System.out.println("Pass Entered:" + pass);
System.out.println("User Entered" + userName);
if (connector.logIn(userName, pass)){
//Creates Intent from new Activity to be launched
Intent k = new Intent(this, MainActivity.class);
//Sends login name to activity k
k.putExtra("loginName", userName);
//Starts new Activity
startActivity(k);
}
else
{
//toast log in unsuccessful
}
}
logIn method on Connector class is as follows:
boolean logIn(String name, String password){
System.out.println("I'm in LogIn method");
java.sql.ResultSet result = null;
boolean log = false;
try {
connection = this.getConnection();
if (connection != null) {
//String for the login query
String statement = "SELECT Password FROM Users WHERE UserName = '"
+ name + "'";
System.out.println(statement);
Statement select = connection.createStatement();
result = select.executeQuery(statement);
System.out.println(result.getString(1));
if(result.getString(1) == password) log = true;
result.close();
result = null;
closeConnection();
}
else {
System.out.println("Error: No active Connection");
return log;
}
} catch (Exception e) {
e.printStackTrace();
}
return log;
}
I don’t even get to see the “I’m in logIn method” in the console, which makes me guess the app crashes before getting into it, although the crash occurs in the line where the method is called.
Here is the log:
04-25 23:32:45.855: E/AndroidRuntime(795): FATAL EXCEPTION: main
04-25 23:32:45.855: E/AndroidRuntime(795): java.lang.IllegalStateException: Could not execute method of the activity
04-25 23:32:45.855: E/AndroidRuntime(795): at android.view.View$1.onClick(View.java:2144)
04-25 23:32:45.855: E/AndroidRuntime(795): at android.view.View.performClick(View.java:2485)
04-25 23:32:45.855: E/AndroidRuntime(795): at android.view.View$PerformClick.run(View.java:9080)
04-25 23:32:45.855: E/AndroidRuntime(795): at android.os.Handler.handleCallback(Handler.java:587)
04-25 23:32:45.855: E/AndroidRuntime(795): at android.os.Handler.dispatchMessage(Handler.java:92)
04-25 23:32:45.855: E/AndroidRuntime(795): at android.os.Looper.loop(Looper.java:123)
04-25 23:32:45.855: E/AndroidRuntime(795): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-25 23:32:45.855: E/AndroidRuntime(795): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 23:32:45.855: E/AndroidRuntime(795): at java.lang.reflect.Method.invoke(Method.java:507)
04-25 23:32:45.855: E/AndroidRuntime(795): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-25 23:32:45.855: E/AndroidRuntime(795): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-25 23:32:45.855: E/AndroidRuntime(795): at dalvik.system.NativeStart.main(Native Method)
04-25 23:32:45.855: E/AndroidRuntime(795): Caused by: java.lang.reflect.InvocationTargetException
04-25 23:32:45.855: E/AndroidRuntime(795): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 23:32:45.855: E/AndroidRuntime(795): at java.lang.reflect.Method.invoke(Method.java:507)
04-25 23:32:45.855: E/AndroidRuntime(795): at android.view.View$1.onClick(View.java:2139)
04-25 23:32:45.855: E/AndroidRuntime(795): ... 11 more
04-25 23:32:45.855: E/AndroidRuntime(795): Caused by: java.lang.NullPointerException
Thanks in advance!
Sounds like your connector object is null. Did you forget to initialize it or scope it incorrectly? Post up where connector is initialized if that wasn’t enough to help you find it