Null values in hashtable - Java

0

I am developing a small app in Java, in which I make a connection to a database and I get a JSON field, that JSON I transform it into an object, and later I insert them into a hashtable, and finally I return the hashtable. ..

The Subject is in which I have 4 rows in the database, and in the App when adding the hashtable data to the table, it only takes 3 ... If I add another data to the database, only take 4 of 5 ... and so on

Code: (DatabaseConnection.java)

    public static <T> Hashtable<Integer, T> GetList(String Table) throws Exception{

    T obj = null;
    Hashtable<Integer, T> objs = new Hashtable<Integer, T>();
    int id = 0;
    String data;
    Connection Connector = Connect();

    try{

        Statement Stats = Connector.createStatement();
        String sql = "SELECT * FROM " + Table;
        ResultSet result = Stats.executeQuery(sql);

        while(result.next()){
            id = result.getInt(1);
            data = result.getString(2);

            obj = SerializerClass.Deserialize(data);
            objs.put(id, obj);
            System.out.println(id);
        }

    }catch(SQLException sqlEx){
        throw new Exception("Exeption Ocurred. " + sqlEx.getMessage());
    }

    return objs;
} 

Code # 2 (PrincipalForm.java)

public void UpdateGUI(){
    Columns = new String[] {"Nombre", "Servicio", "Fecha", "Estado"};
    n = new DefaultTableModel(Columns, 0);
    SessionsTable.setModel(n);

    try{
        this.SesionesList = DataBaseConnection.GetList("sesiones");
        if (!SesionesList.isEmpty()) {
            for (int i = 0; i < SesionesList.size(); i++) {
                if (SesionesList.get(i) != null) {
                    String Name = SesionesList.get(i).Name;
                    PaqueteCasual Service = SesionesList.get(i).Servicio;
                    String fecha = SesionesList.get(i).Fecha;
                    Estado State = SesionesList.get(i).State;

                    Object[] data = { Name, Service, fecha, State };

                    n.addRow(data);
                }else { System.out.println("Is null"); }
            }
        }else{
            System.out.print("Its Empty.");
        }

    } catch (Exception ex) {
        System.err.print("ERROR: " + ex.getMessage());
        Logger.getLogger(SesionesPrincipalForm.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From what I have noticed, it is that when it reaches the 4th value, it returns a null. As you may notice in Code # 2, I have an IF that confirms if it is null or not, and indeed ... with the first 3 values it takes, but the 4th one is Null

    
asked by Alexandre Marquez 04.08.2018 в 23:09
source

1 answer

1
for (int i = 0; i < SesionesList.size(); i++) {
            if (SesionesList.get(i) != null) {
                String Name = SesionesList.get(i).Name;

You are trying to go through Map as if it were a List . It does not work like that.

In the lists, you search by index, and you know that it goes by order. So the first element will be 0, then 1 , so up to size() - 1 .

In the maps you relate a key with the element, and you search for the key. If there are 10 elements, you do not have to be able to recover them using the numbers from 0 to 9 as keys, but the keys that were used.

For example

Map mapa = new Hashtable();
mapa.put("Hola", "Mundo");
System.out.println(mapa.get("Hola")); // Imprime "Mundo"
System.out.println(mapa.get(0));  // Imprime null, porque no hay ningún elemento que tenga 0 como clave.

If you want to iterate, two options:

  • Use a list or array, which is what they are for.

  • The keys() method returns a Enumeration with all keys defined. Iteras about those keys, and for each one of those keys you do the corresponding get .

answered by 04.08.2018 в 23:22