2013-08-26 92 views
0

我想用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文件来存储他的我nfomation ..我只想知道我做错了什么,其他代码我可以自己写

+0

* ClassCastException * Java试图执行强制转换,但由于没有继承而无法执行 –

回答

0

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

尝试将其更改为:

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

,因为有一个在编译时没有错误代码不告诉你的错误,ClassCastException是一个运行时异常。

+0

否...此类从不使用com.google.gson.internal.LinkedTreeMap,如果将我的代码更改为您的代码,则会收到项目错误= > p.displayName无法解析,或者不是字段 – Jeremy

+0

@ user2718549肯定存在一个LinkedTreeMap对象,它来自某处。找出。你提到的编译错误是由另一个更改引起的,而不是这个。当然这很明显? – EJP

+0

@EJP你能帮我解决吗? – Jeremy