サーブレット、JSPの画面遷移でエラーが発生します。
HTTPステータス 500 - java.lang.NullPointerException
※質問登録画面でデータを入力し、質問一覧画面に遷移する際に
エラー発生します
不備、修正箇所等教えていただけたら幸いです。
java
1package servlet; 2 3import java.io.IOException; 4import java.util.List; 5 6import javax.servlet.RequestDispatcher; 7import javax.servlet.ServletException; 8import javax.servlet.annotation.WebServlet; 9import javax.servlet.http.HttpServlet; 10import javax.servlet.http.HttpServletRequest; 11import javax.servlet.http.HttpServletResponse; 12import javax.servlet.http.HttpSession; 13 14import model.GetQuestionListLogic; 15import model.PostQuestionLogic; 16import model.Question; 17/** 18 * Servlet implementation class Doctest 19 */ 20 21@WebServlet("/RegistQuestionServlet") 22public class RegistQuestionServlet extends HttpServlet { 23 private static final long serialVersionUID = 1L; 24 25/** 26* @see HttpServlet#doget(HttpServletRequest request, HttpServletResponse response) 27*/ 28protected void doGet(HttpServletRequest request, HttpServletResponse response) 29throws ServletException, IOException { 30//フォワード 31RequestDispatcher dispatcher = 32 request.getRequestDispatcher("/WEB-INF/jsp/QandARegist.jsp"); 33 dispatcher.forward(request, response); 34} 35 36/** 37* @see HttpServlet#dopost(HttpServletRequest request, HttpServletResponse response) 38*/ 39protected void doPost(HttpServletRequest request, HttpServletResponse response) 40throws ServletException, IOException { 41//文字コードエンコーディング 42request.setCharacterEncoding("UTF-8"); 43//リクエストパラメータ取得 44// int id = request.getParameter("id"); 45 String handle_name = request.getParameter("handle_name"); 46 String title = request.getParameter("title"); 47 String contents = request.getParameter("contents"); 48 int urgency = Integer.parseInt(request.getParameter("urgency")); 49 String edit_delete_key = request.getParameter("edit_delete_key"); 50 String regist_timestamp = request.getParameter("regist_timestamp"); 51 String update_timestamp = request.getParameter("update_timestamp"); 52 53// 質問リストに質問を追加 54 Question question = new Question(handle_name, title, contents, urgency, edit_delete_key, regist_timestamp, update_timestamp); 55 PostQuestionLogic postQuestionLogic = new PostQuestionLogic(); 56 postQuestionLogic.execute(question); 57 58//質問リスト取得 59GetQuestionListLogic getQuestionListLogic = new GetQuestionListLogic(); 60List<Question> questionList = getQuestionListLogic.execute(); 61 62//セッションスコープにbeansのquestionを保存 63HttpSession session = request.getSession(); 64session.setAttribute("handle_name", handle_name); 65session.setAttribute("title", title); 66session.setAttribute("contents", contents); 67session.setAttribute("urgency", urgency); 68session.setAttribute("edit_delete_key", edit_delete_key); 69session.setAttribute("regist_timestamp", regist_timestamp); 70session.setAttribute("update_timestamp", update_timestamp); 71 72//フォワード 73RequestDispatcher dispatcher =request.getRequestDispatcher("/WEB-INF/jsp/QandAList.jsp"); 74dispatcher.forward(request, response); 75 } 76} 77
jsp
1<!-- QandAList.jsp(質問一覧画面) --> 2<%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8" %> 4<%@ page import="model.Question"%> 5<% 6Question q = (Question)session.getAttribute("Question"); 7%> 8 9<!DOCTYPE html> 10<html lang="ja"> 11<head> 12 <meta charset="utf-8"> 13 <meta name="viewport" content=“width=device-width,initial-scale=1.0”> 14 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 15 <title>課題1</title> 16 <link rel="stylesheet" href="css/QandAList.css"> 17</head> 18<body> 19<header> 20 <div class="container"> 21 <p>質問一覧</p> 22 <a href="/QandASystem/RegistQuestionServlet">質問する</a> 23 </div> 24</header> 25<div class="main"> 26 <div class="container"> 27 <table border="1"> 28 <thead> 29 <tr> 30 <th class="no" width="7%">No</th> 31 <th width="15%">緊急度</th> 32 <th width="40%">タイトル</th> 33 <th>登録者</th> 34 <th>登録日</th> 35 <th>更新日</th> 36 </tr> 37 </thead> 38 <tbody> 39 <tr> 40 <td>5</td> 41 <td><%=q.getUrgency()%></td> 42 <td><%=q.getTitle()%></td> 43 <td><%=q.getHandle_name() %></td> 44 <td><%=q.getRegist_timestamp() %></td> 45 <td><%=q.getUpdate_timestamp() %></td> 46 </tr> 47 <tr> 48 <td>4</td> 49 <td><%=q.getUrgency()%></td> 50 <td><%=q.getTitle()%></td> 51 <td><%=q.getHandle_name() %></td> 52 <td><%=q.getRegist_timestamp() %></td> 53 <td><%=q.getUpdate_timestamp() %></td> 54 </tr> 55 <tr> 56 <td>3</td> 57 <td><%=q.getUrgency()%></td> 58 <td><%=q.getTitle()%></td> 59 <td><%=q.getHandle_name() %></td> 60 <td><%=q.getRegist_timestamp() %></td> 61 <td><%=q.getUpdate_timestamp() %></td> 62 </tr> 63 <tr> 64 <td>2</td> 65 <td><%=q.getUrgency()%></td> 66 <td><%=q.getTitle()%></td> 67 <td><%=q.getHandle_name() %></td> 68 <td><%=q.getRegist_timestamp() %></td> 69 <td><%=q.getUpdate_timestamp() %></td> 70 </tr> 71 <tr> 72 <td>1</td> 73 <td><%=q.getUrgency()%></td> 74 <td><%=q.getTitle()%></td> 75 <td><%=q.getHandle_name() %></td> 76 <td><%=q.getRegist_timestamp() %></td> 77 <td><%=q.getUpdate_timestamp() %></td> 78 </tr> 79 </tbody> 80 </table> 81 </div> 82</div> 83<script src="js/main.js"></script> 84</body> 85</html>
jsp
1<!-- QandARegist.jsp(質問登録画面) --> 2<%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8" %> 4 5<!DOCTYPE html> 6<html lang="ja"> 7<head> 8 <meta charset="utf-8"> 9 <title>質問登録画面</title> 10 <link rel="stylesheet" href="css/QandARegist.css"> 11</head> 12<body> 13 <header> 14 <div class="container"> 15 <div class="header-flex"> 16 <div class="header-left"> 17 <h3>質問登録</h3> 18 </div> 19 <div class="header-right"> 20 <a href="/QandASystem/QuestionListServlet class="btn">戻る</a> 21 </div> 22 </div> 23 </div> 24 </header> 25 <section class="question-form"> 26 <div class="container"> 27 <form action="/QandASystem/RegistQuestionServlet" method="post"> 28 <table border="1"> 29 <tr> 30 <th>名前(ハンドルネーム)</th> 31 <td><input type="text" class="inp"></td> 32 </tr> 33 <tr> 34 <th>タイトル</th> 35 <td><input type="text" class="inp"></td> 36 </tr> 37 <tr> 38 <th class="contents">内容</th> 39 <td><textarea rows="16"></textarea></td> 40 </tr> 41 <tr class="urgency"> 42 <th>緊急度</th> 43 <td> 44 <label><input type="radio" name="urgency" value="1">急いでいます</label> 45 <label><input type="radio" name="urgency" value="2">困ってます</label> 46 <label><input type="radio" name="urgency" value="3">いつでも</label> 47 </td> 48 </tr> 49 <tr> 50 <th>編集・削除キー</th> 51 <td><input type="text" class="inp"></td> 52 </tr> 53 </table> 54 <input type="submit" value="登録"> 55 </form> 56 </div> 57 </section> 58</body> 59</html>
あなたの回答
tips
プレビュー