This question has already been answered:

I get by id but by sql or hql and the criteria goes NPE.and when displayed in jsp instead of letters question marks .. I tried to configure the server.xml file in tomcat in UTF-8, src, jsp everything in UTF-8.spring 4.1. 1 hibernate 4.3.5. Help please. Already the third day I suffer. and here is the code.

**MAIN.JS** $('#button2').click( function(){ $.ajax({ url:"getcity", type : "GET", contentType:"application/text", success:function(City){ if(City!=null){ $('#result').html(City); }else{$('#result').html("ОШИБКААААААА!!!!!");} } }) }) **CONTROLLER** @ResponseBody @RequestMapping(value = "/getcity",method = RequestMethod.GET) public String getCityPoSql() { cities cit = cityService.getCity(); return"<h1>" + cit.toString() + "</h1>"; } } **DAO** @Transactional(readOnly = true) public cities getCity() { Criteria criteria = sessionFactory.getCurrentSession().createCriteria(cities.class); criteria.add(eq("city", "Москва")); cities cit =(cities) criteria.uniqueResult(); return cit ; } } 

cityService is not null..and getCity returns null..base in UTF-8 /// config sessionFactory ..

 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url"> <value>jdbc:mysql://localhost:3306/cities</value> </property> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="com.plan.model"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> 

If in criteria query here so to write: actually so

 @Transactional(readOnly = true) public cities getCityIdSql() { Criteria criteria = sessionFactory.getCurrentSession().createCriteria(cities.class); cities cit =(cities) criteria.add(Restrictions.eq("city","Москва")).uniqueResult(); return cit ; } } 

whether it should return all other fields where city is Moscow .. in my case it is a string and returns NPE. there is such a field and the value is

 **Stacktrace** org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) root cause java.lang.NullPointerException com.plan.pn.cityController.getCityPoSql(cityController.java:42) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:606) org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.14 logs. Apache Tomcat/7.0.14 

Reported as a duplicate by participants aleksandr barakin , user194374, cheops , Mr. Black , dirkgntly Aug 3 '16 at 8:41 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Please attach stack trace. - Slava Semushin
  • @Slava Semushin added - Altysh
  • You will also need at least: 1) Hibernate settings, in particular, we are interested in the settings for connecting to the database 2) What database are you using? 3) In what encoding is the data stored in the table? - Slava Semushin

1 answer 1

Let's first solve the problem with NullPointerException : judging by the stack trace, an exception occurs on these lines

  cities cit = cityService.getCity(); return"<h1>" + cit.toString() + "</h1>"; 

Therefore, there are two options that need to be checked / corrected:

  • cityService not null
  • cityService.getCity() returns null
  • cityService is not null; and cityService.getCity null; - Altysh
  • Then it would be nice to see how the cityService.getCity() method looks. - Slava Semushin
  • I hope you are not tired .. added the method .. - Altysh
  • one
    Here is a similar question on StackOverflow: stackoverflow.com/questions/2036965/ uniqueResult() They say that uniqueResult() returns null when the query to the database did not return anything. Therefore, you need to add a check for this case and, as advised, turn on logging and see what a real database query is executed and why it returns nothing. - Slava Semushin
  • I thought this is the line - Altysh 6:48 pm