我在将变量传递给另一个类时遇到问题,因为它不断传递给我一个空白变量。
我希望从方法id
获取search()
的值,并将其转移到方法class foo
内的total
...
班级Boo
:
//method inside of class boo
public void search(){
try{
String id = searchBox.getText();
String idNum="";
rs = stat.executeQuery("Select * from students where idNum='"+id+"'");
while(rs.next()){
idNum = rs.getString("idNum");
}//while
Members members = new Members();
setVisible(false);
members.setIdVal(id);
}catch(Exception e){
System.out.println("Error: "+e);
}
}//search
班级Foo
:
// methods inside Foo
public void total(){
System.out.println("Get: "+getIdVal());
try{
rs2 = stat2.executeQuery("SELECT * FROM paymentRecord where idNum ='"+getIdVal()+"';");
}catch(Exception e){
System.out.println("Total Error: "+e);
}//
}//total
public void setIdVal(String val){
this.val = val;
}//get the id
public String getIdVal(){
//System.out.println("Inputted ID:" + val);
return this.val;
}//get the id
答案 0 :(得分:0)
getIdVal()
是Foo
类的方法,但它应该是Boo
类的方法。
class Boo {
private String id;
public void search(){
// ...
this.id = searchBox.getText();
// ...
}
public int getIdVal() {
return this.id
}
}
Foo
课程:
Class Foo {
public string Total(){
Boo boo = new Boo();
System.out.println(boo.getIdVal()); // will print the value from id searched in search() method
}
}
您必须选择调用search()
方法的位置。