关于铸造的问题

时间:2013-08-26 15:17:28

标签: java casting gson

我正在尝试使用Java中的auth系统制作游戏。当我尝试运行它时,我可以看到控制台日志中抛出异常,但项目中没有错误。我知道这是运行时错误

控制台日志显示以下信息:

Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to Auth$Profile
    at Auth.<init>(Auth.java:30)

这是我的代码:

 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;


       }

第30行是:

Profile p = (Profile)ps.get(name);

这是我的代码的一部分,我的想法是如果玩家按“记住密码”,游戏将生成一个.json文件来存储他的信息..我只是想知道我做错了什么,其他代码我可以自己写吗

1 个答案:

答案 0 :(得分:0)

您的ps.get(name)正在返回com.google.gson.internal.LinkedTreeMap个对象而不是Profile

尝试将其更改为:

LinkedTreeMap p = (LinkedTreeMap )ps.get(name);

您的代码没有显示错误,因为编译时没有错误,ClassCastException是运行时异常。

相关问题