Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm using the following code to convert char[] to string, but it is throwing:

java.lang.ClassCastException: java.lang.String cannot be cast to [C

char[] charArray = user.getUserPassword();  
String userPassword = String.valueOf(charArray);

Here user.getUserPassword() returns char[]. I also tried the following:

String userPassword = new String(charArray);

But it also throws the same exception. (I'm using java 8.)

The complete stack trace;

java.lang.ClassCastException: java.lang.String cannot be cast to [C
at org.hibernate.type.descriptor.java.PrimitiveCharacterArrayTypeDescriptor.unwrap(PrimitiveCharacterArrayTypeDescriptor.java:40)
at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$1.doBind(VarcharTypeDescriptor.java:64)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:90)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:286)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:281)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:616)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1901)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1839)
at org.hibernate.loader.Loader.doQuery(Loader.java:910)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
at org.hibernate.loader.Loader.doList(Loader.java:2554)
at org.hibernate.loader.Loader.doList(Loader.java:2540)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)
at org.hibernate.loader.Loader.list(Loader.java:2365)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:497)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:236)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
at com.humworld.ccs.daoimpl.UserDAOImpl.login(UserDAOImpl.java:29)
at com.humworld.ccs.serviceimpl.UserServiceImpl.login(UserServiceImpl.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy37.login(Unknown Source)
at com.humworld.ccs.controller.LoginController.loginAuthentication(LoginController.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2476)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2465)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

This is the UserDAOImpl class;

@Repository("userDAO")
public class UserDAOImpl implements UserDAO{

@Autowired
private SessionFactory sessionFactory;

@SuppressWarnings("unchecked")
@Override
public List<Object[]> login(String UserName, String UserPassword) {

    Session session = sessionFactory.openSession();
    String hql = HqlConstant.UserQuery;
    Query query = session.createQuery(hql);
    query.setParameter("UserName", UserName);
    query.setParameter("UserPassword", UserPassword);
    List<Object[]> list = query.list();

    session.close();
    return list;
}
}

This is the HQL query;

from UserModel um inner join um.userRoleModel where um.UserName = :UserName and um.UserPassword = :UserPassword 
share|improve this question
3  
if user.getUserPassword() would return a String the Exception would make sense - please post code from User with that method – Jan Apr 26 at 11:25
    
I would check the signature of user.getUserPassword() one more time if I were you. – NilsH Apr 26 at 11:25
    
also: Line numbers (Exception / Code) might make sense to track this down – Jan Apr 26 at 11:25
    
Please provide the complete stack trace for a fast and correct answer. – Kachiex Apr 26 at 11:33
    
Here is the complete stack trace; – karthi Apr 26 at 11:40
up vote 1 down vote accepted

My theory on what happens:

You put a String into

 query.setParameter("UserPassword", UserPassword);

BUT

As you said, your userPassword property for your user Object is of type char[]

Now hibernate is a quite clever tool - it would recognize that object type for userPassword is char[] and then tries to set that into the generated query. But you passed a String.

Try this for once:

   query.setParameter("UserPassword", UserPassword.toCharArray());

in your login method.

share|improve this answer
    
Perfectly working, Thank you so much. – karthi Apr 26 at 12:48

Try use String userPassword = new StringBuilder() .append(user.getUserPassword()).toString();

share|improve this answer
    
This also throws the same exception. – karthi Apr 26 at 11:42
    
it wont solve problem as problem is related to casting from String to char[] not other way around – user902383 Apr 26 at 11:47

Try this:

String result=String.copyValueOf(charArray);
share|improve this answer
    
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review – madhead Apr 26 at 14:12
    
@madhead: how is this not an answer? It tries to directly address the error the OP reports. Voting to keep this answer. – gariepy Apr 26 at 15:42

If you want to cast a String to a char (array, I'm guessing). I used a for loop.

for (int i = 0; i < length; i++) {
  char[i] Letter = message1.charAt(i);
}

This was derived from my older code, but this should still convert the string message1 to a char Array... Tell me if this would help :P

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.