0

I am trying to create a list of objects from a database but whenever I add another element to the end of the list it changes the values of the previous elements. I have seen similar questions on here but none seem to help my problem.

Here is the code for the class Deck

public static class Deck {
    private static int deckid;
    private static String deckName;
    private static int deckCreatorID;
    private static boolean isDeckPublic;
    private static String deckDescription;

    public Deck(int id, String name, int creator, boolean isPublic, String description){
        deckid = id;
        deckName = name;
        deckCreatorID = creator;
        isDeckPublic = isPublic;
        deckDescription = description;
    }

    public String getDeckName(){
        return deckName;
    }

    public String getDeckDescription(){
        return deckDescription;
    }

    public int getDeckCreator(){
        return deckCreatorID;
    }

    public int getDeckid() { return deckid; }

    public boolean getDeckPublic() { return isDeckPublic; }

}

Here is the code to create and add the objects to the list:

public static List<marketplaceController.Deck> getAllCards(){
    List<marketplaceController.Deck> deckList = new ArrayList<>();

    List<Integer> idList = new ArrayList<>();
    List<String> nameList = new ArrayList<>();
    List<Integer> cIdList = new ArrayList<>();
    List<Boolean> publicList = new ArrayList<>();
    List<String> descList = new ArrayList<>();

    try{ // This try-catch block is mainly from https://www.javatpoint.com/example-to-connect-to-the-mysql-database
        Class.forName("com.mysql.cj.jdbc.Driver"); // Define the driver to use
        Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/users","root","UnlockDB.123"); // Connect to the local db
        Statement stmt=con.createStatement();
        ResultSet rs=stmt.executeQuery("select * from decks"); // Execute the query on the db

        while(rs.next()) { // Runs while columns are to be store
            idList.add(rs.getInt(1));
            nameList.add(rs.getString(2));
            cIdList.add(rs.getInt(3));
            publicList.add(rs.getBoolean(4));
            descList.add(rs.getString(5));
        }

        con.close();
    }
    catch(Exception e){
        loginController.popup("An error occurred connecting to the database", "An error occurred");
    }

    for(int i = 0; i < idList.size(); ++i){ // This loop outputs the correct data
        deckList.add(new marketplaceController.Deck(idList.get(i), nameList.get(i), cIdList.get(i), publicList.get(i), descList.get(i)));
        System.out.println(deckList.get(i).getDeckid());
        System.out.println(deckList.get(i).getDeckName());
        System.out.println(deckList.get(i).getDeckCreator());
        System.out.println(deckList.get(i).getDeckPublic());
        System.out.println(deckList.get(i).getDeckDescription());
    }

    for(int i = 0; i < deckList.size(); ++i){ // This outputs the overwitten data
        System.out.println(deckList.get(i).getDeckid());
        System.out.println(deckList.get(i).getDeckName());
        System.out.println(deckList.get(i).getDeckCreator());
        System.out.println(deckList.get(i).getDeckPublic());
        System.out.println(deckList.get(i).getDeckDescription());
    }


    return deckList;
}

When the elements of deckList are printed straight after they are created they show the correct elements e.g.

1
TESTDECK
1
true
""

2
TESTDECK2
1
true
""

However, when I iterate through the completed list and print the contents the following is printed:

2
TESTDECK2
1
true
""

2
TESTDECK2
1
true
""

This is probably a very stupiud question but I'm reltively new to Java and so any help is greatly appreciated

3
  • 2
    don't make the class fieldsstatic. More info here: Static variables in java Commented Apr 17, 2020 at 9:12
  • @jhamon Thanks this worked. I'll take a look at that Commented Apr 17, 2020 at 9:15
  • I suggest you to use the "try with resources" for all the closeable objects. There you have: Connect, Statement and ResultSet; and you're closing only the connection Commented Apr 17, 2020 at 9:22

1 Answer 1

1

As @jhamon pointed out using static variables can be a very bad idea when you don't know what this means. Simply put a static field is shared among all instances of a class because it is a property of the class and not the instance. So when you have 10 instances of Deck all of them will return the same value for e.g. deckid.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.