Reputation: 325
Currently using Swing to create a GUI, for a login system. I retrieve a couple things from the database, one is a String named Username, and one is a int, named points. I have a class, where I have all my getters and setters in, which is called DBHandler. Upon retrieving these values, I use:
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
When I set these values (points is set inside the DBHandler.login method) using:
public login(){
DBHandler db = new DBHandler();
db.setUsername(usernameTemp.getText());
db.login();
}
However, when in another JFrame, that I call the instance of DBHandler, all values seem to be null. I use the getters to retrieve the values, but they are always empty.
public StudentScreen() {
DBHandler db = new DBHandler();
initComponents();
showUser.setText(db.getUsername());
showScore.setText("" + db.getPoints());
}
I know this is a fairly simple problem, but I just haven't been able to get past this. I've checked a couple posts, but nothing helped understanding this issue.
Best Regards
Upvotes: 0
Views: 68
Reputation: 2560
Inside StudentScreen you are creating a new instance of DBHandler which has all its values set to their default values. You have to use the same instance everywhere. You either achieve this with a singleton pattern or by just passing the DBHandler as a parameter to the constructor of StudentScreen
Upvotes: 2