I'm pretty new on java and android programming. I'm trying to connect an android application to a SQL Server database installed on a Windows laptop. I'm programming under Windows using ADT.I added the jtds-1.2.5.jar too The code is the following (placed in the MainActivity.java):
package com.test;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class MainActivity extends Activity {
ConnectionClass connectionClass;
EditText e1, e2;
Button btnlogin;
TextView t1, t2;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectionClass = new ConnectionClass();
e1 = (EditText) findViewById(R.id.editText1);
e2 = (EditText) findViewById(R.id.editText2);
btnlogin = (Button) findViewById(R.id.button1);
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DoLogin doLogin = new DoLogin();
doLogin.execute("");
}
});
}
public class DoLogin extends AsyncTask<String, String, String> {
String z = "";
Boolean isSuccess = false;
String userid = e1.getText().toString();
String password = e2.getText().toString();
@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String r) {
progressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, r, Toast.LENGTH_SHORT).show();
if (isSuccess) {
Toast.makeText(MainActivity.this, "Login Successfull",
Toast.LENGTH_LONG).show();
// finish();
}
}
@Override
protected String doInBackground(String... params) {
if (userid.trim().equals("") || password.trim().equals(""))
z = "Please enter User Id and Password";
else {
try {
Connection con = connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
String query = "select * from LoginTable where username='"
+ userid + "' and password='" + password + "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs.next()) {
z = "Login successfull";
isSuccess = true;
} else {
z = "Invalid Credentials";
isSuccess = false;
}
}
} catch (Exception ex) {
isSuccess = false;
z = "Exceptions";
}
}
return z;
}
}
}
When I press the "connect to database" button I receive the following error message: Can someone help me with this?
09-05 11:32:42.505: E/AndroidRuntime(1402): FATAL EXCEPTION: main
09-05
11:32:42.505:E/AndroidRuntime(1402):java.lang.NullPointerException
09-05 11:32:42.505: E/AndroidRuntime(1402): at
com.test.MainActivity$DoLogin.onPreExecute(MainActivity.java:54)
09-05 11:32:42.505: E/AndroidRuntime(1402): at
android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
09-05 11:32:42.505: E/AndroidRuntime(1402): at
android.os.AsyncTask.execute(AsyncTask.java:534)
09-05 11:32:42.505: E/AndroidRuntime(1402): at
com.test.MainActivity$1.onClick(MainActivity.java:38)
09-05 11:32:42.505: E/AndroidRuntime(1402): at
android.view.View.performClick(View.java:4240)
09-05 11:32:42.505: E/AndroidRuntime(1402): at
android.view.View$PerformClick.run(View.java:17721)
09-05 11:32:42.505: E/AndroidRuntime(1402): at
android.os.Handler.handleCallback(Handler.java:730)
09-05 11:32:42.505: E/AndroidRuntime(1402): at
android.os.Handler.dispatchMessage(Handler.java:92)
09-05 11:32:42.505: E/AndroidRuntime(1402): at
android.os.Looper.loop(Looper.java:137)
09-05 11:32:42.505: E/AndroidRuntime(1402): at
android.app.ActivityThread.main(ActivityThread.java:5103)
09-05 11:32:42.505: E/AndroidRuntime(1402): at
java.lang.reflect.Method.invokeNative(Native Method)
09-05 11:32:42.505: E/AndroidRuntime(1402): at
java.lang.reflect.Method.invoke(Method.java:525)
09-05 11:32:42.505:
E/AndroidRuntime(1402):atcom.android.internal.os
.ZygoteInit$MethodAndArgsCaller
.run(ZygoteInit.java:737)
09-05 11:32:42.505: E/AndroidRuntime(1402): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-05 11:32:42.505: E/AndroidRuntime(1402): at
dalvik.system.NativeStart.main(Native Method)
09-05 11:32:46.115: I/Process(1402): Sending signal.
PID: 1402 SIG: 9
Thanks in advance
In DoLogin
class
@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
}
here you getting null pointer exception because you doesn't define progressBar
in the scope.
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.