'Why error is coming after adding javascript , bootstrap and CSS path in list-todos.jsp in Springboot
I am working on spring-boot project Code is working fine until I add javascript , bootstrap and CSS path in list-todos.jsp but after I add them and hit URL "http://localhost:8080/list-todos", it is giving error (NullPointerException). Also, if I remove those paths it is working fine.
list-todos.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title> Todo's for ${name}</title>
<link href="webjars/bootstrap/3.3.6/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body>
<H1>Your Todos</H1>
<table>
<caption>Your todos are</caption>
<thead>
<tr>
<th>Description</th>
<th>Target Date</th>
<th>Is it Done?</th>
</tr>
</thead>
<tbody>
<c:forEach items="${todos}" var="todo">
<tr>
<td>${todo.desc}</td>
<td>${todo.targetDate}</td>
<td>${todo.done}</td>
</tr>
</c:forEach>
</tbody>
</table>
<Br/>
<a href = "/add-todo">Add a Todo</a>
<script src="webjars/jquery/1.9.1/jquery.min.js"></script>
<script src="webjars/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
TodoController.java
package com.in28minutes.springboot.web.controller;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.in28minutes.springboot.web.service.TodoService;
@Controller
@SessionAttributes("name")
public class TodoController {
@Autowired
TodoService service;
@RequestMapping(value="/list-todos", method = RequestMethod.GET)
public String showTodos( ModelMap model ) {
String name = (String) model.get("name");
model.put("todos", service.retrieveTodos(name));
return "list-todos";
}
@RequestMapping(value="/add-todo", method = RequestMethod.GET)
public String showAddTodos( ModelMap model ) {
return "todo";
}
@RequestMapping(value="/add-todo", method = RequestMethod.POST)
public String addTodo(ModelMap model, @RequestParam String desc){
service.addTodo((String) model.get("name"), desc, new Date(), false);
return "redirect:/list-todos";
}
}
TodoService.java
package com.in28minutes.springboot.web.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Service;
import com.in28minutes.springboot.web.model.Todo;
@Service
public class TodoService {
private static List<Todo> todos = new ArrayList<Todo>();
private static int todoCount = 3;
static {
todos.add(new Todo(1, "Rajeev", "Learn Spring MVC", new Date(),
false));
todos.add(new Todo(2, "Rajeev", "Learn Struts", new Date(), false));
todos.add(new Todo(3, "Rajeev", "Learn Hibernate", new Date(),
false));
}
public List<Todo> retrieveTodos(String user) {
List<Todo> filteredTodos = new ArrayList<Todo>();
for (Todo todo : todos) {
if (todo.getUser().equals(user)) {
filteredTodos.add(todo);
}
}
return filteredTodos;
}
public void addTodo(String name, String desc, Date targetDate,
boolean isDone) {
todos.add(new Todo(++todoCount, name, desc, targetDate, isDone));
}
public void deleteTodo(int id) {
Iterator<Todo> iterator = todos.iterator();
while (iterator.hasNext()) {
Todo todo = iterator.next();
if (todo.getId() == id) {
iterator.remove();
}
}
}
}
POM.XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.in28minutes.springboot.web</groupId>
<artifactId>spring-boot-first-web-application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-first-web-application</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>13.0.2</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.6</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Error in Compiler
: Forwarding to [/WEB-INF/jsp/todo.jsp]
2022-05-14 13:39:49.257 DEBUG 3120 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2022-05-14 13:39:52.036 DEBUG 3120 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : POST "/add-todo", parameters={masked}
2022-05-14 13:39:52.037 DEBUG 3120 --- [nio-8080-exec-6] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.in28minutes.springboot.web.controller.TodoController#addTodo(ModelMap, String)
2022-05-14 13:39:52.046 DEBUG 3120 --- [nio-8080-exec-6] o.s.web.servlet.view.RedirectView : View name 'redirect:', model {}
2022-05-14 13:39:52.048 DEBUG 3120 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : Completed 302 FOUND
2022-05-14 13:39:52.053 DEBUG 3120 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : GET "/list-todos", parameters={}
2022-05-14 13:39:52.053 DEBUG 3120 --- [nio-8080-exec-7] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.in28minutes.springboot.web.controller.TodoController#showTodos(ModelMap)
2022-05-14 13:39:52.057 DEBUG 3120 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Failed to complete request: java.lang.NullPointerException
2022-05-14 13:39:52.067 ERROR 3120 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
**These path, when I am removing them from "list-todos.jsp" code is working fine.
<script src="webjars/jquery/1.9.1/jquery.min.js"></script>
<script src="webjars/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="webjars/bootstrap/3.3.6/css/bootstrap.min.css"
rel="stylesheet">
Please let me know, if any additional information required. Please help me with the issue.
Update : Adding cdn is also resolving the problem. But want to find out what is wrong with this code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


