java将一个类中字符串传入另一个监视器类中总是出现null空指针错误,求大神帮忙看一下,代码如下
UpdateListen.java(一个监视器类)publicclassUpdateListeneimplementsActionListener{Stringtext_...
UpdateListen.java(一个监视器类)
public class UpdateListene implements ActionListener{
String text_id;
JTextField text_bookname;
String inputid,inputbookname;
public void setid(String inputid2){
this.text_id=inputid2;}
public void setbookname(JTextField t){
text_bookname=t;}
public void actionPerformed(ActionEvent e) {
System.out.println(text_id);
inputbookname = text_bookname.getText().toString().trim();
System.out.println(text_id);
System.out.println(inputbookname);
ModifyTable modify= new ModifyTable();
modify.setDatasourceName("myData");
modify.setSQL("INSERT INTO myData..borrowRecord VALUES('"+inputid+"','"+inputbookname+"','')");
String backMess = null;
try {
backMess = modify.modifyRecord();
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println(backMess);
}
}
LoginListen.java主要代码:
public void actionPerformed(ActionEvent e) {
try{
con = DriverManager.getConnection("jdbc:odbc:myData","id","password");
sql = con.createStatement();
rs = sql.executeQuery("SELECT * FROM myData..table1 WHERE id IS NOT NULL");
while(rs.next()){
name = rs.getString(1);
pwd = rs.getString(2);
UpdateListene ul = new UpdateListene();
ul.setid(name);
if (jTextField1.getText().toString().trim().equals(name)
&& jPasswordField1.getText().toString().trim().equals(pwd)) {
JOptionPane.showMessageDialog(null,"登陆成功","消息对话框",JOptionPane.WARNING_MESSAGE);
FirstPage page = new FirstPage();
page.createPage();
break;}
windowBorrow.java
UpdateListene ul = new UpdateListene();
ul.setbookname(text);
我想得到输入文本框时的书的名字以及之前登陆时的用户名,可是测试时那个inputid和inputbookname总是获取不到,一直都是null 展开
public class UpdateListene implements ActionListener{
String text_id;
JTextField text_bookname;
String inputid,inputbookname;
public void setid(String inputid2){
this.text_id=inputid2;}
public void setbookname(JTextField t){
text_bookname=t;}
public void actionPerformed(ActionEvent e) {
System.out.println(text_id);
inputbookname = text_bookname.getText().toString().trim();
System.out.println(text_id);
System.out.println(inputbookname);
ModifyTable modify= new ModifyTable();
modify.setDatasourceName("myData");
modify.setSQL("INSERT INTO myData..borrowRecord VALUES('"+inputid+"','"+inputbookname+"','')");
String backMess = null;
try {
backMess = modify.modifyRecord();
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println(backMess);
}
}
LoginListen.java主要代码:
public void actionPerformed(ActionEvent e) {
try{
con = DriverManager.getConnection("jdbc:odbc:myData","id","password");
sql = con.createStatement();
rs = sql.executeQuery("SELECT * FROM myData..table1 WHERE id IS NOT NULL");
while(rs.next()){
name = rs.getString(1);
pwd = rs.getString(2);
UpdateListene ul = new UpdateListene();
ul.setid(name);
if (jTextField1.getText().toString().trim().equals(name)
&& jPasswordField1.getText().toString().trim().equals(pwd)) {
JOptionPane.showMessageDialog(null,"登陆成功","消息对话框",JOptionPane.WARNING_MESSAGE);
FirstPage page = new FirstPage();
page.createPage();
break;}
windowBorrow.java
UpdateListene ul = new UpdateListene();
ul.setbookname(text);
我想得到输入文本框时的书的名字以及之前登陆时的用户名,可是测试时那个inputid和inputbookname总是获取不到,一直都是null 展开
1个回答
2015-08-10
展开全部
debug出来看看到底是什么值
public void setbookname(JTextField t){
System.out.println(t==null?"t is null ": t.getText());
text_bookname=t;
}
BTW,这样写的事件,太乱了!界面应该和逻辑之间,松散一些。。。
public void setbookname(JTextField t){
System.out.println(t==null?"t is null ": t.getText());
text_bookname=t;
}
BTW,这样写的事件,太乱了!界面应该和逻辑之间,松散一些。。。
追问
我还是个新手(๑• . •๑)你来测试一下你的代码看看
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询