HashMap总是返回null?

时间:2015-08-17 03:52:08

标签: java null hashmap

由于某些奇怪的原因,我的hashmap总是返回一个空值,即使它不应该为空。 mysql数据库中的行不为空。我没有收到任何错误我做了一个调试测试,看看它打印出来的是什么,并且它已经无效了。

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);
    }

}

1 个答案:

答案 0 :(得分:0)

如果您将auth个对象粘贴到HashMap,那么您需要在auth课程中properly override equals and hashCode

此外,您应该为您的班级Auth而不是auth命名,以便关注standard Java naming conventions。因此,将auth.java重命名为Auth.java,并将其定义如下(需要Java 7或更高版本):

<强> 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 + '\'' +
            '}';
    }
}

我只包含uuid而不包括equalshashCode中的其他字段,这意味着如果两个Auth具有相同的uuid,那么它们将是put被视为平等。如果在对象HashMap之后将字段修改为equals,则包含其他字段会使您失望。我认为这对您来说是最简单的方法,但要知道hashCode@IBAction func done() { let hudView = HudView.hudInView(navigationController!.view, animated: true) hudView.text = "Tagged" let location = NSEntityDescription.insertNewObjectForEntityForName("Location", inManagedObjectContext: managedObjectContext) as! Location location.locationDescription = descriptionText location.category = categoryName location.latitude = coordinate.latitude location.longitude = coordinate.longitude location.date = date location.placemark = placemark var error: NSError? if !managedObjectContext.save(&error) { println("Error: \(error)") abort() } afterDelay(0.6) {self.dismissViewControllerAnimated(true, completion: nil)} } 中对于持久对象的确切包含的内容是subject of debate for some time