Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 比较两个字符串时的classCastexception_Java_String_Compare_Classcastexception - Fatal编程技术网

Java 比较两个字符串时的classCastexception

Java 比较两个字符串时的classCastexception,java,string,compare,classcastexception,Java,String,Compare,Classcastexception,我在Stackoverflow上发现了什么是ClassCastException,但我不知道如何解决我的问题: 我有一个类Gebruiker=dutch供用户使用。每个Gebruiker/User对象都有一个字符串Gebruikersnaam=用户名和一个字符串密码。布尔类型决定用户是管理员true还是普通用户=false public class Gebruiker implements Serializable{ private String gebruikersnaam;

我在Stackoverflow上发现了什么是ClassCastException,但我不知道如何解决我的问题:

我有一个类Gebruiker=dutch供用户使用。每个Gebruiker/User对象都有一个字符串Gebruikersnaam=用户名和一个字符串密码。布尔类型决定用户是管理员true还是普通用户=false

public class Gebruiker implements Serializable{

        private String gebruikersnaam;
        private String wachtwoord;
        private Boolean type;  //0 = user 1=super user

        public Gebruiker(String gebruikersnaam, String wachtwoord, Boolean type) {
            gebruikersnaam = this.gebruikersnaam;
            wachtwoord = this.wachtwoord;
            type = this.type;

        }
此类中的方法以字符串形式返回用户名

        /**
         * @return the gebruikersnaam
         */
        public String getGebruikersnaam() {
            return gebruikersnaam;
        }
有一个数组列表gebruikers;,我存储所有用户的地方

下面代码中的If语句使用my methode gebr.getGebruikersnaam将给定Gebruiker对象的用户名与用户名输入字段LogOnName中的文本进行比较。所以我比较了一个字符串和一个字符串,但是我得到了一个错误

 for (Gebruiker gebr : gebruikers) {
   //line 72 in the code: 
   if (gebr.getGebruikersnaam().equals(logOnName.getText())){
      //do something here

      }
    }
由于我无法解决的错误而生成的Stacktrace的一部分:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to my.venster.Gebruiker
    at my.venster.zeeOnline.logOnGebruiker(zeeOnline.java:72)
    at my.venster.zeeOnline.logOnButtonActionPerformed(zeeOnline.java:910)
    at my.venster.zeeOnline.access$300(zeeOnline.java:30)
    at my.venster.zeeOnline$4.actionPerformed(zeeOnline.java:429)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
我明白,当我试图将一个类型强制转换为与该类型不兼容的另一个类型时,可能会得到ClassCastException。ie将一个整数转换成一个字符串,但在这里,我需要一个来自用户对象的字符串,它也是一个字符串

        /**
         * @return the gebruikersnaam
         */
        public String getGebruikersnaam() {
            return gebruikersnaam;
        }
当我尝试为一个用户执行登录过程时,为什么会出现令人讨厌的ClassCastException?

打印logOnName.getText并查看它是否打印字符串或对象。你必须这样做作业:

this.gebruikersnaam = gebruikersnaam;
this.wachtwoord = wachtwoord;
this.type = type;

你能把CComplete代码放在这里吗,即zeeOnline.ajava第72行。另外,在你的构造函数中,从实例变量赋值局部变量,反之亦然。this.gebruikersnaam=gebruikersnaam;非常清楚:java.lang.String不能强制转换到第72行的my.venster.Gebruiker。尽管泛型具有假定的安全性,但您的ArrayList包含字符串。构造函数中的所有赋值都是向后的。