线程“AWT-EventQueue-0”中的异常java.lang.ClassCastException:无法将java.lang.String强制转换为java.util.List

时间:2013-11-07 13:16:30

标签: java netbeans classcastexception

我在Netbeans 7.2.1中创建了一个基本的Java程序。这是我的注册页面,用户在其中注册帐户。我使用"卷轴"从我的数据库访问信息的属性。但是,它一直给我这个错误(仅在运行时,而不是在编译时):

run:
[EL Info]: 2013-11-07 15:05:22.977--ServerSession(751265673)--EclipseLink, version: Eclipse Persistence Services - 2.3.2.v20111125-r10461
[EL Info]: 2013-11-07 15:05:23.721--ServerSession(751265673)--file:/C:/Users/Christiaan/Documents/Skoolwerk/11A1/IT/PAT/PAT 3/build/classes/_TryNCatchDBPU login successful
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: Cannot cast java.lang.String to java.util.List
    at java.lang.Class.cast(Class.java:3005)
    at org.jdesktop.beansbinding.Binding.convertForward(Binding.java:1312)
    at org.jdesktop.beansbinding.Binding.getSourceValueForTarget(Binding.java:844)
    at org.jdesktop.beansbinding.Binding.refreshUnmanaged(Binding.java:1222)
    at org.jdesktop.beansbinding.Binding.refresh(Binding.java:1207)
    at org.jdesktop.beansbinding.Binding.refreshAndNotify(Binding.java:1143)
    at org.jdesktop.beansbinding.AutoBinding.sourceChangedImpl(AutoBinding.java:223)
    at org.jdesktop.beansbinding.Binding.sourceChanged(Binding.java:1411)
    at org.jdesktop.beansbinding.Binding.access$1200(Binding.java:38)
    at org.jdesktop.beansbinding.Binding$PSL.propertyStateChanged(Binding.java:1618)
    at org.jdesktop.beansbinding.PropertyHelper.firePropertyStateChange(PropertyHelper.java:212)
    at org.jdesktop.beansbinding.ELProperty.notifyListeners(ELProperty.java:688)
    at org.jdesktop.beansbinding.ELProperty.access$800(ELProperty.java:155)
    at org.jdesktop.beansbinding.ELProperty$SourceEntry.processSourceChanged(ELProperty.java:312)
    at org.jdesktop.beansbinding.ELProperty$SourceEntry.sourceChanged(ELProperty.java:326)
    at org.jdesktop.beansbinding.ELProperty$SourceEntry.propertyChange(ELProperty.java:333)
    at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
    at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:327)
    at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
    at java.awt.Component.firePropertyChange(Component.java:8382)
    at pat.pkg3.LoginNewUser.setScroller(LoginNewUser.java:35)
    at pat.pkg3.LoginNewUser.<init>(LoginNewUser.java:23)
    at pat.pkg3.LoginNewUser$2.run(LoginNewUser.java:229)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701)
    at java.awt.EventQueue.access$000(EventQueue.java:102)
    at java.awt.EventQueue$3.run(EventQueue.java:662)
    at java.awt.EventQueue$3.run(EventQueue.java:660)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:671)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
BUILD SUCCESSFUL (total time: 5 seconds)

解决错误的唯一方法是删除调用&#34; setScroller()&#34;的代码。方法(接近下面代码中的顶部):

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pat.pkg3;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class LoginNewUser extends javax.swing.JFrame {

    JFrame LoginNewUser = new JFrame();

    public int size; public String username, password, group;
    int index = 0;

    public LoginNewUser() 
    {
        initComponents();
        setScroller(groupsList.get(index));  //BY REMOVING THIS, IT WORKS
    }  
    private Groups scroller;
    public static final String PROP_SCROLLER = "scroller";

    public Groups getScroller() {
        return scroller;
    }

    public void setScroller(Groups scroller) {
        Groups oldScroller = this.scroller;
        this.scroller = scroller;
        firePropertyChange(PROP_SCROLLER, oldScroller, scroller);
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        TryNCatchDBPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("TryNCatchDBPU").createEntityManager();
        groupsQuery = java.beans.Beans.isDesignTime() ? null : TryNCatchDBPUEntityManager.createQuery("SELECT g FROM Groups g");
        groupsList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : groupsQuery.getResultList();
        individualQuery = java.beans.Beans.isDesignTime() ? null : TryNCatchDBPUEntityManager.createQuery("SELECT i FROM Individual i");
        individualList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : individualQuery.getResultList();
        jlLogo = new javax.swing.JLabel();
        jlHeader = new javax.swing.JLabel();
        jlEnterUsername = new javax.swing.JLabel();
        jlEnterPassword = new javax.swing.JLabel();
        jtfUsername = new javax.swing.JTextField();
        jbRegister = new javax.swing.JButton();
        jlReenterPassword = new javax.swing.JLabel();
        jpfPassword = new javax.swing.JPasswordField();
        jpfPasswordConfirm = new javax.swing.JPasswordField();
        jlChooseGroup = new javax.swing.JLabel();
        jcbGroup = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jlLogo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pat/pkg3/tryncatch logo.png"))); // NOI18N

        jlHeader.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        jlHeader.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlHeader.setText("Register");

        jlEnterUsername.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jlEnterUsername.setText("Enter a username:");

        jlEnterPassword.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jlEnterPassword.setText("Enter a password:");

        jbRegister.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jbRegister.setText("Register");
        jbRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbRegisterActionPerformed(evt);
            }
        });

        jlReenterPassword.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jlReenterPassword.setText("Re-enter your password:");

        jlChooseGroup.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jlChooseGroup.setText("Choose a group:");

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${scroller.groupname}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, this, eLProperty, jcbGroup, "");
        bindingGroup.addBinding(jComboBoxBinding);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jbRegister, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(378, 378, 378))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jlLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(359, 359, 359))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(296, Short.MAX_VALUE)
                .addComponent(jlHeader, javax.swing.GroupLayout.PREFERRED_SIZE, 504, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(224, 224, 224))
            .addGroup(layout.createSequentialGroup()
                .addGap(345, 345, 345)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlEnterUsername)
                    .addComponent(jlEnterPassword)
                    .addComponent(jlReenterPassword)
                    .addComponent(jlChooseGroup))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtfUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                    .addComponent(jpfPassword)
                    .addComponent(jpfPasswordConfirm)
                    .addComponent(jcbGroup, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(284, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jlLogo)
                .addGap(11, 11, 11)
                .addComponent(jlHeader, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jlEnterUsername))
                    .addComponent(jtfUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlEnterPassword)
                    .addComponent(jpfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlReenterPassword)
                    .addComponent(jpfPasswordConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlChooseGroup)
                    .addComponent(jcbGroup, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addComponent(jbRegister)
                .addContainerGap(52, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1040)/2, (screenSize.height-575)/2, 1040, 575);
    }// </editor-fold>

    private void jbRegisterActionPerformed(java.awt.event.ActionEvent evt) {                                           
        size = individualList.size();
        username = jtfUsername.getText();
        password = jpfPassword.getText();
        String passwordConfirm = jpfPasswordConfirm.getText();
        if (!password.equals(passwordConfirm)) //Checks if passwords match
        {
            JOptionPane.showMessageDialog(null, "Your passwords don't match. Please try again");
        }
        else{
        group = jcbGroup.getSelectedItem().toString();
        String sql = "INSERT INTO JAVADB.INDIVIDUAL (NUM, USERNAME, PASSWORD, GROUPNAME, POINTS, COMMUNITYRANKING, GROUPRANKING, NUMPREDICTIONS, NUMCORRECTPREDICTIONS) VALUES (" + (size + 1) + ", " + username + ", " + password + ", " +  group + ", 0, 0, 0, 0, 0)";
        newUser(sql);
        }
    }                                          

    public void newUser(String sql)
        {
            try
            {
                Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/TryNCatchDB", "javadb", "javadb");
                java.sql.Statement st = con.createStatement();
                st.executeUpdate(sql);
                JOptionPane.showMessageDialog(null, "You have successfully created a new account!");
                this.setVisible(false); new Home().setVisible(true); //Opens Home page (Home class)
            }
          catch(SQLException s)
            {
                JOptionPane.showMessageDialog(null, "There is already a user with this name. Try again.");
                jtfUsername.setText(""); jpfPassword.setText(""); jpfPasswordConfirm.setText(""); //Clears input to try again.
            }
        }

    /**           
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LoginNewUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LoginNewUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LoginNewUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LoginNewUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoginNewUser().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.persistence.EntityManager TryNCatchDBPUEntityManager;
    private java.util.List<pat.pkg3.Groups> groupsList;
    private javax.persistence.Query groupsQuery;
    private java.util.List<pat.pkg3.Individual> individualList;
    private javax.persistence.Query individualQuery;
    private javax.swing.JButton jbRegister;
    private javax.swing.JComboBox jcbGroup;
    private javax.swing.JLabel jlChooseGroup;
    private javax.swing.JLabel jlEnterPassword;
    private javax.swing.JLabel jlEnterUsername;
    private javax.swing.JLabel jlHeader;
    private javax.swing.JLabel jlLogo;
    private javax.swing.JLabel jlReenterPassword;
    private javax.swing.JPasswordField jpfPassword;
    private javax.swing.JPasswordField jpfPasswordConfirm;
    private javax.swing.JTextField jtfUsername;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration

}

Here's what the design preview looks like

我面临什么问题?谢谢!

0 个答案:

没有答案