I am trying to make a game with auth system in 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:
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
Your ps.get(name)
is returning a com.google.gson.internal.LinkedTreeMap
object instead of 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.