Results 1 to 8 of 8
  1. #1
    niteangell21 is offline Member
    Join Date
    Sep 2010
    Posts
    40
    Rep Power
    0

    Default [Ljava.lang.Object; cannot be cast to domain.Customer

    Hi guys,
    I am new to Java, working on my 2nd class. I have a project where I use hibernate to read in/write into a DB. I use Net beans 6.9.1.
    I have a customer object wiht the following fields.

    private int id;
    private String name = null;
    public Task task = null;
    public List<Task> TaskList = new ArrayList<Task>();
    public List<Customer> customerList = new ArrayList<Customer>();

    I can write into DB without any problem, when I try to read back in, I get this error.

    j"ava.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to domain.Customer"

    I dont know whats going on. I am reading back in just 'id and name' into Customer object. Customer has a reference to task object as well as 2 array lists.
    Last edited by niteangell21; 09-26-2010 at 07:56 PM.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    I wish I were magic, but I'm not; we'll have to see the code where the data is read from the DB if you want to diagnose the problem.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by niteangell21 View Post
    Hi guys,
    I am new to Java, working on my 2nd class. I have a project where I use hibernate to read in/write into a DB. I use Net beans 6.9.1.
    I have a customer object wiht the following fields.

    private int id;
    private String name = null;
    public Task task = null;
    public List<Task> TaskList = new ArrayList<Task>();
    public List<Customer> customerList = new ArrayList<Customer>();

    I can write into DB without any problem, when I try to read back in, I get this error.

    j"ava.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to domain.Customer"

    I dont know whats going on. I am reading back in just 'id and name' into Customer object. Customer has a reference to task object as well as 2 array lists.
    [Ljava.lang.Object is the 'internal' name of an array of type Object, i.e. Object[]; it looks like the serialization of a List; did you write (serialize) an entire List<Customer>?

    kind regards,

    Jos

  4. #4
    niteangell21 is offline Member
    Join Date
    Sep 2010
    Posts
    40
    Rep Power
    0

    Default

    sorry I am attaching my code.

    this is my domain object.

    package domain;

    import java.io.Serializable;

    import java.util.*;

    public class Customer implements Serializable {

    private static final long serialVersionUID = 1L;

    private int id;
    private String name = null;
    public Task task = null;
    public List<Task> TaskList = new ArrayList<Task>();
    public List<Customer> customerList = new ArrayList<Customer>();


    public Customer() {

    }

    public void setId(int id){
    this.id=id;
    }
    public int getId(){
    return this.id;
    }



    public String getName() {
    return name;
    }

    public void setName(String aName) {
    name = aName;
    }

    public void setTask(Task atask) {
    task = atask;
    }

    public Task getTask() {
    return task;
    }

    public boolean validate() {

    if (name == null) {
    return false;
    }
    if (task == null) {
    return false;
    }

    return true;
    }


    This is where I read data from DB
    ----------------------------------



    package services;

    import domain.Customer;

    import exception.*;
    import org.apache.log4j.*;
    import java.util.*;
    import java.sql.*;
    import org.hibernate.*;
    import org.hibernate.cfg.*;
    import org.hibernate.util.*;



    public Customer readCustomer(Customer customer)
    throws CustomerNotFoundException {

    try {
    Session session = getSession();
    Transaction tranx = session.beginTransaction();

    SQLQuery sqlQuery = session.createSQLQuery("select * from customer");

    List resultList = sqlQuery.list();
    tranx.commit();
    session.close();

    Iterator iter = resultList.iterator();
    while (iter.hasNext()) {

    Customer c = new Customer();
    c = (Customer) iter.next();
    System.out.println(" customer name is :" + c.getName());
    }

    Hope this helps!!
    Last edited by niteangell21; 09-26-2010 at 08:41 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Which line has the error in it?
    You need to post the full text of the error message which contains the source line number where the error happens.

  6. #6
    niteangell21 is offline Member
    Join Date
    Sep 2010
    Posts
    40
    Rep Power
    0

    Default

    This is my output
    ----------------


    run:
    2010-09-26 12:34:41,187 [AWT-EventQueue-0] INFO business.Manager - getting Service instance from factory through Manager
    2010-09-26 12:34:41,187 [AWT-EventQueue-0] INFO services.Factory - Getting service from factory now
    16 [AWT-EventQueue-0] INFO org.hibernate.cfg.Environment - Hibernate 3.5.6-Final
    16 [AWT-EventQueue-0] INFO org.hibernate.cfg.Environment - hibernate.properties not found
    31 [AWT-EventQueue-0] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
    31 [AWT-EventQueue-0] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
    2010-09-26 12:34:41,390 [AWT-EventQueue-0] INFO business.CustomerManager - storing customer name using Manager
    203 [AWT-EventQueue-0] INFO services.CustomerServiceHibernateImpl - Inside store customer hibernate file!
    156 [AWT-EventQueue-0] INFO org.hibernate.cfg.Configuration - configuring from resource: hibernate.cfg.xml
    156 [AWT-EventQueue-0] INFO org.hibernate.cfg.Configuration - Configuration resource: hibernate.cfg.xml
    281 [AWT-EventQueue-0] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : Customer.hbm.xml
    391 [AWT-EventQueue-0] INFO org.hibernate.cfg.HbmBinder - Mapping class: domain.Customer -> customer
    406 [AWT-EventQueue-0] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
    484 [AWT-EventQueue-0] INFO org.hibernate.connection.DriverManagerConnectionPr ovider - Using Hibernate built-in connection pool (not for production use!)
    484 [AWT-EventQueue-0] INFO org.hibernate.connection.DriverManagerConnectionPr ovider - Hibernate connection pool size: 20
    484 [AWT-EventQueue-0] INFO org.hibernate.connection.DriverManagerConnectionPr ovider - autocommit mode: false
    500 [AWT-EventQueue-0] INFO org.hibernate.connection.DriverManagerConnectionPr ovider - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/priya
    500 [AWT-EventQueue-0] INFO org.hibernate.connection.DriverManagerConnectionPr ovider - connection properties: {user=root, password=****}
    891 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - RDBMS: MySQL, version: 5.1.50-community
    891 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.13 ( Revision: ${bzr.revision-id} )
    937 [AWT-EventQueue-0] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQLDialect
    937 [AWT-EventQueue-0] INFO org.hibernate.engine.jdbc.JdbcSupportLoader - Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
    937 [AWT-EventQueue-0] INFO org.hibernate.transaction.TransactionFactoryFactor y - Using default transaction strategy (direct JDBC transactions)
    953 [AWT-EventQueue-0] INFO org.hibernate.transaction.TransactionManagerLookup Factory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
    953 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
    953 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
    953 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
    953 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
    953 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
    953 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
    953 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
    953 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2
    953 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
    953 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
    953 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
    953 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
    969 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
    969 [AWT-EventQueue-0] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
    969 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
    969 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
    969 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
    969 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
    969 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
    969 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
    969 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
    984 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
    984 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
    984 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
    984 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
    984 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
    984 [AWT-EventQueue-0] INFO org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled
    1031 [AWT-EventQueue-0] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
    1359 [AWT-EventQueue-0] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
    Hibernate: select max(id) from customer
    Hibernate: insert into customer (name, id) values (?, ?)
    2010-09-26 12:34:42,734 [AWT-EventQueue-0] INFO business.Manager - getting Service instance from factory through Manager
    1547 [AWT-EventQueue-0] INFO business.Manager - getting Service instance from factory through Manager
    2010-09-26 12:34:42,734 [AWT-EventQueue-0] INFO services.Factory - Getting service from factory now
    1547 [AWT-EventQueue-0] INFO services.Factory - Getting service from factory now
    Sep 26, 2010 12:34:42 PM presentation.LoginUI1$1 actionPerformed
    SEVERE: null

    IGNORE THE BELOW EXCEPTION AS I HAVENT IMPLEMENTED THAT YET.(TASK SERVICE )
    --------------------------------------------------------------------
    exception.ServiceLoadException: ITaskServicenot loaded
    at services.Factory.getService(Factory.java:25)
    at business.Manager.getService(Manager.java:16)
    at business.CustomerManager.create(CustomerManager.ja va:36)
    at presentation.LoginUI1$1.actionPerformed(LoginUI1.j ava:82)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 2)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 5992010-09-26
    CONTINUE FROM HERE ONWARDS
    ------------------------------
    12:34:42,875 [AWT-EventQueue-0] INFO business.Manager - getting Service instance from factory through Manager
    )
    1688 [AWT-EventQueue-0] INFO business.Manager - getting Service instance from factory through Manager
    2010-09-26 12:34:42,875 [AWT-EventQueue-0] INFO services.Factory - Getting service from factory now
    1688 [AWT-EventQueue-0] INFO services.Factory - Getting service from factory now
    2010-09-26 12:34:42,875 [AWT-EventQueue-0] INFO business.CustomerManager - reading customer name using Manager
    1688 [AWT-EventQueue-0] INFO business.CustomerManager - reading customer name using Manager
    1688 [AWT-EventQueue-0] INFO services.CustomerServiceHibernateImpl - Inside read customer hibernate!
    1688 [AWT-EventQueue-0] INFO services.CustomerServiceHibernateImpl - Inside read customer hibernate!
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    Hibernate: select * from customer
    1750 [AWT-EventQueue-0] INFO services.CustomerServiceHibernateImpl - INFO -

    THIS IS THE ERROR
    --------------------
    Exception: [Ljava.lang.Object; cannot be cast to domain.Customer
    1750 [AWT-EventQueue-0] INFO services.CustomerServiceHibernateImpl - INFO -
    java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to domain.Customer
    at services.CustomerServiceHibernateImpl.readCustomer (CustomerServiceHibernateImpl.java:66)
    at business.CustomerManager.readCustomer(CustomerMana ger.java:25)
    at presentation.LoginUI1$1.actionPerformed(LoginUI1.j ava:97)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 2)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    2010-09-26 12:34:42,968 [AWT-EventQueue-0] INFO business.Manager - getting Service instance from factory through Manager
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    1781 [AWT-EventQueue-0] INFO business.Manager - getting Service instance from factory through Manager
    1781 [AWT-EventQueue-0] INFO business.Manager - getting Service instance from factory through Manager
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    2010-09-26 12:34:42,968 [AWT-EventQueue-0] INFO services.Factory - Getting service from factory now
    1781 [AWT-EventQueue-0] INFO services.Factory - Getting service from factory now
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    1781 [AWT-EventQueue-0] INFO services.Factory - Getting service from factory now
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    exception.ServiceLoadException: ITaskServicenot loaded
    at services.Factory.getService(Factory.java:25)
    at business.Manager.getService(Manager.java:16)
    at business.CustomerManager.readTask(CustomerManager. java:46)
    at presentation.LoginUI1$1.actionPerformed(LoginUI1.j ava:113)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 2)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    .readCustomer (CustomerServiceHibernateImpl.java:66)
    What code is at line 66 in CustomerServiceHibernateImpl.java?

    c = (Customer) iter.next();

    Add some debug code to print out the value returned by the next method to show what is being returned.
    Last edited by Norm; 09-26-2010 at 09:02 PM.

  8. #8
    niteangell21 is offline Member
    Join Date
    Sep 2010
    Posts
    40
    Rep Power
    0

    Default

    Never mind . i fixed it.
    It was actually the query i used to read data which generated the error.

    SQLQuery sqlQuery = session.createSQLQuery("select * from customer");

    I modified it and it worked just fine...

    SQLQuery sqlQuery = session.createSQLQuery("select * from customer").addEntity(Customer.class);

    Thanks for all your responses and help!!

Similar Threads

  1. Replies: 5
    Last Post: 03-17-2011, 03:26 PM
  2. How can I cast object to Vector?
    By batya in forum AWT / Swing
    Replies: 2
    Last Post: 11-19-2009, 06:02 PM
  3. [SOLVED] Ljava.lang.String;@923e30 at runtime
    By Jerome in forum New To Java
    Replies: 6
    Last Post: 12-31-2008, 12:08 PM
  4. Replies: 1
    Last Post: 08-02-2007, 05:07 PM
  5. How can I cast Object as an int
    By romina in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 12:20 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •