简体   繁体   English

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

[英]Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException error

When I run a method that should download the item from the database, the following error will appear: 当我运行应从数据库下载项目的方法时,将出现以下错误:

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)

This is my class com.dke.ps.Tables.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 + " ]";
    }

}

And there is my com.dke.ps.Items.Item 还有我的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;

}

I really do not know where the problem is. 我真的不知道问题出在哪里。 Could anyone please explain to me the exception? 有人可以向我解释例外情况吗?

BTW BTW

The is the getDbItems() method: 是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();

The expection show in the this: 期望显示在:

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);
        }

The first class was created through Persistence - Entity Classes from database and the second one I created. 第一个类是通过“持久性-来自数据库的实体类”创建的,第二个是我创建的。 If I changed array list to ArrayList dbItems it throws me the error: no suitable constructor found for ArrayList. 如果我将数组列表更改为ArrayList dbItems,则会抛出错误:找不到适合ArrayList的构造函数。 Constructor ArrayList.ArrayList(int) is not aplicable 构造函数ArrayList.ArrayList(int)不适用

Thanks for help! 感谢帮助!

The issue is that "listOfDbItems.get(i)" returns com.dke.ps.Tables.Item whereas "dlmItems" expecting "com.dke.ps.Items.Item". 问题是“ listOfDbItems.get(i)”返回com.dke.ps.Tables.Item,而“ dlmItems”期望“ com.dke.ps.Items.Item”。 You should change the DefaultListModel dlmItems = new DefaultListModel() to accept com.dke.ps.Items.Item or maybe convert Tables.Item objects into Items.Item object before adding to dlmItem. 您应该更改DefaultListModel dlmItems = new DefaultListModel()以接受com.dke.ps.Items.Item,或者在将Tables.Item对象转换为Items.Item对象之前,将其添加到dlmItem。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:线程“ AWT-EventQueue-0”中的异常java.lang.ClassCastException: - Error :Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: 线程“ AWT-EventQueue-0”中的java异常java.lang.ClassCastException - java Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException 线程“AWT-EventQueue-0”中的异常java.lang.ClassCastException - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException 线程“ AWT-EventQueue-0”中的异常java.lang.ClassCastException:add()异常 - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: Exception with add() 线程“AWT-EventQueue-0”中的异常java.lang.ClassCastException:java.util.ArrayList - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: java.util.ArrayList 我的JFrame类中的线程“AWT-EventQueue-0”java.lang.ClassCastException中的异常 - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException in my JFrame class 线程“AWT-EventQueue-0”中的异常 java.lang.ClassCastException:javax.swing.JTable - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: javax.swing.JTable 错误:线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - ERROR : Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException 线程“ AWT-EventQueue-0”中的异常java.lang.ArrayIndexOutOfBoundsException:-1错误 - Exception in thread “AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException: -1 error 错误“线程“AWT-EventQueue-0”中的异常java.lang.NumberFormatException” - Error "Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM