2
我正在嘗試將我的android應用程序連接到MS SQL Server 2008.我正在使用Android Studio。這是我的MainActivity.java
文件:如何解決android studio中的NullPointerException錯誤?
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class MainActivity extends ActionBarActivity {
Button add;
TextView errorlbl;
EditText name, address, pincode;
Connection connect;
PreparedStatement preparedStatement;
Statement st;
String ipaddress, db, username, password;
@SuppressLint("NewApi")
private Connection ConnectionHelper(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 + ";"
+ "databaseName=" + database + ";user=" + user
+ ";password=" + password + ";";
connection = DriverManager.getConnection(ConnectionURL);
} catch (SQLException se) {
Log.e("ERRO", se.getMessage());
} catch (ClassNotFoundException e) {
Log.e("ERRO", e.getMessage());
} catch (Exception e) {
Log.e("ERRO", e.getMessage());
}
return connection;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = (Button) findViewById(R.id.btnadd);
errorlbl = (TextView) findViewById(R.id.lblerror);
name = (EditText) findViewById(R.id.txtname);
address = (EditText) findViewById(R.id.txtaddress);
pincode = (EditText) findViewById(R.id.txtpincode);
ipaddress = "192.168.0.9";
db = "mydatabase";
username = "myusername";
password = "mypassword";
connect = ConnectionHelper(username, password, db, ipaddress);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
connect = ConnectionHelper(username, password, db, ipaddress);
st = connect.createStatement();
preparedStatement = connect
.prepareStatement("insert into studentRecord(Name,Address,Pincode) values ('"
+ name.getText().toString()
+ "','"
+ address.getText().toString()
+ "','"
+ pincode.getText().toString() + "')");
preparedStatement.executeUpdate();
errorlbl.setText("Data Added successfully");
} catch (SQLException e) {
errorlbl.setText(e.getMessage().toString());
}
}
});
}
}
但我發現了以下錯誤:
java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference at app.mysqlapp.MainActivity$1.onClick(MainActivity.java:77)
錯誤指向下面的代碼
st = connect.createStatement();
如何我解決這個問題?
我已經包含了jtds-1.2.7 jar文件,並且我已經正確設置了清單文件。
我是Android開發的初學者。請幫我解決這個問題。謝謝!