0

我正在尝试实现从我的 Android 应用程序到存储在 MS SQL Server 2008 中的数据库的登录系统。与服务器的连接有效,当我调用 logIn 方法让用户连接连接帮助程序类以比较密码匹配时出现问题.

这是单击登录按钮时执行的函数:

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
        }
    }

Connector 类的 logIn 方法如下:

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;
    }

我什至没有在控制台中看到“我正在使用 logIn 方法”,这让我猜测应用程序在进入它之前就崩溃了,尽管崩溃发生在调用该方法的行中。

这是日志:

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

提前致谢!

4

1 回答 1

0

听起来您的连接器对象为空。您是否忘记对其进行初始化或范围不正确?如果还不足以帮助您找到连接器,请张贴初始化连接器的位置

于 2012-04-25T23:56:09.900 回答