繁体   English   中英

有关铸造的问题

[英]Questions about casting

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

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

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文件来存储他的信息。我只想知道我做错了什么,其他代码我也可以编写我

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

尝试将其更改为:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM