質問編集履歴
1
コードを質問本文に記載しました
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -4,8 +4,127 @@ | |
| 4 4 | 
             
            画像では、関数の結果(H2 Databaseより値を取得)を配列に渡し、その結果をリクエストセッションに設定しているのですが、
         | 
| 5 5 | 
             
            Nullで設定されjspでNullPointerExceptionとなります。
         | 
| 6 6 |  | 
| 7 | 
            +
            
         | 
| 8 | 
            +
             | 
| 7 9 | 
             
            Nullになる理由はなぜでしょうか。
         | 
| 8 10 |  | 
| 9 11 | 
             
            開発環境:Eclipse
         | 
| 10 12 |  | 
| 13 | 
            +
            コードは以下となります。
         | 
| 14 | 
            +
            ```Java
         | 
| 15 | 
            +
            ・サーブレット
         | 
| 16 | 
            +
            @WebServlet("/Main")
         | 
| 17 | 
            +
            public class Main extends HttpServlet {
         | 
| 18 | 
            +
            	private static final long serialVersionUID = 1L;
         | 
| 19 | 
            +
                   
         | 
| 20 | 
            +
                /**
         | 
| 21 | 
            +
                 * @see HttpServlet#HttpServlet()
         | 
| 22 | 
            +
                 */
         | 
| 23 | 
            +
                public Main() {
         | 
| 24 | 
            +
                    super();
         | 
| 25 | 
            +
                    // TODO Auto-generated constructor stub
         | 
| 26 | 
            +
                }
         | 
| 27 | 
            +
             | 
| 28 | 
            +
            	/**
         | 
| 29 | 
            +
            	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         | 
| 30 | 
            +
            	 */
         | 
| 31 | 
            +
            	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         | 
| 32 | 
            +
            		// TODO Auto-generated method stub
         | 
| 33 | 
            +
            		GetMutterListLogic getMutterListLogic = new GetMutterListLogic();
         | 
| 34 | 
            +
            		List<Mutter> mutterList = getMutterListLogic.execute();
         | 
| 35 | 
            +
            		request.setAttribute("mutterList", mutterList);
         | 
| 36 | 
            +
            		
         | 
| 37 | 
            +
            		
         | 
| 38 | 
            +
            		HttpSession session = request.getSession();
         | 
| 39 | 
            +
            		User loginUser = (User) session.getAttribute("loginUser");
         | 
| 40 | 
            +
            		
         | 
| 41 | 
            +
            		if(loginUser == null) {
         | 
| 42 | 
            +
            			response.sendRedirect("/docoTsubu/");
         | 
| 43 | 
            +
            		}else {
         | 
| 44 | 
            +
            			RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
         | 
| 45 | 
            +
            			dispatcher.forward(request, response);
         | 
| 46 | 
            +
            		}
         | 
| 47 | 
            +
            	}
         | 
| 48 | 
            +
            	/**
         | 
| 49 | 
            +
            	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         | 
| 50 | 
            +
            	 */
         | 
| 51 | 
            +
            	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         | 
| 52 | 
            +
            		// TODO Auto-generated method stub
         | 
| 11 | 
            -
             | 
| 53 | 
            +
            		request.setCharacterEncoding("UTF-8");
         | 
| 54 | 
            +
            		String text = request.getParameter("text");
         | 
| 55 | 
            +
            		
         | 
| 56 | 
            +
            		if(text !=null && text.length() !=0) {
         | 
| 57 | 
            +
            							
         | 
| 58 | 
            +
            			HttpSession session = request.getSession();
         | 
| 59 | 
            +
            			User loginUser = (User)session.getAttribute("loginUser");
         | 
| 60 | 
            +
            			
         | 
| 61 | 
            +
            			Mutter mutter = new Mutter(loginUser.getName(),text);
         | 
| 62 | 
            +
            			PostMutterLogic postMutterLogic = new PostMutterLogic();
         | 
| 63 | 
            +
            			postMutterLogic.execute(mutter);						
         | 
| 64 | 
            +
            			
         | 
| 65 | 
            +
            		} else {
         | 
| 66 | 
            +
            			request.setAttribute("errorMsg", "入力されていません");
         | 
| 67 | 
            +
            	}
         | 
| 68 | 
            +
            	GetMutterListLogic getMutterListLogic = new GetMutterListLogic();
         | 
| 69 | 
            +
            	List<Mutter> mutterList = getMutterListLogic.execute();
         | 
| 70 | 
            +
            	request.setAttribute("mutterList", mutterList);
         | 
| 71 | 
            +
            				
         | 
| 72 | 
            +
            	RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
         | 
| 73 | 
            +
            	dispatcher.forward(request, response);
         | 
| 74 | 
            +
             | 
| 75 | 
            +
            	}
         | 
| 76 | 
            +
            }
         | 
| 77 | 
            +
             | 
| 78 | 
            +
            ・JSP
         | 
| 79 | 
            +
            <%@ page language="java" contentType="text/html; charset=UTF-8"
         | 
| 80 | 
            +
                pageEncoding="UTF-8"%>
         | 
| 81 | 
            +
            <%@ page import="model.User,model.Mutter,java.util.List" %>
         | 
| 82 | 
            +
            <%
         | 
| 83 | 
            +
            User loginUser = (User) session.getAttribute("loginUser");
         | 
| 84 | 
            +
            List<Mutter>mutterList = (List<Mutter>) application.getAttribute("mutterList");
         | 
| 85 | 
            +
            %>
         | 
| 86 | 
            +
            <!DOCTYPE html>
         | 
| 87 | 
            +
            <html>
         | 
| 88 | 
            +
            <head>
         | 
| 89 | 
            +
            <meta charset="UTF-8">
         | 
| 90 | 
            +
            <title>test</title>
         | 
| 91 | 
            +
            </head>
         | 
| 92 | 
            +
            <body>
         | 
| 93 | 
            +
            <h1>どこつぶメイン</h1>
         | 
| 94 | 
            +
            <p>
         | 
| 95 | 
            +
            <%= loginUser.getName() %>さん、ログイン中
         | 
| 96 | 
            +
            <a href  = "/docoTsubu/Logout">ログアウト</a>
         | 
| 97 | 
            +
            </p>
         | 
| 98 | 
            +
            <p><a href= "/docoTsubu/Main">更新</a></p>
         | 
| 99 | 
            +
            <form action = "/docoTsubu/Main" method="post">
         | 
| 100 | 
            +
            <input type="text" name = "text">
         | 
| 101 | 
            +
            <input type="submit" value= "つぶやく">
         | 
| 102 | 
            +
            </form>
         | 
| 103 | 
            +
            <%  for(Mutter mutter : mutterList) {%>
         | 
| 104 | 
            +
            	<p><%= mutter.getName() %>:<%= mutter.getText() %></p>
         | 
| 105 | 
            +
            	<%} %>
         | 
| 106 | 
            +
            </body>
         | 
| 107 | 
            +
            </html>
         | 
| 108 | 
            +
             | 
| 109 | 
            +
            ・クラス
         | 
| 110 | 
            +
            public class Mutter implements Serializable{
         | 
| 111 | 
            +
             | 
| 112 | 
            +
            		private String userName;
         | 
| 113 | 
            +
            		private String text;
         | 
| 114 | 
            +
            		private int id;
         | 
| 115 | 
            +
            		
         | 
| 116 | 
            +
            		public Mutter() {}
         | 
| 117 | 
            +
            		public Mutter(String userName,String text) {
         | 
| 118 | 
            +
            			this.userName = userName;
         | 
| 119 | 
            +
            			this.text = text;
         | 
| 120 | 
            +
            		}
         | 
| 121 | 
            +
            		public Mutter(int id,String userName,String text) {
         | 
| 122 | 
            +
            			this.id = id;
         | 
| 123 | 
            +
            			this.userName = userName;
         | 
| 124 | 
            +
            			this.text = text;
         | 
| 125 | 
            +
            		}
         | 
| 126 | 
            +
            		public int getId() {return id;}
         | 
| 127 | 
            +
            		public String getName() {return userName;}
         | 
| 128 | 
            +
            		public String getText() {return text;}
         | 
| 129 | 
            +
            	}	
         | 
| 130 | 
            +
            ```
         | 
