-3

For some odd reason my hashmap is always returning a null value even if it shouldn't be null. The row isn't null in the mysql database. I'm getting no error I did a debug test to see what it's printing out and it's returing null.

auth.java

private String uuid;
private String name;
private int bits;
private String gang;
private String rank;



public auth(String uuid, String name, int bits, String gang, String rank)
{
    this.uuid = uuid;
    this.name = name;
    this.bits = bits;
    this.gang = gang;
    this.rank = rank;
}

authmanager.java

public HashMap<String, auth> auth = new HashMap<>();



public void saveUser(String uuid, String name, int bits, String gang, String rank)
{

    this.auth.put(uuid, new auth(uuid, name, bits, gang, rank));

}

establishconnection.java

public void establishProfile(Player p){

    String UUID = p.getUniqueId().toString();
    String NAME = p.getName();

    try
    {


        ResultSet query = sql.querySQL("SELECT * FROM `profiles` WHERE `UUID`= '" + UUID + "';");

        PreparedStatement CREATE = c.prepareStatement("INSERT INTO `profiles` (`UUID`,`Name`, `Bits`, `Bans`, `Gang`, `Rank`) VALUES (?, ?, ?, ?, ?, ?) ");
        PreparedStatement LOAD = c.prepareStatement("SELECT * FROM `profiles` WHERE `UUID`= ?");

        if ( query.next() )
        {
            LOAD.setString(1, UUID);
            plugin.authmanager.saveUser(query.getString("UUID"), query.getString("Name"), query.getInt("Bits"), query.getString("Gang"), query.getString("Rank"));
            LOAD.close();
            query.close();
            p.sendMessage(tables.PROFILE_LOADED);
            Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "[SQL]" +
                    " has loaded profile for " + NAME + "(" + UUID + ")"  );


        } else

        {
            CREATE.setString(1, UUID);
            CREATE.setString(2, NAME);
            CREATE.setInt(3, 0);
            CREATE.setInt(4, 0);
            CREATE.setString(5, null);
            CREATE.setString(6, "Default");

            CREATE.executeUpdate();
            CREATE.close();
            p.sendMessage(tables.PROFILE_CREATED);
            Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[SQL]" +
                    " executed a new query for " + NAME + "(" + UUID + "}" +  plugin.authmanager.auth.get(0) );

        }


    }   catch (Exception e)

    {
        e.printStackTrace();

            Bukkit.broadcastMessage("Someone's profile has failed to load!\n ERROR: " + e);
    }

}
2Hash
  • 123
  • 1
  • 2
  • 12

1 Answers1

0

If you're sticking auth objects into a HashMap, then you need to properly override equals and hashCode in your auth class.

Also, you should name your class Auth instead of auth to follow standard Java naming conventions. So rename auth.java to Auth.java, and define it as follows (requires Java 7 or higher):

Auth.java

import java.util.Objects;

public class Auth {
    private String uuid;
    private String name;
    private int bits;
    private String gang;
    private String rank;

    public Auth() {
    }

    public Auth(String uuid, String name, int bits, String gang, String rank) {
        this.uuid = uuid;
        this.name = name;
        this.bits = bits;
        this.gang = gang;
        this.rank = rank;
    }

    public String getRank() {
        return rank;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Auth)) {
            return false;
        }
        Auth auth = (Auth) o;
        return Objects.equals(uuid, auth.uuid);
    }

    @Override
    public int hashCode() {
        return Objects.hash(uuid);
    }

    @Override
    public String toString() {
        return "Auth{" +
            "uuid='" + uuid + '\'' +
            ", name='" + name + '\'' +
            ", bits=" + bits +
            ", gang='" + gang + '\'' +
            ", rank='" + rank + '\'' +
            '}';
    }
}

I only included uuid and not the other fields in equals and hashCode which means that if two Auths have the same uuid they will be treated as being equal. Including other fields will trip you up if you modify fields after the object has been put into a HashMap. I thought that would be the easiest approach for you, but know that what exactly to include in equals and hashCode for persistent objects has been a subject of debate for some time.

Community
  • 1
  • 1
heenenee
  • 19,914
  • 1
  • 60
  • 86
  • @2Hash I added an example `Auth` implementation. Also, if you find my answer sufficient, please accept it. Thanks! http://meta.stackexchange.com/a/23139/302176 – heenenee Aug 18 '15 at 02:16
  • how would I get a specific value from the hashmap back? How would I be able to just return the "rank" instead of the whole thing? – 2Hash Aug 18 '15 at 05:38
  • @2Hash Do `plugin.authmanager.auth.get(p.getUniqueId().toString())` where `p` is a `Player`. – heenenee Aug 18 '15 at 05:41
  • You must of answered my old question but I deleted it, but how would I be able to return just "rank" instead of all the values? – 2Hash Aug 18 '15 at 05:46
  • I just added a `getRank()` method to `Auth`. `plugin.authmanager.auth.get(p.getUniqueId().toString()).getRank()` will get just the rank. If you need other values, you should add the appropriate get methods for them as well. – heenenee Aug 18 '15 at 05:53
  • One more thing how would I give "gang" a certain value? Like for instance if someone makes a gang named "dog" the value in the hashmap where gang is would become "dog" for that user. – 2Hash Aug 18 '15 at 12:46