Java:HashMap.get返回null [英] Java: HashMap.get returns null

查看:143
本文介绍了Java:HashMap.get返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AS3中编写了一个用于MMORPG的套接字服务器。我有一些HashMap.get(byte [])的奇怪行为的问题。
什么事情可能导致以下问题?



我不使用任何序列化系统,我发送字节,客户端和服务器知道怎么做接收字节的字节。客户端做出的第一个请求是要求服务器创建播放会话。服务器将生成一个带有8个条目的随机会话令牌作为byte [],并将令牌添加到HashMap。响应将为9个字节长。第一个定义响应类型(在这种情况下,字节'+'表示您已被接受,这是您的令牌。)和字节2-9是令牌。客户端必须存储令牌并将其附加到任何进一步的请求(这个概念可以与PHP会话进行比较)。



这是将客户端添加到HashMap的代码:

  byte [] token = Util.generateToken 

// f是JDCB SQL查询的ResultSet条目返回值
客户端客户端=新客户端(f.getInt(id),token);
Core.clients.put(token,client);

//显示client.token而不是令牌以确保client!= null
Log.log(User+ f.getString(name)+已登录with session token+ Util.getHexString(client.token)+。);

日志:


[01:50:30]用户已使用会话令牌92:B7:F8:C6:4B:53:17:3A登录。



b $ b

这些是一些调试行,将在任何进一步的请求显示:

  )Token as Hex String,8 bytes long 
Log.soc(Token:+ Util.getHexString(buffer.getBytes(1,9)));

//显示所有的键(HashMap< byte [],client>)Core.clients
int it = 0;
for(byte [] b:Core.clients.keySet())
Log.soc(Key Core.clients#+ StringUtils.leftPad(String.valueOf(++ it),2, '0')+=+ Util.getHexString((b)));

//显示可用性bool以确保
Log.soc(Core.clients.containsKey(buffer.getBytes(1,9)));

//获取Core.clients值其中key = Token
Log.soc(Client:+ Core.clients.get(buffer.getBytes(1,9)));

日志:


[01:51:09]令牌:92:B7:F8:C6:4B:53:17:3A



[01:51:09] Core.clients#01 = 92:B7:F8:C6:4B:53:17:3A



[01:51:09] false



[01:51:09]客户:null


HashMap使用 hashCode()和<$

c $ c> equals()从给定的键中查找条目。一个字节数组只会等于它自己。它不会等于另一个字节数组,即使这个其他字节数组具有相同的长度和相同的字节。你应该将数组包装在一个重写 equals() hashCode() / p>

I am programming a socket server in Java for an MMORPG in AS3. I have some problems with strange behaviour of HashMap.get(byte[]). What things could cause the following problem?

I don't use any serialization system, I send bytes and both client and server knows what to do with what bytes of the received bytes. The first request the client makes is asking the server to create a playing session. The server will generate a random session token as byte[] with 8 entries and adds the token to the HashMap. The response will be 9 bytes long. The first one defines the response type (in this case byte '+' which means "You have been accepted. Here is your token.") and byte 2-9 are the token. The client must store the token and append it to any further request (This concept can be compared with PHP sessions).

This is the code for adding the client to the HashMap:

byte[] token = Util.generateToken();

// f is ResultSet entry of JDCB SQL query return value
Client client = new Client(f.getInt("id"), token);
Core.clients.put(token, client);

// I display client.token instead of token to make sure client != null      
Log.log("User " + f.getString("name") + " has logged in with session token " + Util.getHexString(client.token) + ".");

Log:

[01:50:30] User has logged in with session token 92:B7:F8:C6:4B:53:17:3A.

These are some debug lines that will displayed at any further request:

// Show (byte[]) Token as Hex String, 8 bytes long
Log.soc("Token: " + Util.getHexString(buffer.getBytes(1, 9)));

// Show all keys in (HashMap<byte[], client>) Core.clients
int it = 0;
for (byte[] b : Core.clients.keySet())
Log.soc("Key Core.clients #" + StringUtils.leftPad(String.valueOf(++it), 2, '0') + " = " + Util.getHexString((b)));

// Display availability bool to make sure
Log.soc(Core.clients.containsKey(buffer.getBytes(1, 9)));

// Get Core.clients value where key = Token
Log.soc("Client: " + Core.clients.get(buffer.getBytes(1, 9)));

Log:

[01:51:09] Token: 92:B7:F8:C6:4B:53:17:3A

[01:51:09] Key Core.clients #01 = 92:B7:F8:C6:4B:53:17:3A

[01:51:09] false

[01:51:09] Client: null

So, how could I find out what's going wrong?

解决方案

A HashMap uses hashCode() and equals() to find the entry from a given key. And a byte array will only ever be equal to itself. It won't be equal to another byte array, even if this other byte array has the same length and the same bytes. You should wrap the arrays inside a Key class which override equals() and hashCode() to make that work.

这篇关于Java:HashMap.get返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆