Java 语句=conn.createStatement()上的nullpointer异常;
我在android Aware中使用jdbc不推荐 我有以下代码:Java 语句=conn.createStatement()上的nullpointer异常;,java,android,Java,Android,我在android Aware中使用jdbc不推荐 我有以下代码: public int CheckUser(String uName,String password) { try { String sql="select uid from UserMaster where username='"+uName+"' and password='"+password+"'";
public int CheckUser(String uName,String password)
{
try {
String sql="select uid from UserMaster where username='"+uName+"' and password='"+password+"'";
statement = conn.createStatement();
resultSet = statement.executeQuery(sql);
int value=0;
if (resultSet.next()) {
value=resultSet.getInt(1);
}
return value;
}
catch (Exception e)
{
e.printStackTrace();
return 0;
}
}
我在statement=conn.createStatement上获取空指针异常
日志:
请帮忙
我尝试在本地声明语句对象,但没有效果,错误仍然存在
调用此函数的我的文件的代码:
public class Login extends Activity {
public static String resp = "";
public static String name = "";
String username;
String password;
EditText etLoginID;
EditText etPassword;
final DatabaseHandler db = new DatabaseHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
List<Contact> contacts = db.getAllContacts();
if(contacts.size() != 0){
for (Contact cn : contacts){
resp = cn.getID();
name = cn.getName();
}
// Intent i=new Intent(getApplicationContext(),Message.class);
//startActivity(i);
}
etLoginID=(EditText)findViewById(R.id.etName);
etPassword=(EditText)findViewById(R.id.etPassword);
Button btnLogin =(Button)findViewById(R.id.btnLogin);
final gaSQLConnect con=new gaSQLConnect();
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
con.setConnection("AndroidDB", "sa", "ok");
username=etLoginID.getText().toString();
password=etPassword.getText().toString();
String[] values=new String[2];
values[0]=username;
values[1]=password;
UserLogin ul=new UserLogin();
ul.execute(values);
//Intent i= new Intent(getApplicationContext(),Messages.class);
//startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
private class UserLogin extends AsyncTask<String[], Void, String>{
final gaSQLConnect con=new gaSQLConnect();
String[] values;
@Override
protected String doInBackground(String[]... params) {
// TODO Auto-generated method stub
values=params[0];
String result=null;
int uid;
db.deleteContact(new Contact(resp));
if (!values[0].isEmpty() && !values[1].isEmpty())
{
uid=con.CheckUser(etLoginID.getText().toString(), etPassword.getText().toString());
return Integer.toString(uid) ;
}
else{
uid=0;;
}
return Integer.toString(uid) ;
}
protected void onPostExecute(String result){
Toast.makeText(getApplicationContext(),result, Toast.LENGTH_LONG).show();
db.addContact(new Contact(result, values[0]));
Intent i=new Intent(getApplicationContext(),Messages.class);
startActivity(i);
}
}
由于异常跟踪表明您的conn对象为null,因此对其调用任何方法都将导致NullPointerException。您需要在初始化conn对象的位置检查代码,以正确初始化它。此外,进行空检查以避免空指针异常也是一种很好的做法 您的数据库连接为空。但是您正在尝试创建一个语句。所以只有你得到了nullPointerException
然后,当您试图创建连接不能为null的语句时,请继续执行该语句,如果该语句为null,则必须对此进行检查
if(conn!=null){
//Create statement
{
您的问题是在一个gaSQLConnect对象上调用setConnection,然后在另一个gaSQLConnect对象上调用CheckUser,该对象具有conn变量的不同副本。停止创建这么多gaSQLConnect对象。仅使用一次连接创建。
如果您试图通过任何int值来成功传递完整连接而不是连接对象,则将出现此错误。这是您可能希望的最明显的异常。如果不清楚为什么会在这里出现NullPointerException,以及为什么移动语句声明没有效果,那么您需要学习基础知识。您应该调查conn为null的原因。在运行这段代码之前,您需要创建一个连接对象-您无法做到这一点。还不清楚您是否正在尝试创建连接。如果您正在尝试,请在您尝试的地方发布代码,这样我们可以帮助您确定为什么没有创建连接。@DavidWallace先生,请检查我的编辑好吗?那么您实际上是在任何地方调用setConnection吗?是,我在loginpage的onClick中调用它,也在login页面中调用checkuser方法。@您正在setConnection方法中设置连接属性,这是确定的。但是您需要有一个getConnection方法来返回conn对象。还要确保setConnection只调用一次,以后您只需使用getConnection方法在其他位置获取连接。
public class gaSQLConnect
{
String url ="";
Connection conn=null;
Statement statement=null;
ResultSet resultSet=null;
public ResultSet getMessages(int uid)
{
try
{
String sql="select mid,uid,message,rstamp from MessagesMaster where uid='"+uid+"' aand rstamp = 0 order by tstamp desc";
statement = conn.createStatement();
resultSet = statement.executeQuery(sql);
return resultSet;
}
catch(Exception ex)
{
}
return resultSet;
}
public void setConnection(String DBName,String UserName,String Password)
{
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
url ="jdbc:jtds:sqlserver://10.0.2.2:1433;instanceName=14GRAFICALI\\MSSQLSERVER2008;DatabaseName="+DBName+";integratedSecurity=true;user="+UserName+";password="+Password+"";
conn =DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
}
int recFound=0;
public int CheckUser(String uName,String password)
{
try {
Statement statement=null;
ResultSet resultSet=null;
String sql="select uid from UserMaster where username='"+uName+"' and password='"+password+"'";
statement = conn.createStatement();
resultSet = statement.executeQuery(sql);
int value=0;
if (resultSet.next()) {
value=resultSet.getInt(1);
}
return value;
/* if (value > 0) {
recFound = 1;
} else {
recFound = 0;
}
if (recFound > 0)
{
return true;
} else {
return false;
}*/
}
catch (Exception e)
{
e.printStackTrace();
return 0;
}
}
public void UpdateMessage(String[] mlst) {
// TODO Auto-generated method stub
try
{
String sql="update messagesmaster set rstamp = 1 where mid in (" + mlst + ") ";
statement = conn.createStatement();
statement.executeUpdate(sql);
}
catch(Exception ex)
{
}
}
}
if(conn != null)
if(conn!=null){
//Create statement
{