[英]Questions about casting
I am trying to make a game with auth system in Java. 我正在尝试使用Java中的身份验证系统制作游戏。 When I am trying to run it, i can see an exception thrown in the console log but there is no error in the project.
当我尝试运行它时,我可以在控制台日志中看到一个异常,但是项目中没有错误。 I know this is runtime error
我知道这是运行时错误
The console log displays the following information: 控制台日志显示以下信息:
Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to Auth$Profile
at Auth.<init>(Auth.java:30)
Here is my code: 这是我的代码:
public Auth(File profilesFile) {
try {
ProfilesJSON e = (ProfilesJSON)this.gson.fromJson(new FileReader(profilesFile), ProfilesJSON.class);
Map ps = e.authenticationDatabase;
Iterator var5 = ps.keySet().iterator();
while(var5.hasNext()) {
String name = (String)var5.next();
Profile p = (Profile)ps.get(name);
if(p != null) {
if(p.displayName == null || p.displayName.length() == 0) {
p.displayName = p.username;
}
this.profiles.add(p);
}
}
} catch (FileNotFoundException var7) {
;
} catch (NullPointerException var8) {
;
}
}
public class Profile {
public String username;
public String password;
public String uid;
public String displayName;
public String name;
public String playerUID;
public Profile(String u, String t, String id, String d) {
this.username = u;
this.password = t;
this.uid = id;
this.displayName = d;
}
}
public class ProfilesJSON {
public Map profiles;
public String selectedProfile;
public String password;
public Map authenticationDatabase;
}
Line 30 is: 第30行是:
Profile p = (Profile)ps.get(name);
This is a part of my code, my idea is if the player press "Remember Password", the game will generate a .json file to store his infomation..I just want to know what I did wrong, other code i can write it myself 这是我代码的一部分,我的想法是,如果玩家按下“记住密码”,游戏将生成一个.json文件来存储他的信息。我只想知道我做错了什么,其他代码我也可以编写我
Your ps.get(name)
is returning a com.google.gson.internal.LinkedTreeMap
object instead of Profile
. 您的
ps.get(name)
返回com.google.gson.internal.LinkedTreeMap
对象,而不是Profile
。
try to change it to: 尝试将其更改为:
LinkedTreeMap p = (LinkedTreeMap )ps.get(name);
Your code doesn't show you errors because there's no error in compile time, ClassCastException is a runtime exception. 您的代码不会显示错误,因为编译时没有错误, ClassCastException是运行时异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.