简体   繁体   中英

Connecting to a SQL database in android studio using jtds-1.3.1 using an emulator

Good day coders. I fairly new to android studio. It is my first time connecting a Android application to SQL server. I've always used SQLite and it was never a problem.

Currently I am trying to connect to a SQL database using the jtds-1.3.1 with a emulator. My application just crashes without even a break point been used.

My logcat provides with me this:

--------- beginning of crash

08-22 06:00:31.278 3273-3273/com.example.drizzybass.skyetek_login_with_sqlserver E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.drizzybass.skyetek_login_with_sqlserver, PID: 3273
        java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.drizzybass.skyetek_login_with_sqlserver/com.example.drizzybass.skyetek_login_with_sqlserver.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.support.v7.app

To be honest I don't know where to start debugging my application without stepping through break points. I set break points from the Mainactivity to the onCreate method to no prevail.

My MainActivity code looks like this:

 EditText user = (EditText) findViewById(R.id.txtUserName);
    EditText password = (EditText) findViewById(R.id.txtPassword);
    Button btnLogin = (Button) findViewById(R.id.btnLogin);
    String strUser = user.getText().toString();
    String strPass = password.getText().toString();

    String username, pass, db, ip;
    Connection con;

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

        //Declaring server IP, username, database name and password
        ip = "xxx"; //xxx isnt the actually information
        db = "xxx";
        username = "xxx";
        pass = "xxx";

        btnLogin.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                CheckLogin checkLogin = new CheckLogin();
                checkLogin.execute("");

            }
        });
    }

    public class CheckLogin extends AsyncTask<String, String, String>
    {
        String x = "";

        Boolean isSuccess = false;//use to check whether the login fails or not

        protected String doInBackground(String... params)
        {
            if(strUser.trim().equals("") || strPass.trim().equals(""))
            {
                x = "Please enter Username and Password";
            }
            else
            {
                try
                {

                    con =  connectionclass(username, pass, db, ip);
                    if(con == null)
                    {
                        x = "Please check your internet connection";
                    }
                    else
                    {
                        String query = "select * from tblLogin where username = " + strUser.toString() + " and pass_word = " + strPass.toString();
                        Statement stat = con.createStatement();
                        ResultSet rs = stat.executeQuery(query);
                        if(rs.next())
                        {
                            x = "Login Successful";
                            isSuccess = true;
                            con.close();
                        }
                        else
                        {
                            x = "Invalid Credentials";
                            isSuccess = false;
                        }
                    }
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    x = ex.getMessage();
                }
            }
            return x;
        }

        public Connection connectionclass(String user, String password, String database, String server) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            Connection connection = null;
            String ConnectionURL = null;

            try {
                Class.forName("net.sourceforge.jtds.jdbc.Driver");
                ConnectionURL = "jdbc:jtds:sqlserver://" + server + database + ";user=" + user + ";password=" + password + ";";
                connection =  DriverManager.getConnection(ConnectionURL);

            }
            catch (SQLException ae)
            {
                Log.e("error here 1 : ", ae.getMessage());
            }
            catch (ClassNotFoundException e)
            {
                Log.e("error here 2 : ", e.getMessage());
            }
            catch (Exception ex)
            {
                Log.e("error here 3 : ", ex.getMessage());
            }
            return connection;

        }
    }

My build gradle module app looks like this:

    dependencies
            {
                compile fileTree(dir: 'libs', include: ['*.jar'])
                compile 'com.android.support:appcompat-v7:25.3.1'
                compile project(':jtds-1.3.1')
            } 

And lastly my design contains:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.drizzybass.skyetek_login_with_sqlserver.MainActivity">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="40sp">

            <ImageView
                android:src="@drawable/skyelogo"
                android:textAlignment="center"
                android:layout_weight="1"

                android:layout_height="92dp"

                android:layout_gravity="center_horizontal"
                />
        </TableRow>


        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <EditText
                android:id="@+id/txtUserName"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="40sp"
                android:layout_weight="1"
                android:gravity="center"
                android:hint="Username"
                android:inputType="textPersonName"
                android:text="" />
        </TableRow>



        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/txtPassword"
                android:layout_width="wrap_content"
                android:layout_weight="1"
                android:hint="Password"
                android:gravity="center"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:password="true"
                />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/btnLogin"
                android:onClick="btnLogin"
                android:layout_weight="1"
                android:elevation="0dp"
                android:text="LOGIN" />
        </TableRow>


        <TextView android:id="@+id/link_signup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="24dp"
            android:text="No account yet? Create one now"
            android:gravity="center"
            android:textSize="16dip"/>

    </TableLayout>

</RelativeLayout>

If anyone can help me with this it would be highly appreciated.

place

EditText user = (EditText) findViewById(R.id.txtUserName);
EditText password = (EditText) findViewById(R.id.txtPassword);
Button btnLogin = (Button) findViewById(R.id.btnLogin);
String strUser = user.getText().toString();
String strPass = password.getText().toString();

inside oncreate()

返回x后错过了括号添加}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM