gpt4 book ai didi

java - 语句上的空指针异常 = conn.createStatement();

转载 作者:行者123 更新时间:2023-12-01 06:35:14 25 4
gpt4 key购买 nike

我在android中使用jdbc(注意不推荐)

我有以下代码:

 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();

上遇到空指针异常

Logcat:

09-12 11:04:14.543: W/KeyCharacterMap(483): No keyboard for id 0
09-12 11:04:14.553: W/KeyCharacterMap(483): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
09-12 11:04:51.173: W/System.err(483): java.lang.NullPointerException
09-12 11:04:51.194: W/System.err(483): at com.example.messagesql.gaSQLConnect.CheckUser(gaSQLConnect.java:69)
09-12 11:04:51.213: W/System.err(483): at com.example.messagesql.Login$UserLogin.doInBackground(Login.java:109)
09-12 11:04:51.213: W/System.err(483): at com.example.messagesql.Login$UserLogin.doInBackground(Login.java:1)
09-12 11:04:51.225: W/System.err(483): at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-12 11:04:51.233: W/System.err(483): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
09-12 11:04:51.233: W/System.err(483): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
09-12 11:04:51.253: W/System.err(483): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
09-12 11:04:51.253: W/System.err(483): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
09-12 11:04:51.264: W/System.err(483): at java.lang.Thread.run(Thread.java:1019)

请帮忙。

我尝试在本地声明 Statement 对象,但没有效果,错误仍然存​​在。

我的文件调用此函数的代码:

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);

}
}

上面是我调用该函数的文件代码。

在这个 onClick 登录方法中,我设置了连接。

那么为什么它不能在整个应用程序中持续存在?

DBConnection类的完整代码:

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)
{

}
}
}

最佳答案

由于异常跟踪表明您的 conn 对象为 null,因此调用它的任何方法都会导致 NullPointerException。您需要检查初始化 conn 对象的代码以正确初始化它。此外,进行空检查以避免空指针异常始终是一个好习惯。

关于java - 语句上的空指针异常 = conn.createStatement();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18756159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com