当我运行模拟器时,它完美地唤醒,没有错误,但是当我按下登录按钮时,我收到错误"java.lang.NullPointerException:尝试调用接口方法"。
这是我的代码:
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 = "92.244.93.250:3306";
db = "lokal";
username = "lokal";
password = "12lokal34";
connect = ConnectionHelper(username, password, db, ipaddress);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
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());
}
}
});
}
}
日志猫错误 说:
2182-2182/app.mysqlapp E/Android运行时:致命异常:main 进程: app.mysqlapp, PID: 2182 java.lang.NullPointerException:尝试在空对象引用上调用接口方法"java.sql.Statement java.sql.Connection.createStatement()'
您的ConnectionHelper
方法捕获异常,并在出现问题时返回null
。因此,在调用它时,由于已选择退出结构化异常处理,因此必须允许null
返回值。您没有这样做,您只是使用 ConnectionHelper
的返回值,而不首先检查它是否null
。
显然,ConnectionHelper
中发生了错误,您正在捕获并转储到日志中。所以这会告诉你出了什么问题。
旁注:Java 中压倒性的惯例是不将方法的第一个字母大写。
旁注 2:更好的做法是允许 ConnectionHelper
中的异常传播到顶级入口点并在那里处理它们。
,你的连接对象是空的,这意味着ConnectionHelper()返回null。这进一步暗示下面的行返回 null
connection = DriverManager.getConnection(ConnectionURL);
现在,仅当驱动程序名称错误或连接URL错误时,才会发生这种情况。由于驱动程序名称看起来正确,因此唯一出错的地方是连接 url 字符串。我刚刚在SO检查了这个类似的答案。
对我来说,它看起来像,Connetion URl 字符串应该是
ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
+ "database=" + database + ";user=" + user
+ ";password=" + password + ";";
请注意连接字符串中database
和databaseName
变量的差异。这应该有效。
更新 @T.J. Crowder的回答中提到的所有观点都是正确的,你应该养成遵循标准惯例的习惯。
从你的代码中可以清楚地看出,你的连接助手有问题,它返回空
值注意:这只是我的猜测
你已经使用了net.sourceforge.jtds.jdbc.Driver,这是用于MsSql服务器的,并且你已经给出了3306作为数据库服务器的端口,主要是3306用于MySql,所以你确定你已经在3306上配置了MsSql