初心者です。エラー助けてください。
解決したいこと
サーブレット・JSPを使って、Webアプリで『つぶやき機能、いいねボタン、よくないねボタン』を作っているのですが、『いいねボタン、よくないねボタン』の構文でエラーが出てしまいます。解決方法を教えてください。
発生している問題・エラー
例外
org.apache.jasper.JasperException: [/WEB-INF/jsp/main.jsp] の処理中に行番号 [35] で
例外が発生しました。
32: <% } %>
33: <p>
34: <a href="/docoTsubu/IineIndex?action=like">いいね</a>:
35: <%= iine.getLike() %>人
36: <a href="/docoTsubu/IineIndex?action=dislike">よくないね</a>:
37: <%= iine.getDislike() %>人
38: <p/>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:618)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:515)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
servlet.Main.doGet(Main.java:43)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
根本原因
java.lang.NullPointerException
org.apache.jsp.WEB_002dINF.jsp.main_jsp._jspService(main_jsp.java:133)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
servlet.Main.doGet(Main.java:43)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
該当するソースコード(main.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="model.User,model.Mutter,java.util.List,model.Iine,model.IineLogic" %>
<%
User loginUser = (User) session.getAttribute("loginUser");
List<Mutter>mutterList = (List<Mutter>) application.getAttribute("mutterList");
String errorMsg = (String) request.getAttribute("errorMsg");
Iine iine = (Iine) application.getAttribute("iine");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>どこつぶ</title>
</head>
<body>
<h1>どこつぶメイン</h1>
<p>
<%= loginUser.getName() %>さん、ログイン中
<a href="/docoTsubu/Logout">ログアウト</a>
</p>
<p><a href="/docoTsubu/Main">更新</a></p>
<form action="/docoTsubu/Main" method="post">
<input type="text" name="text">
<input type="submit" value="つぶやく">
</form>
<% if(errorMsg != null) { %>
<p><%= errorMsg %></p>
<% } %>
<% for(Mutter mutter : mutterList) {%>
<p><%= mutter.getUserName() %>:<%= mutter.getText() %></p>
<% } %>
<p>
<a href="/docoTsubu/IineIndex?action=like">いいね</a>:
<%= iine.getLike() %>人
<a href="/docoTsubu/IineIndex?action=dislike">よくないね</a>:
<%= iine.getDislike() %>人
<p/>
</body>
</html>
該当するソースコード(モデルクラス、iine.java)
package model;
import java.io.Serializable;
///追加したモデルクラス
public class Iine implements Serializable{
private int like;
private int dislike;
public Iine() {
like = 0;
dislike = 0;
}
public int getLike() { return like; }
public void setLike(int like) {
this.like = like;
}
public int getDislike() { return dislike; }
public void setDislike(int dislike) {
this.dislike = dislike;
}
}
該当するソースコード(モデルクラス、IineLogic)
package model;
///追加したモデルクラス
public class IineLogic {
public void like(Iine iine) {
int count = iine.getLike();
iine.setLike(count + 1);
}
public void dislike(Iine iine) {
int count = iine.getDislike();
iine.setDislike(count + 1);
}
}
該当するソースコード(サーブレットクラス、iineIndex)
package servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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 model.Iine;
import model.IineLogic;
@WebServlet("/IineIndex")
public class IineIndex extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext application = this.getServletContext();
Iine iine = (Iine) application.getAttribute("iine");
if(iine == null) {
iine = new Iine();
}
request.setCharacterEncoding("UTF-8");
String action = request.getParameter("action");
IineLogic iineLogic = new IineLogic();
if(action != null && action.equals("like")) {
iineLogic.like(iine);
} else if(action != null && action.equals("dislike")) {
iineLogic.dislike(iine);
}
application.setAttribute("iine", iine);
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
dispatcher.forward(request, response);
}
}
該当するソースコード(モデルクラス,User.java)
package model;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private String pass;
public User() {}
public User(String name, String pass) {
this.name = name;
this.pass = pass;
}
public String getName() { return name; }
public String getPass() { return pass; }
}
該当するソースコード(サーブレットクラス,Logout.java)
package servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 javax.servlet.http.HttpSession;
@WebServlet("/Logout")
public class Logout extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.invalidate();
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/logout.jsp");
dispatcher.forward(request,response);
}
}
該当するソースコード(jspクラス,loginResult)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="model.User" %>
<%
User loginUser = (User) session.getAttribute("loginUser");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>どこつぶ</title>
</head>
</head>
<body>
<h1>とこつぶログイン</h1>
<% if(loginUser != null) { %>
<p>ログインに成功しました</p>
<p>ようこそ<%= loginUser.getName() %>さん</p>
<a href="/docoTsubu/Main">つぶやき投稿・閲覧へ</a>
<% } else { %>
<p>ログインに失敗しました</p>
<a href="/docoTsubu/">TOPへ</a>
<% } %>
</body>
</html>
該当するソースコード(jspクラス,logout.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>どこつぶ</title>
</head>
</head>
<body>
<h1>どこつぶログ敦夫</h1>
<p>ログアウトしました</p>
<a href="/docoTsubu/">トップへ</a>
</body>
</html>
該当するソースコード(サーブレットクラス,Login.java)
package servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 javax.servlet.http.HttpSession;
import model.LoginLogic;
import model.User;
@WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String pass = request.getParameter("pass");
User user = new User(name,pass);
LoginLogic loginLogic = new LoginLogic();
boolean isLogin = loginLogic.execute(user);
if(isLogin) {
HttpSession session = request.getSession();
session.setAttribute("loginUser", user);
}
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/loginResult.jsp");
dispatcher.forward(request, response);
}
}
該当するソースコード(モデルクラス,PostMutterLogic.java)
package model;
import java.util.List;
public class PostMutterLogic {
public void execute(Mutter mutter, List<Mutter> mutterList) {
mutterList.add(0,mutter);
}
}
該当するソースコード(サーブレットクラス,Main.java)
package servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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 javax.servlet.http.HttpSession;
import model.Mutter;
import model.PostMutterLogic;
import model.User;
@WebServlet("/Main")
public class Main extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext application = this.getServletContext();
List<Mutter> mutterList = (List<Mutter>) application.getAttribute("mutterList");
if(mutterList == null) {
mutterList = new ArrayList<Mutter>();
application.setAttribute("mutterList",mutterList);
}
HttpSession session = request.getSession();
User loginUser = (User) session.getAttribute("loginUser");
if(loginUser == null) {
response.sendRedirect("/docoTsubu/");
} else {
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
dispatcher.forward(request, response);}
}
protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
request.setCharacterEncoding("UTF-8");
String text = request.getParameter("text");
if(text != null && text.length() != 0) {
ServletContext application = this.getServletContext();
List<Mutter>mutterList = (List<Mutter>)application.getAttribute("mutterList");
HttpSession session = request.getSession();
User loginUser = (User) session.getAttribute("loginUser");
Mutter mutter = new Mutter(loginUser.getName(), text);
PostMutterLogic postMutterLogic = new PostMutterLogic();
postMutterLogic.execute(mutter, mutterList);
application.setAttribute("mutterList", mutterList);
} else {
request.setAttribute("errorMsg","つぶやきが入力されていません");
}
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
dispatcher.forward(request, response);
}
}
該当するソースコード(jspクラス,index.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>どこつぶ</title>
</head>
<body>
<h1>どこつぶへようこそ</h1>
<form action="/docoTsubu/Login" method="post">
ユーザー名:<input type="text" name="name"><br>
パスワード:<input type="password" name="pass"><br>
<input type="submit" value="ログイン">
</form>
</body>
</html>
自分で試したこと
ネット検索、参考書で調べたりしましたが、自力で解決できません。
どなたかご教授いただけませんでしょうか?
0