This is a migrated thread and some comments may be shown as answers.
Problem Getting Data via Service and DAO Classes
3 Answers 26 Views
This is a migrated thread and some comments may be shown as answers.
Henry
Top achievements
Rank 1
Henry asked on 01 May 2020, 05:30 PM

I'm running a trial with Kendo UI for JSP 2020.1.406

I've been following the instructions here: https://docs.telerik.com/kendo-ui/jsp/introduction?DisableOverride=true&utm_medium=email&utm_source=eloqua&utm_campaign=dt-jsp-trials

This got me set up with a base project, to which I was able to add a JSP page and some CSS to style it.  Now I want to add a dropdown list and populate it with some data.  The stuff that gets the data is all from an existing project.  Basically, I'm making a call to a Service class and then to a query in a Dao class.

This is my main controller:

package com.agristats.transfersystem.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.agristats.transfersystem.bo.Program;
import com.agristats.transfersystem.service.ProgramService;

@Controller
public class MainController {

@RequestMapping("/")
public String main() {
return "main";
}


}

This is working.  Then, following the 2nd introductory video on that referenced page, I added this servlet to an api package:

package com.agristats.transfersystem.api;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.agristats.transfersystem.bo.Program;
import com.agristats.transfersystem.service.ProgramService;
import com.google.gson.Gson;

/**
 * Servlet implementation class Programs
 */
@WebServlet("/api/programs")
public class Programs extends HttpServlet {
private static final long serialVersionUID = 1L;
    private final ProgramService programService;
    
    private List<Program> programs;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Programs() {
        this.programService = new ProgramService();
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// response.getWriter().append("Served at: ").append(request.getContextPath());

try {
programs = programService.findAllWantedPrograms();

Gson gson = new Gson();

response.setContentType("application/json");

response.getWriter().write(gson.toJson(programs));
} catch (IOException e) {
response.sendError(500);
e.printStackTrace();
}
}


/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

This fails here:  programs = programService.findAllWantedPrograms();

With this error:

java.lang.NullPointerException: null
at com.agristats.transfersystem.service.ProgramService.findAllWantedPrograms(ProgramService.java:32) ~[classes/:0.0.1-SNAPSHOT]
at com.agristats.transfersystem.api.Programs.doGet(Programs.java:41) ~[classes/:0.0.1-SNAPSHOT]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[servlet-api.jar:na]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[servlet-api.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [catalina.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.13]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-websocket.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.13]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.13]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.13]
at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:128) [spring-boot-2.3.0.M4.jar:2.3.0.M4]
at org.springframework.boot.web.servlet.support.ErrorPageFilter.access$000(ErrorPageFilter.java:66) [spring-boot-2.3.0.M4.jar:2.3.0.M4]
at org.springframework.boot.web.servlet.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:103) [spring-boot-2.3.0.M4.jar:2.3.0.M4]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:121) [spring-boot-2.3.0.M4.jar:2.3.0.M4]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.13]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:9.0.13]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:9.0.13]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) [catalina.jar:9.0.13]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [catalina.jar:9.0.13]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [catalina.jar:9.0.13]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [catalina.jar:9.0.13]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [catalina.jar:9.0.13]
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668) [catalina.jar:9.0.13]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [catalina.jar:9.0.13]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [catalina.jar:9.0.13]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-coyote.jar:9.0.13]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-coyote.jar:9.0.13]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791) [tomcat-coyote.jar:9.0.13]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417) [tomcat-coyote.jar:9.0.13]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-coyote.jar:9.0.13]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_163]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_163]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:9.0.13]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_163]

It appears that when the code gets into the Service class and tries to run the Dao query, the dao object is empty.  

Am I missing something on the Spring side of this?  Can this not be done within the Servlet?  Is the method outlined in the video no longer valid (it is seven years old)?  Is there a better way to do this?  Any help would be most appreciated.

3 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 05 May 2020, 10:55 AM

Hi Henry,

 

When binding the DropDownList to a remote service it will make an ajax request to retrieve the data. You just need to have an action method that will return the data as JSON and point the dropdown to that method. Check out the resources below that illustrate how the widget can be bound to remote data:

https://docs.telerik.com/kendo-ui/jsp/tags/dropdownlist/ajax-binding

https://demos.telerik.com/jsp-ui/dropdownlist/remotedatasource

 

That said, from the available information it seems that the error you are seeing is not directly related to the DropDownList component. I did a quick search and found the following stackoverflow thread that elaborates in detail about the error:

https://stackoverflow.com/a/218510

 

Regards,
Viktor Tachev
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Henry
Top achievements
Rank 1
answered on 05 May 2020, 11:13 AM

I did try the Ajax binding method yesterday.  I wasn't able to implement it, however, because I am missing these libraries:

import com.kendoui.spring.models.DataSourceRequest;

import com.kendoui.spring.models.DataSourceResult;

I'm working with the free trial.  I basically followed the instructions on this page: https://docs.telerik.com/kendo-ui/jsp/introduction

Nothing in that instructs me to bring in these com.kendoui.spring.models files.  Maybe I'll look into the spring-demos project and see if they are there.

It's odd to me that there's such a difference between the instructions given on the Getting Started page videos and the actual Demo code. 

0
Viktor Tachev
Telerik team
answered on 07 May 2020, 09:28 AM

Hi Henry,

 

The trial version of the components include both DataSourceRequest and DataSourceResult. You can see them in the offline examples as well. 

The videos you are referring to are using an older Kendo version. I will note this and we will update the getting article as soon as possible.

 

Regards,
Viktor Tachev
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Asked by
Henry
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Henry
Top achievements
Rank 1
Share this question
or