java - HashMap always returning null? -
for odd reason hashmap returning null value if shouldn't null. row isn't null in mysql database. i'm getting no error did debug test see it's printing out , 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 * `profiles` `uuid`= '" + uuid + "';"); preparedstatement create = c.preparestatement("insert `profiles` (`uuid`,`name`, `bits`, `bans`, `gang`, `rank`) values (?, ?, ?, ?, ?, ?) "); preparedstatement load = c.preparestatement("select * `profiles` `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 " + 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 new query " + name + "(" + uuid + "}" + plugin.authmanager.auth.get(0) ); } } catch (exception e) { e.printstacktrace(); bukkit.broadcastmessage("someone's profile has failed load!\n error: " + e); } }
if you're sticking auth
objects hashmap
, need properly override equals
, hashcode
in auth
class.
also, should name class auth
instead of auth
follow standard java naming conventions. rename auth.java
auth.java
, , define 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 included uuid
, not other fields in equals
, hashcode
means if 2 auth
s have same uuid
treated being equal. including other fields trip if modify fields after object has been put
hashmap
. thought easiest approach you, know include in equals
, hashcode
persistent objects has been subject of debate time.
Comments
Post a Comment