foreach循环上的JAVA ClassCastException

时间:2019-07-05 08:06:47

标签: java exception iterator

我在遍历我创建的类的数组列表时遇到了奇怪的行为。

该类是简单的不变的东西:

public class Social {
    private String url;
    private SocialNames cat;

    public Social(String url, SocialNames cat) {
        this.url = url;
        this.cat = cat;
    }

    public String getUrl() {
        return url;
    }

    public SocialNames getCat() {
        return cat;
    }
}

包含arraylist的主程序类如下所示:

import java.util.List;

public class Data {
    private String facebook;
    private String twitter;
    private String google_plus;

    private ArrayList<Social> rrss = new ArrayList<Social>();

    public Data() {
    }

    private void parse() {
        //This function is way more complicated in the real code
        //But i think it doesn't interact with rrss

        facebook = "http://www.facebook.com/";
        twitter = "http://www.twitter.com/";
        google_plus = "http://www.google.com/";
    }

    public void load(String data) {
        parse();
        composeRRSS();
        dumpResults();       
    }

    private void dumpResults() {  
        for (Social s: rrss) {
            System.out.println("SOCIAL: "+s.getCat() + " URL="+s.getUrl());
        }        
    }

    private void composeRRSS() {
        if (facebook != null && !facebook.isEmpty()) {
            rrss.add(new Social(facebook, SocialNames.FACEBOOK));
        }

        if (twitter != null && !twitter.isEmpty()) {
            rrss.add(new Social(twitter, SocialNames.TWITTER));  
        }

        if (google_plus != null && !google_plus.isEmpty()) {
            rrss.add(new Social(google_plus, SocialNames.GOOGLE_PLUS));            
        }
    }
}

SocialNames只是一个枚举,如:

FACEBOOK,TWITTER,GOOGLE_PLUS

当我调用load()时,我得到以下堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to quickproject.Social

at quickproject.Data.dumpResults(Data.java:252)
    at quickproject.Data.load(Data.java:234)
    at quickproject.QuickProject.load(QuickProject.java:413)
    at quickproject.QuickProject.access$700(QuickProject.java:55)
    at quickproject.QuickProject$13.actionPerformed(QuickProject.java:443)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:842)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:886)
    at java.awt.Component.processMouseEvent(Component.java:6539)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6304)
    at java.awt.Container.processEvent(Container.java:2239)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2297)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
    at java.awt.Container.dispatchEventImpl(Container.java:2283)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
    at java.awt.EventQueue$4.run(EventQueue.java:733)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我对此处失败的原因一无所知,rrss显然是社交对象的数组列表,甚至更奇怪的是,我在Data类中有另一个函数可以做到:

for (Social s: rrss) {
    switch(s.getCat()) {
        case FACEBOOK:
            sb.append("<facebook>").append(s.getUrl()).append("</facebook>").append(System.lineSeparator());
            break;
        case TWITTER:
            sb.append("<twitter>").append(s.getUrl()).append("</twitter>").append(System.lineSeparator());                    
            break;
        case GOOGLE_PLUS:
            sb.append("<google_plus>").append(s.getUrl()).append("</google_plus>").append(System.lineSeparator());
            break;
    }
}

它确实有效!

任何帮助将不胜感激。 祝你有美好的一天。

[编辑]

SocialNames代码:

public enum SocialNames {
    FACEBOOK, TWITTER, GOOGLE_PLUS;
}

1 个答案:

答案 0 :(得分:-3)

您应该重写此

 System.out.println("SOCIAL: "+s.getCat() + " URL="+s.getUrl());

对此

 System.out.println("SOCIAL: "+s.getCat().name() + " URL="+s.getUrl());