java - Questions about casting -


i trying make game auth system in java. when trying run it, can see exception thrown in console log there no error in project. know runtime error

the console log displays following information:

exception in thread "main" java.lang.classcastexception: com.google.gson.internal.linkedtreemap cannot cast auth$profile     @ auth.<init>(auth.java:30) 

here 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 part of code, idea if player press "remember password", game generate .json file store infomation..i want know did wrong, other code can write myself

your ps.get(name) returning com.google.gson.internal.linkedtreemap object instead of profile.

try change to:

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

your code doesn't show errors because there's no error in compile time, classcastexception runtime exception.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -