ClassCastException 错误(对象和向量)

发布于 2024-12-07 13:37:01 字数 6969 浏览 2 评论 0原文

我正在使用 LWUIT 列表。以下是列表的创建方式:

db = new ClientDB();
listclient = db.listClient();
tModel = new ListSource(listclient);
renderer = new CListCell(true, new String[]{"", "", "Encours : ", "Impayés : "});
tList = new CList(tModel, renderer);

public Vector listClient()
    {
        try
        {
            filter = new Filter("");
            comparator = new Comparator();
            enumRec = this.enumerateRecords(filter, comparator);
            while(enumRec.hasNextElement())
            {
                id = enumRec.nextRecordId();
                recIDs.addElement(new Integer(id));
                bData = this.getRecord(id);
                client = new Client(bData);
                if (!innerResultat.isEmpty())
                {
                    innerResultat.removeAllElements();
                }
                innerResultat.addElement(new String(client.getIdclient()));
                innerResultat.addElement(new String(client.getCin()));
                innerResultat.addElement(new String(client.getNoms()));
                innerResultat.addElement(new String(client.getEncours()));
                innerResultat.addElement(new String(client.getImpayes()));
                resultat.addElement(innerResultat);
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return resultat;
    }
public class ListSource extends DefaultListModel {

    public ListSource(Vector valeur)
    {
        super(valeur);
    }
    public int getListIDs(int ligneSelected)
    {
        return ligneSelected;
    }
}
public class CListCell extends Container implements ListCellRenderer {

    private boolean isPhotoDisplayed;

    private BorderLayout photoLayout = new BorderLayout();
    private BoxLayout yLayout = new BoxLayout(BoxLayout.Y_AXIS);

    private Vector vLabels = new Vector();
    private Label pic = new Label("");
    private Container cnt;

    private Font fontLibelle = (MenuPrincipalForm.r).getFont("FontTextFieldBold");

    private Label focus = new Label("");

    public CListCell(boolean displayPhoto, String[] libelles)
    {
        isPhotoDisplayed = displayPhoto;
        this.getSelectedStyle().setPadding(0, 0, 0, 0);
        this.getUnselectedStyle().setPadding(0, 0, 0, 0);
        if (displayPhoto)
        {
            setLayout(photoLayout);
            addComponent(BorderLayout.WEST, pic);
        }
        else
        {
            setLayout(yLayout);
        }
        cnt = new Container(yLayout);
        for (int i=0;i<libelles.length;i++)
        {
            vLabels.addElement(new Label(libelles[i]));
            ((Label)vLabels.elementAt(i)).getStyle().setBgTransparency(0);
            ((Label)vLabels.elementAt(i)).getStyle().setFont(fontLibelle);
            cnt.addComponent(((Label)vLabels.elementAt(i)));
        }
        addComponent(BorderLayout.CENTER, cnt);
        focus.getStyle().setBgTransparency(100);
    }
    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected)
    {
        Vector reelData = new Vector();
        if (value != null && (value instanceof Vector))
        {
            if (((Vector)value).isEmpty())
            {
                if (isPhotoDisplayed)
                    pic.setIcon(null);
                for (int i=0; i<vLabels.size(); i++)
                    ((Label)vLabels.elementAt(i)).setText(((Label)vLabels.elementAt(i)).getText().concat("-"));
            }
            else
            {
                reelData = (Vector)((Vector)value).elementAt(index);
                for (int i=0; i<vLabels.size(); i++)
                    ((Label)vLabels.elementAt(i)).setText(((Label)vLabels.elementAt(i)).getText().concat(String.valueOf(reelData.elementAt(i+1))));
                pic.setIcon(null);
            }
        }
        return this;
    }
    public Component getListFocusComponent(List list)
    {
        return focus;
    }
}
public class CList extends List {
    public CList(ListSource model, CListCell renderer)
    {
        setModel(model);
        setListCellRenderer(renderer);
        setItemGap(0);
        setFixedSelection(List.FIXED_NONE_CYCLIC);
    }
}

ListCellRenderergetListCellRendererComponent 方法中,value 参数导致了我的问题。它的类型在Javadoc中是Object,而在我的应用程序中它的真实数据类型是Vector,它是一个Vector的Vector(一个元素是Vector的Vector)。

我的问题是,我在运行时收到 ClassCastException 错误,并且堆栈错误显示 getListCellRendererComponent 方法导致了错误。

那么如何使用 Object 和 Vector 呢?

堆栈错误是:

java.lang.ClassCastException:   0
 - view.CListCell.getListCellRendererComponent(), bci=112
 - com.sun.lwuit.plaf.DefaultLookAndFeel.getListPreferredSize(), bci=218
 - com.sun.lwuit.List.calcPreferredSize(), bci=7
 - com.sun.lwuit.Component.preferredSize(), bci=28
 - com.sun.lwuit.Component.getPreferredSize(), bci=1
 - com.sun.lwuit.Component.getPreferredH(), bci=1
 - com.sun.lwuit.layouts.FlowLayout.getPreferredSize(), bci=70
 - com.sun.lwuit.Container.calcPreferredSize(), bci=5
 - com.sun.lwuit.Component.preferredSize(), bci=28
 - com.sun.lwuit.Component.getPreferredSize(), bci=1
 - com.sun.lwuit.Component.getPreferredH(), bci=1
 - com.sun.lwuit.layouts.BoxLayout.getPreferredSize(), bci=38
 - com.sun.lwuit.Container.calcPreferredSize(), bci=5
 - com.sun.lwuit.Component.preferredSize(), bci=28
 - com.sun.lwuit.Component.getPreferredSize(), bci=1
 - com.sun.lwuit.Component.getPreferredH(), bci=1
 - com.sun.lwuit.layouts.FlowLayout.getPreferredSize(), bci=70
 - com.sun.lwuit.Container.calcPreferredSize(), bci=5
 - com.sun.lwuit.Component.calcScrollSize(), bci=1
 - com.sun.lwuit.Component.getScrollDimension(), bci=23
 - com.sun.lwuit.Container.isScrollableY(), bci=8
 - com.sun.lwuit.Component.getSideGap(), bci=1
 - com.sun.lwuit.layouts.FlowLayout.layoutContainer(), bci=18
 - com.sun.lwuit.Container.doLayout(), bci=5
 - com.sun.lwuit.Container.layoutContainer(), bci=13
 - com.sun.lwuit.Container.doLayout(), bci=37
 - com.sun.lwuit.Container.layoutContainer(), bci=13
 - com.sun.lwuit.Form.initFocused(), bci=20
 - com.sun.lwuit.Form.show(), bci=25
 - com.sun.lwuit.Form.show(), bci=2
 - controler.SmartPhoneBanking.displayScreen(), bci=19
 - view.MenuPrincipalForm.actionPerformed(), bci=56
 - com.sun.lwuit.util.EventDispatcher.fireActionSync(), bci=19
 - com.sun.lwuit.util.EventDispatcher.fireActionEvent(), bci=89
 - com.sun.lwuit.Button.fireActionEvent(), bci=70
 - com.sun.lwuit.Button.released(), bci=17
 - com.sun.lwuit.Button.pointerReleased(), bci=1
 - com.sun.lwuit.Form.pointerReleased(), bci=93
 - com.sun.lwuit.Component.pointerReleased(), bci=7
 - com.sun.lwuit.Display.handleEvent(), bci=125
 - com.sun.lwuit.Display.edtLoopImpl(), bci=115
 - com.sun.lwuit.Display.mainEDTLoop(), bci=198
 - com.sun.lwuit.RunnableWrapper.run(), bci=242
 - java.lang.Thread.run(), bci=11

I am working with LWUIT List. Here is how the List is created :

db = new ClientDB();
listclient = db.listClient();
tModel = new ListSource(listclient);
renderer = new CListCell(true, new String[]{"", "", "Encours : ", "Impayés : "});
tList = new CList(tModel, renderer);

public Vector listClient()
    {
        try
        {
            filter = new Filter("");
            comparator = new Comparator();
            enumRec = this.enumerateRecords(filter, comparator);
            while(enumRec.hasNextElement())
            {
                id = enumRec.nextRecordId();
                recIDs.addElement(new Integer(id));
                bData = this.getRecord(id);
                client = new Client(bData);
                if (!innerResultat.isEmpty())
                {
                    innerResultat.removeAllElements();
                }
                innerResultat.addElement(new String(client.getIdclient()));
                innerResultat.addElement(new String(client.getCin()));
                innerResultat.addElement(new String(client.getNoms()));
                innerResultat.addElement(new String(client.getEncours()));
                innerResultat.addElement(new String(client.getImpayes()));
                resultat.addElement(innerResultat);
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return resultat;
    }
public class ListSource extends DefaultListModel {

    public ListSource(Vector valeur)
    {
        super(valeur);
    }
    public int getListIDs(int ligneSelected)
    {
        return ligneSelected;
    }
}
public class CListCell extends Container implements ListCellRenderer {

    private boolean isPhotoDisplayed;

    private BorderLayout photoLayout = new BorderLayout();
    private BoxLayout yLayout = new BoxLayout(BoxLayout.Y_AXIS);

    private Vector vLabels = new Vector();
    private Label pic = new Label("");
    private Container cnt;

    private Font fontLibelle = (MenuPrincipalForm.r).getFont("FontTextFieldBold");

    private Label focus = new Label("");

    public CListCell(boolean displayPhoto, String[] libelles)
    {
        isPhotoDisplayed = displayPhoto;
        this.getSelectedStyle().setPadding(0, 0, 0, 0);
        this.getUnselectedStyle().setPadding(0, 0, 0, 0);
        if (displayPhoto)
        {
            setLayout(photoLayout);
            addComponent(BorderLayout.WEST, pic);
        }
        else
        {
            setLayout(yLayout);
        }
        cnt = new Container(yLayout);
        for (int i=0;i<libelles.length;i++)
        {
            vLabels.addElement(new Label(libelles[i]));
            ((Label)vLabels.elementAt(i)).getStyle().setBgTransparency(0);
            ((Label)vLabels.elementAt(i)).getStyle().setFont(fontLibelle);
            cnt.addComponent(((Label)vLabels.elementAt(i)));
        }
        addComponent(BorderLayout.CENTER, cnt);
        focus.getStyle().setBgTransparency(100);
    }
    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected)
    {
        Vector reelData = new Vector();
        if (value != null && (value instanceof Vector))
        {
            if (((Vector)value).isEmpty())
            {
                if (isPhotoDisplayed)
                    pic.setIcon(null);
                for (int i=0; i<vLabels.size(); i++)
                    ((Label)vLabels.elementAt(i)).setText(((Label)vLabels.elementAt(i)).getText().concat("-"));
            }
            else
            {
                reelData = (Vector)((Vector)value).elementAt(index);
                for (int i=0; i<vLabels.size(); i++)
                    ((Label)vLabels.elementAt(i)).setText(((Label)vLabels.elementAt(i)).getText().concat(String.valueOf(reelData.elementAt(i+1))));
                pic.setIcon(null);
            }
        }
        return this;
    }
    public Component getListFocusComponent(List list)
    {
        return focus;
    }
}
public class CList extends List {
    public CList(ListSource model, CListCell renderer)
    {
        setModel(model);
        setListCellRenderer(renderer);
        setItemGap(0);
        setFixedSelection(List.FIXED_NONE_CYCLIC);
    }
}

And in the getListCellRendererComponent method of the ListCellRenderer the value parameter causes me the problem. Its type is Object in the Javadoc , and the real datatype of it in my application is Vector, it is a Vector of Vector ( a Vector whose elements are Vector).

My problem is that I get the ClassCastException error in runtime , and the stack error shows that the getListCellRendererComponent method caused the error.

So how to work with Object and Vector ?

Stack error is :

java.lang.ClassCastException:   0
 - view.CListCell.getListCellRendererComponent(), bci=112
 - com.sun.lwuit.plaf.DefaultLookAndFeel.getListPreferredSize(), bci=218
 - com.sun.lwuit.List.calcPreferredSize(), bci=7
 - com.sun.lwuit.Component.preferredSize(), bci=28
 - com.sun.lwuit.Component.getPreferredSize(), bci=1
 - com.sun.lwuit.Component.getPreferredH(), bci=1
 - com.sun.lwuit.layouts.FlowLayout.getPreferredSize(), bci=70
 - com.sun.lwuit.Container.calcPreferredSize(), bci=5
 - com.sun.lwuit.Component.preferredSize(), bci=28
 - com.sun.lwuit.Component.getPreferredSize(), bci=1
 - com.sun.lwuit.Component.getPreferredH(), bci=1
 - com.sun.lwuit.layouts.BoxLayout.getPreferredSize(), bci=38
 - com.sun.lwuit.Container.calcPreferredSize(), bci=5
 - com.sun.lwuit.Component.preferredSize(), bci=28
 - com.sun.lwuit.Component.getPreferredSize(), bci=1
 - com.sun.lwuit.Component.getPreferredH(), bci=1
 - com.sun.lwuit.layouts.FlowLayout.getPreferredSize(), bci=70
 - com.sun.lwuit.Container.calcPreferredSize(), bci=5
 - com.sun.lwuit.Component.calcScrollSize(), bci=1
 - com.sun.lwuit.Component.getScrollDimension(), bci=23
 - com.sun.lwuit.Container.isScrollableY(), bci=8
 - com.sun.lwuit.Component.getSideGap(), bci=1
 - com.sun.lwuit.layouts.FlowLayout.layoutContainer(), bci=18
 - com.sun.lwuit.Container.doLayout(), bci=5
 - com.sun.lwuit.Container.layoutContainer(), bci=13
 - com.sun.lwuit.Container.doLayout(), bci=37
 - com.sun.lwuit.Container.layoutContainer(), bci=13
 - com.sun.lwuit.Form.initFocused(), bci=20
 - com.sun.lwuit.Form.show(), bci=25
 - com.sun.lwuit.Form.show(), bci=2
 - controler.SmartPhoneBanking.displayScreen(), bci=19
 - view.MenuPrincipalForm.actionPerformed(), bci=56
 - com.sun.lwuit.util.EventDispatcher.fireActionSync(), bci=19
 - com.sun.lwuit.util.EventDispatcher.fireActionEvent(), bci=89
 - com.sun.lwuit.Button.fireActionEvent(), bci=70
 - com.sun.lwuit.Button.released(), bci=17
 - com.sun.lwuit.Button.pointerReleased(), bci=1
 - com.sun.lwuit.Form.pointerReleased(), bci=93
 - com.sun.lwuit.Component.pointerReleased(), bci=7
 - com.sun.lwuit.Display.handleEvent(), bci=125
 - com.sun.lwuit.Display.edtLoopImpl(), bci=115
 - com.sun.lwuit.Display.mainEDTLoop(), bci=198
 - com.sun.lwuit.RunnableWrapper.run(), bci=242
 - java.lang.Thread.run(), bci=11

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

oО清风挽发oО 2024-12-14 13:37:01

一般来说,最好检查一下强制转换是否可行。在你的情况下,你似乎得到了一些不是向量的对象。

if (value != null && value instanceof Vector) {
    Vector vector = (Vector) value;

    ... your body with vector instead of value
} else {
    throw new IllegalArgumentException("Man I didn't get a Vector!");
}

您考虑过使用泛型类型吗?

In general it's a good idea to check whether cast are possible or not. In your case you seem to get some object which is not a Vector.

if (value != null && value instanceof Vector) {
    Vector vector = (Vector) value;

    ... your body with vector instead of value
} else {
    throw new IllegalArgumentException("Man I didn't get a Vector!");
}

Did you consider using generic types?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文