Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: HELP: I have problem casting from Vector to Integer in Java

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default HELP: I have problem casting from Vector to Integer in Java

    Hello!
    I have a function which queries all users from the database and prints their details, this function calls the line

    Integer usrId=(Integer)((Vector)results.get(i)).get(0);

    which converts the results set into vector, it gets a corresponding vector and gets the first element which corresponds to the id and then casts it as interger, but when i run it, this line gives errors like:

         com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)
         [java] Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.Vector
         [java]     at ejb.UserDirectory.printUsers(Unknown Source)
         [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

    Now i wonder how else can i implement this method? For it worked before and now its giving me errors. Please help.

    Here is the complete code for the method:


    public String printUsers(){
     
    		Query q = em.createNativeQuery("select id from EntityObject");
     
    	List results = q.getResultList();
     
          		String str=new String();
          		EntityObject msgUser;      
          		int i;
     
    		for (i=0;i<results.size();i++) {
            		Integer usrId=(Integer)((Vector)results.get(i)).get(0);// This line gives errors
            		msgUser=em.find(EntityObject.class,usrId);
            		str=str+msgUser.toStringShort()+"\n";
    		}
           		return(str);
           	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HELP: I have problem casting from Vector to Integer in Java

    Separate your compound statement up into single, simple statements to get the correct type for each usage.

  3. The Following User Says Thank You to Norm For This Useful Post:

    tintin (November 17th, 2011)

  4. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: HELP: I have problem casting from Vector to Integer in Java

    Norm, Thanks a lot for a reply
    I separated the elements as you advised, but i still get the same result: Here is the new code after i modified

    public String printUsers(){
     
    		Query q = em.createNativeQuery("select id from EntityObject");
     
    	List results = q.getResultList();
     
          		String str=new String();
          		EntityObject msgUser;      
          	       String str=new String();
                   MessageUser msgUser;      
                    List element; //This new variable holds the current value from list
                  int i;
     
            for (i=0;i<results.size();i++) {
                //Integer usrId=(Integer)((Vector)results.get(i)).get(0);
     
                       element = (Vector) results.get(i);
                       Integer usrId = (Integer) element.get(0); //This still gives errors
     
                       msgUser=em.find(EntityObject.class,usrId);
                    str=str+msgUser.toStringShort()+"\n";
            }
           		return(str);
    }
    Last edited by tintin; November 17th, 2011 at 08:34 AM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HELP: I have problem casting from Vector to Integer in Java

    When you get an error, please copy and paste here the full text of the error.

    Print out the value of the variables that are being used to be sure that they are what you expect. Use the getClass() method.
    Last edited by Norm; November 17th, 2011 at 08:48 AM.

  6. The Following User Says Thank You to Norm For This Useful Post:

    tintin (November 17th, 2011)

  7. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: HELP: I have problem casting from Vector to Integer in Java

    Norm, this is the error i get when i run the code above.
    I dont know how to use the getClass() method in my code, as it shows me there are incompartible types.
    Thanks for the reply, i appreciate

    [java] javax.ejb.EJBException
         [java] 	at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5193)
         [java] 	at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5091)
         [java] 	at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4879)
         [java] 	at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2039)
         [java] 	at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1990)
         [java] 	at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:213)
         [java] 	at com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:79)
         [java] 	at $Proxy202.printUsers(Unknown Source)
         [java] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         [java] 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         [java] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         [java] 	at java.lang.reflect.Method.invoke(Method.java:597)
         [java] 	at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie.dispatchToMethod(ReflectiveTie.java:144)
         [java] 	at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:174)
         [java] 	at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatchToServant(CorbaServerRequestDispatcherImpl.java:528)
         [java] 	at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatch(CorbaServerRequestDispatcherImpl.java:199)
         [java] 	at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequest(CorbaMessageMediatorImpl.java:1624)
         [java] 	at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:1486)
         [java] 	at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleInput(CorbaMessageMediatorImpl.java:990)
         [java] 	at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:214)
         [java] 	at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:742)
         [java] 	at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.dispatch(CorbaMessageMediatorImpl.java:539)
         [java] 	at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.doWork(CorbaMessageMediatorImpl.java:2324)
         [java] 	at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
         [java] 	at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)
         [java] Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.Vector
         [java] 	at ejb.UserDirectory.printUsers(Unknown Source)
         [java] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         [java] 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         [java] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         [java] 	at java.lang.reflect.Method.invoke(Method.java:597)
         [java] 	at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
         [java] 	at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
         [java] 	at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5366)
         [java] 	at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
         [java] 	at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
         [java] 	at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
         [java] 	at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
         [java] 	at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)
         [java] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         [java] 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         [java] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         [java] 	at java.lang.reflect.Method.invoke(Method.java:597)
         [java] 	at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)
         [java] 	at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
         [java] 	at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370)
         [java] 	at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5338)
         [java] 	at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5326)
         [java] 	at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:206)
         [java] 	... 19 more

  8. #6
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Thumbs up Re: HELP: I have problem casting from Vector to Integer in Java [SOLVED]

    Norm, thanks a lot for the replies, I got a solution to it.
    I guess i was casting it from Integet to vector in the first place, which was supposed to be a straight way from List to getting the first element, hence i was going the wrong way at first.

    Thanks a lot for your help.
     
    public String printUsers(){
     
    		Query q = em.createNativeQuery("select id from EntityObject");
     
    	List results = q.getResultList();
     
          		String str=new String();
          		EntityObject msgUser;      
          	       String str=new String();
                   MessageUser msgUser;      
                    List element; //This new variable holds the current value from list
                  int i;
     
            for (i=0;i<results.size();i++) {
                         Integer usrId =(Integer) results.get(i);        //This was modified and gives the correct value now          
                       msgUser=em.find(EntityObject.class,usrId);
                    str=str+msgUser.toStringShort()+"\n";
            }
           		return(str);
    }
    Last edited by tintin; November 17th, 2011 at 11:20 AM.

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HELP: I have problem casting from Vector to Integer in Java

    how to use the getClass() method
    getClass() is a method in the Object class that you can use it with any object to get and display the class of any object. Your code was having a casting problem. The error message should show you the classes that were involved. Printing the results from getClass() can help you debug the casting problem.

  10. The Following User Says Thank You to Norm For This Useful Post:

    tintin (November 17th, 2011)

  11. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: HELP: I have problem casting from Vector to Integer in Java

    Okay,
    Thanks for the description. It has been helpful.
    Thanks a lot, i was actually doing the operations which were used for Vector methods and obsolete for List, that's what i discovered when i came to find a solution. With a vector, i had to cast and do all those operations, but with List it was simple and straight, so it is now working.

Similar Threads

  1. [SOLVED] Java Reflection, laoding external classes and casting
    By ashenwolf in forum Java Theory & Questions
    Replies: 3
    Last Post: May 10th, 2011, 12:33 PM
  2. Replies: 2
    Last Post: December 22nd, 2010, 09:21 AM
  3. how to read an integer of DOUBLE datatype with type casting
    By amr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2010, 03:03 PM
  4. Hex to Integer Problem
    By TempSpectre in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2010, 06:01 AM
  5. Problem about Vector
    By tohid in forum Collections and Generics
    Replies: 1
    Last Post: March 26th, 2010, 01:44 PM

Tags for this Thread