线程“AWT-EventQueue-0”中的异常java.lang.ClassCastException错误

时间:2017-05-29 15:48:21

标签: java jpa

当我运行一个应该从数据库下载项目的方法时,将出现以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.dke.ps.Tables.Item cannot be cast to com.dke.ps.Items.Item

这是我的课程com.dke.ps.Tables.Item -

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dke.ps.Tables;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author michal
 */
@Entity
@Table(name = "item")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Item.findAll", query = "SELECT i FROM Item i")
    , @NamedQuery(name = "Item.findByItemid", query = "SELECT i FROM Item i WHERE i.itemid = :itemid")
    , @NamedQuery(name = "Item.findByName", query = "SELECT i FROM Item i WHERE i.name = :name")
    , @NamedQuery(name = "Item.findByDescription", query = "SELECT i FROM Item i WHERE i.description = :description")
    , @NamedQuery(name = "Item.findByIcon", query = "SELECT i FROM Item i WHERE i.icon = :icon")
    , @NamedQuery(name = "Item.findByType", query = "SELECT i FROM Item i WHERE i.type = :type")
    , @NamedQuery(name = "Item.findByPrice", query = "SELECT i FROM Item i WHERE i.price = :price")})
public class Item implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "itemid")
    private Integer itemid;
    @Column(name = "name")
    private String name;
    @Column(name = "description")
    private String description;
    @Column(name = "icon")
    private String icon;
    @Column(name = "type")
    private Integer type;
    @Column(name = "price")
    private Integer price;

    public Item() {
    }

    public Item(Integer itemid) {
        this.itemid = itemid;
    }

    public Integer getItemid() {
        return itemid;
    }

    public void setItemid(Integer itemid) {
        this.itemid = itemid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (itemid != null ? itemid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Item)) {
            return false;
        }
        Item other = (Item) object;
        if ((this.itemid == null && other.itemid != null) || (this.itemid != null && !this.itemid.equals(other.itemid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.dke.ps.Tables.Item[ itemid=" + itemid + " ]";
    }

}

还有我的com.dke.ps.Items.Item

/**
 * General class for an item containing common information sutch as id, name,
 * description, path to an icon, price and type of an item.
 * @author valecvit
 * @author koresmi1
 */
public abstract class Item
{
    /**
     * Unique id of an item.
     */
    public int itemid;
    /**
     * Name of an item.
     */
    public String name;
    /**
     * Description of an item.
     */
    public String description;
    /**
     * Relative path to item image.
     */
    public String icon;
    /**
     * Type of an item.
     */
    public int type;
    /**
     * Price of an item.
     */
    public int price;

}

我真的不知道问题出在哪里。有人可以向我解释这个例外吗?

顺便说一句

是getDbItems()方法:

ArrayList<Item> dbItems = new ArrayList<>();

        EntityTransaction entr=em.getTransaction();
            entr.begin();
        TypedQuery<Item> query = em.createQuery("SELECT i FROM Item i", Item.class);
        dbItems = new ArrayList<Item>(query.getResultList());
        em.getTransaction().commit();

此次展览中显示:

listOfDbItems = server.getDbItems();
        listOfUsersItems = server.getPurchasedItems(user);
        dlmItems.clear();

        int numberOfItemsInDb = listOfDbItems.size();

        for (int i = 0; i < numberOfItemsInDb; i++)
        {
            dlmItems.addElement((listOfDbItems.get(i)).name);
        }

完整堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.dke.ps.Tables.Item cannot be cast to com.dke.ps.Items.Item
    at com.dke.ps.Shop.Shop.loadItems(Shop.java:161)
    at com.dke.ps.Shop.Shop.init(Shop.java:122)
    at com.dke.ps.Shop.Shop.<init>(Shop.java:60)
    at com.dke.ps.Launcher.Launcher.lambda$initialize$4(Launcher.java:113)
    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.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    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:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    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)

0 个答案:

没有答案