0

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

William Pfaffe
  • 305
  • 4
  • 16
  • 1
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Apr 08 '16 at 23:21

1 Answers1

2

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

Thomas
  • 2,375
  • 2
  • 17
  • 32
  • Yep that's it you are creating two instances of DBHandler.If you want to persist the data and retrieve it throught the setter/getter,please use the same object where you have setted the value.Also,try to read some basic concept of object oriented programming. Brushup your understanding on object creation. – Manish Singh Apr 08 '16 at 18:14
  • Could you share a example of this? How would I be able to use the same instance, across files? – William Pfaffe Apr 08 '16 at 18:17
  • 1
    Possibility 1: change `public StudentScreen(){` to `public StudentScreen(DBHandler db){` and work with that instance. When calling said constructor, you have to pass the DBHandler like this: `...new StudentScreen(db)` Possibility 2: use the singleton pattern and access it from a static context. I would suggest the first approach. – Thomas Apr 08 '16 at 18:18
  • I am not too sure what you mean by new StudentScreen(db). I tried to remake this, and I got an error by: StudentScreen db = new StudentScreen(db);, saying that StudentScreen cannot be converted into DBHandler. Also, in the JFrame there is a runable command, where it creates this: public void run() { new StudentScreen().setVisible(true); } And it's giving an error saying: http://i.imgur.com/CvkaDE4.png½1 – William Pfaffe Apr 08 '16 at 19:57
  • Your method 'public StudentScreen(){' is your constructor. When you create an object with the new-keyword, this method will get called. When you write new StudentScreen() the corresponding constructor without a parameter will be called. However when you write another constructor, taking an DBHandler as argument, you can pass that to the other screen. With your statement you are trying to pass a StudentScreen to a StudentScreen but what you want is passing the DBHelper, so you have to create said constructor inside StudentScreen (or via set-method) – Thomas Apr 08 '16 at 21:04