質問編集履歴

9

変更

2016/04/12 13:16

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  Main.java(servlet)ではJSTLを使った時用のコードになっていて、main.jspではJSTLを使っていないコードだったため、mian.jspでもcoreタグライブラリを使用しました。
8
8
 
9
- エラーも java.lang.NullPointerExceptionから変わりました。ぞよろしくお願いします
9
+ エラーも java.lang.NullPointerExceptionから変わッタノですが、属性名と属性の間にスペースを入れたところ治りました。ありがとござ
10
10
 
11
11
  ```java
12
12
 
@@ -92,7 +92,7 @@
92
92
 
93
93
  //変更後→nullPointerではなくTTP Status 500 - /WEB-INF/jsp/main.jsp (line: 24, column: 44) The JSP specification requires that an attribute name is preceded by whitespaceこのようなエラーに変わりました。
94
94
 
95
- このコードはSerlvlet の Main.javaです。
95
+ このコードはSerlvlet の Main.javaです。→スペースを入れたところ治りました。
96
96
 
97
97
  ```java
98
98
 

8

変更

2016/04/12 13:16

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -90,7 +90,7 @@
90
90
 
91
91
  javaでsnsを作っているのですが、NullPointerExceptionが出てしまいます。原因がわかりません、アドバイスの方をどうぞよろしくお願いします。
92
92
 
93
-
93
+ //変更後→nullPointerではなくTTP Status 500 - /WEB-INF/jsp/main.jsp (line: 24, column: 44) The JSP specification requires that an attribute name is preceded by whitespaceこのようなエラーに変わりました。
94
94
 
95
95
  このコードはSerlvlet の Main.javaです。
96
96
 
@@ -292,7 +292,7 @@
292
292
 
293
293
  ```
294
294
 
295
- このコードは main.jspです。
295
+ このコードは main.jspです。main.jspもJSTLを使ったコードに変更しました。
296
296
 
297
297
  ```java
298
298
 

7

変更

2016/04/12 12:57

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
File without changes

6

変更

2016/04/12 12:55

投稿

edoooooo
edoooooo

スコア476

test CHANGED
@@ -1 +1 @@
1
- javaでsnsを作っているのですが、nullpointer が出てしまいます。
1
+ javaでsnsを作っているのですが、 java.lang.NullPointerException が出てしまいます。
test CHANGED
@@ -6,6 +6,8 @@
6
6
 
7
7
  Main.java(servlet)ではJSTLを使った時用のコードになっていて、main.jspではJSTLを使っていないコードだったため、mian.jspでもcoreタグライブラリを使用しました。
8
8
 
9
+ エラーも java.lang.NullPointerExceptionから変わりました。どうぞよろしくお願いします。
10
+
9
11
  ```java
10
12
 
11
13
  HTTP Status 500 - /WEB-INF/jsp/main.jsp (line: 24, column: 44) The JSP specification requires that an attribute name is preceded by whitespace

5

変更

2016/04/12 12:54

投稿

edoooooo
edoooooo

スコア476

test CHANGED
@@ -1 +1 @@
1
- javaでsnsを作っているのですが、HTTP Status 500 - /WEB-INF/jsp/main.jsp が出てしまいます。
1
+ javaでsnsを作っているのですが、nullpointer が出てしまいます。
test CHANGED
@@ -1,5 +1,9 @@
1
1
  ![イメージ説明](d585b73aab2391a347a6dc01b8527689.png)
2
2
 
3
+
4
+
5
+
6
+
3
7
  Main.java(servlet)ではJSTLを使った時用のコードになっていて、main.jspではJSTLを使っていないコードだったため、mian.jspでもcoreタグライブラリを使用しました。
4
8
 
5
9
  ```java

4

Main\.java\(servlet\)ではJSTLを使った時用のコードになっていて、main\.jspではJSTLを使っていないコードだったため、mian\.jspでもcoreタグライブラリを使用しました。

2016/04/12 12:52

投稿

edoooooo
edoooooo

スコア476

test CHANGED
@@ -1 +1 @@
1
- javaでsnsを作っているのですが、NullPointerExceptionが出てしまいます。原因がわかりません、アドバイスの方をどうぞよろしくお願いします。
1
+ javaでsnsを作っているのですが、HTTP Status 500 - /WEB-INF/jsp/main.jsp が出てしまいます。
test CHANGED
@@ -1,8 +1,10 @@
1
1
  ![イメージ説明](d585b73aab2391a347a6dc01b8527689.png)
2
2
 
3
+ Main.java(servlet)ではJSTLを使った時用のコードになっていて、main.jspではJSTLを使っていないコードだったため、mian.jspでもcoreタグライブラリを使用しました。
4
+
3
5
  ```java
4
6
 
5
- HTTP Status 500 - java.lang.NullPointerException
7
+ HTTP Status 500 - /WEB-INF/jsp/main.jsp (line: 24, column: 44) The JSP specification requires that an attribute name is preceded by whitespace
6
8
 
7
9
 
8
10
 
@@ -10,7 +12,7 @@
10
12
 
11
13
 
12
14
 
13
- message java.lang.NullPointerException
15
+ message /WEB-INF/jsp/main.jsp (line: 24, column: 44) The JSP specification requires that an attribute name is preceded by whitespace
14
16
 
15
17
 
16
18
 
@@ -22,11 +24,39 @@
22
24
 
23
25
 
24
26
 
27
+ org.apache.jasper.JasperException: /WEB-INF/jsp/main.jsp (line: 24, column: 44) The JSP specification requires that an attribute name is preceded by whitespace
28
+
29
+ org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
30
+
31
+ org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:443)
32
+
25
- org.apache.jasper.JasperException: java.lang.NullPointerException
33
+ org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:89)
34
+
26
-
35
+ org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:164)
36
+
37
+ org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:153)
38
+
39
+ org.apache.jasper.compiler.Parser.parseCustomTag(Parser.java:1225)
40
+
41
+ org.apache.jasper.compiler.Parser.parseElements(Parser.java:1442)
42
+
43
+ org.apache.jasper.compiler.Parser.parse(Parser.java:138)
44
+
45
+ org.apache.jasper.compiler.ParserController.doParse(ParserController.java:242)
46
+
27
- org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:556)
47
+ org.apache.jasper.compiler.ParserController.parse(ParserController.java:102)
48
+
28
-
49
+ org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
50
+
51
+ org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
52
+
53
+ org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
54
+
55
+ org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
56
+
57
+ org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662)
58
+
29
- org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
59
+ org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364)
30
60
 
31
61
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
32
62
 
@@ -36,7 +66,7 @@
36
66
 
37
67
  org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
38
68
 
39
- servlet.Main.doGet(Main.java:51)
69
+ servlet.Main.doGet(Main.java:54)
40
70
 
41
71
  javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
42
72
 
@@ -44,36 +74,6 @@
44
74
 
45
75
  org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
46
76
 
47
- root cause
48
-
49
-
50
-
51
- java.lang.NullPointerException
52
-
53
- org.apache.jsp.WEB_002dINF.jsp.main_jsp._jspService(main_jsp.java:122)
54
-
55
- org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
56
-
57
- javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
58
-
59
- org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
60
-
61
- org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
62
-
63
- org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
64
-
65
- javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
66
-
67
- org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
68
-
69
- servlet.Main.doGet(Main.java:51)
70
-
71
- javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
72
-
73
- javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
74
-
75
- org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
76
-
77
77
  note The full stack trace of the root cause is available in the Apache Tomcat/7.0.65 logs.
78
78
 
79
79
 
@@ -294,25 +294,9 @@
294
294
 
295
295
  pageEncoding="UTF-8"%>
296
296
 
297
- <%@page import ="model.User,model.Mutter,java.util.List" %>
297
+ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
298
-
299
- <%
298
+
300
-
301
- //セッションスコープに保存されたユーザー情報を取得
302
-
303
- User loginUser=(User)session.getAttribute("loginUser");
304
-
305
- //アプリケーションスコープに保存されたつぶやきリストを取得
306
-
307
- List<Mutter>mutterList=(List<Mutter>)application.getAttribute("mutterList");
308
-
309
- //リクエストスコープに保存されたエラーメッセージを取得
310
-
311
- String errorMsg=(String)request.getAttribute("errorMsg");
312
-
313
- %>
314
-
315
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
299
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
316
300
 
317
301
  <html>
318
302
 
@@ -330,7 +314,7 @@
330
314
 
331
315
  <p>
332
316
 
333
- <%=loginUser.getName() %>さん、ログイン中
317
+ <c:out value="${loginUser.name}"/>さん、ログイン中
334
318
 
335
319
  <a href="/docoTsubuu/Logout">ログアウト</a>
336
320
 
@@ -346,22 +330,26 @@
346
330
 
347
331
  </form>
348
332
 
349
- <%if(errorMsg!=null){ %>
333
+ <c:if test="${not empty errorMsg}">
350
-
334
+
351
- <p><%=errorMsg %></p>
335
+ <p>${errorMsg}</p>
352
-
336
+
353
- <%} %>
337
+ </c:if>
354
-
338
+
355
- <% for(Mutter mutter:mutterList){%>
339
+ <c:forEach var="mutter"items="${mutterList}">
356
-
340
+
357
- <p><%=mutter.getUserName() %> %>:<%=mutter.getText() %></p>
341
+ <p><c:out value="${mutter.userName}"/>:
342
+
358
-
343
+ <c:out value="${mutter.text}"/></p>
344
+
359
- <%} %>
345
+ </c:forEach>
360
346
 
361
347
  </body>
362
348
 
363
349
  </html>
364
350
 
351
+
352
+
365
353
  ```
366
354
 
367
355
  皆さんどうぞよろしくお願いします。

3

時並びの改善

2016/04/12 12:48

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,6 @@
1
- ![イメージ説明](d585b73aab2391a347a6dc01b8527689.png)```java
1
+ ![イメージ説明](d585b73aab2391a347a6dc01b8527689.png)
2
+
3
+ ```java
2
4
 
3
5
  HTTP Status 500 - java.lang.NullPointerException
4
6
 

2

画像の挿入

2016/04/12 09:33

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -1,365 +1,365 @@
1
+ ![イメージ説明](d585b73aab2391a347a6dc01b8527689.png)```java
2
+
3
+ HTTP Status 500 - java.lang.NullPointerException
4
+
5
+
6
+
7
+ type Exception report
8
+
9
+
10
+
11
+ message java.lang.NullPointerException
12
+
13
+
14
+
15
+ description The server encountered an internal error that prevented it from fulfilling this request.
16
+
17
+
18
+
19
+ exception
20
+
21
+
22
+
23
+ org.apache.jasper.JasperException: java.lang.NullPointerException
24
+
25
+ org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:556)
26
+
27
+ org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
28
+
29
+ org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
30
+
31
+ org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
32
+
33
+ javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
34
+
35
+ org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
36
+
37
+ servlet.Main.doGet(Main.java:51)
38
+
39
+ javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
40
+
41
+ javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
42
+
43
+ org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
44
+
45
+ root cause
46
+
47
+
48
+
49
+ java.lang.NullPointerException
50
+
51
+ org.apache.jsp.WEB_002dINF.jsp.main_jsp._jspService(main_jsp.java:122)
52
+
53
+ org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
54
+
55
+ javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
56
+
57
+ org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
58
+
59
+ org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
60
+
61
+ org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
62
+
63
+ javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
64
+
65
+ org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
66
+
67
+ servlet.Main.doGet(Main.java:51)
68
+
69
+ javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
70
+
71
+ javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
72
+
73
+ org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
74
+
75
+ note The full stack trace of the root cause is available in the Apache Tomcat/7.0.65 logs.
76
+
77
+
78
+
79
+ Apache Tomcat/7.0.65
80
+
81
+ ```
82
+
83
+ javaでsnsを作っているのですが、NullPointerExceptionが出てしまいます。原因がわかりません、アドバイスの方をどうぞよろしくお願いします。
84
+
85
+
86
+
87
+ このコードはSerlvlet の Main.javaです。
88
+
1
89
  ```java
2
90
 
3
- HTTP Status 500 - java.lang.NullPointerException
4
-
5
-
6
-
7
- type Exception report
8
-
9
-
10
-
11
- message java.lang.NullPointerException
12
-
13
-
14
-
15
- description The server encountered an internal error that prevented it from fulfilling this request.
16
-
17
-
18
-
19
- exception
20
-
21
-
22
-
23
- org.apache.jasper.JasperException: java.lang.NullPointerException
24
-
25
- org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:556)
26
-
27
- org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
28
-
29
- org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
30
-
31
- org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
32
-
33
- javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
34
-
35
- org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
36
-
37
- servlet.Main.doGet(Main.java:51)
38
-
39
- javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
40
-
41
- javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
42
-
43
- org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
44
-
45
- root cause
46
-
47
-
48
-
49
- java.lang.NullPointerException
50
-
51
- org.apache.jsp.WEB_002dINF.jsp.main_jsp._jspService(main_jsp.java:122)
52
-
53
- org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
54
-
55
- javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
56
-
57
- org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
58
-
59
- org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
60
-
61
- org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
62
-
63
- javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
64
-
65
- org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
66
-
67
- servlet.Main.doGet(Main.java:51)
68
-
69
- javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
70
-
71
- javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
72
-
73
- org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
74
-
75
- note The full stack trace of the root cause is available in the Apache Tomcat/7.0.65 logs.
76
-
77
-
78
-
79
- Apache Tomcat/7.0.65
91
+ package servlet;
92
+
93
+
94
+
95
+
96
+
97
+ import java.util.List;
98
+
99
+ import javax.servlet.RequestDispatcher;
100
+
101
+
102
+
103
+ import java.io.IOException;
104
+
105
+ import java.sql.DriverManager;
106
+
107
+
108
+
109
+ import javax.servlet.ServletException;
110
+
111
+ import javax.servlet.annotation.WebServlet;
112
+
113
+ import javax.servlet.http.HttpServlet;
114
+
115
+ import javax.servlet.http.HttpServletRequest;
116
+
117
+ import javax.servlet.http.HttpServletResponse;
118
+
119
+ import javax.servlet.http.HttpSession;
120
+
121
+
122
+
123
+ import com.mysql.jdbc.Connection;
124
+
125
+ import com.mysql.jdbc.PreparedStatement;
126
+
127
+
128
+
129
+ import model.GetMutterListLogic;
130
+
131
+ import model.Mutter;
132
+
133
+ import model.User;
134
+
135
+ import model.PostMutterLogic;
136
+
137
+
138
+
139
+ /**
140
+
141
+ * Servlet implementation class Main
142
+
143
+ */
144
+
145
+ @WebServlet("/Main")
146
+
147
+ public class Main extends HttpServlet {
148
+
149
+
150
+
151
+ private static final long serialVersionUID = 1L;
152
+
153
+
154
+
155
+ /**
156
+
157
+ * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
158
+
159
+ */
160
+
161
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
162
+
163
+
164
+
165
+ //つぶやきリストを取得して、リクエストスコープに保存
166
+
167
+ GetMutterListLogic getMutterListLogic=new GetMutterListLogic();
168
+
169
+ List<Mutter>mutterList=getMutterListLogic.execute();
170
+
171
+ request.setAttribute("mutterList", mutterList);
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+ //ログインしているか確認するため
180
+
181
+ //セッションスコープからユーザー情報を取得
182
+
183
+ HttpSession session=request.getSession();
184
+
185
+ User loginUser=(User)session.getAttribute("loginUser");
186
+
187
+
188
+
189
+ if(loginUser==null){//ログインしていない場合
190
+
191
+ //リダイレクト
192
+
193
+ response.sendRedirect("/docoTsubuu/");
194
+
195
+ }else{//ログイン済みの場合
196
+
197
+ //フォワード
198
+
199
+ RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
200
+
201
+ dispatcher.forward(request, response);
202
+
203
+ }
204
+
205
+
206
+
207
+ }
208
+
209
+ protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
210
+
211
+ //リクエストパラメータの取得
212
+
213
+ request.setCharacterEncoding("UTF-8");
214
+
215
+ String text=request.getParameter("text");
216
+
217
+
218
+
219
+ //入力値チェック
220
+
221
+ if(text!=null&&text.length()!=0){
222
+
223
+
224
+
225
+ //セッションスコープに保存されたユーザー情報を取得
226
+
227
+ HttpSession session=request.getSession();
228
+
229
+ User loginUser=(User)session.getAttribute("loginUser");
230
+
231
+
232
+
233
+ //つぶやきをつぶやきリストに追加
234
+
235
+ Mutter mutter=new Mutter(loginUser.getName(),text);
236
+
237
+ PostMutterLogic postMutterLogic=new PostMutterLogic();
238
+
239
+ postMutterLogic.execute(mutter);
240
+
241
+
242
+
243
+ //つぶやきリストを習得して、リクエストスコープに保存
244
+
245
+ GetMutterListLogic getMutterListLogic=new GetMutterListLogic();
246
+
247
+ List<Mutter> mutterList=getMutterListLogic.execute();
248
+
249
+ request.setAttribute("mutterList", mutterList);
250
+
251
+
252
+
253
+
254
+
255
+ }else{
256
+
257
+ //エラーメッセージをリクエストスコープに保存
258
+
259
+
260
+
261
+ request.setAttribute("errorMsg", "つぶやきが入力されていません");
262
+
263
+ }
264
+
265
+ //メイン画面にフォワード
266
+
267
+ RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
268
+
269
+ dispatcher.forward(request, response);
270
+
271
+
272
+
273
+ // TODO Auto-generated method stub
274
+
275
+ response.getWriter().append("Served at: ").append(request.getContextPath());
276
+
277
+ }
278
+
279
+
280
+
281
+ }
282
+
283
+
80
284
 
81
285
  ```
82
286
 
83
- javaでsnsを作っているのですが、NullPointerExceptionが出てしまいます。原因がわかりません、アドバイスの方をどうぞよろしくお願いします。
84
-
85
-
86
-
87
- このコードはSerlvlet の Main.javaです。
287
+ このコードは main.jspです。
88
288
 
89
289
  ```java
90
290
 
91
- package servlet;
92
-
93
-
94
-
95
-
96
-
97
- import java.util.List;
98
-
99
- import javax.servlet.RequestDispatcher;
100
-
101
-
102
-
103
- import java.io.IOException;
104
-
105
- import java.sql.DriverManager;
106
-
107
-
108
-
109
- import javax.servlet.ServletException;
110
-
111
- import javax.servlet.annotation.WebServlet;
112
-
113
- import javax.servlet.http.HttpServlet;
114
-
115
- import javax.servlet.http.HttpServletRequest;
116
-
117
- import javax.servlet.http.HttpServletResponse;
118
-
119
- import javax.servlet.http.HttpSession;
120
-
121
-
122
-
123
- import com.mysql.jdbc.Connection;
124
-
125
- import com.mysql.jdbc.PreparedStatement;
126
-
127
-
128
-
129
- import model.GetMutterListLogic;
130
-
131
- import model.Mutter;
132
-
133
- import model.User;
134
-
135
- import model.PostMutterLogic;
136
-
137
-
138
-
139
- /**
140
-
141
- * Servlet implementation class Main
142
-
143
- */
144
-
145
- @WebServlet("/Main")
146
-
147
- public class Main extends HttpServlet {
148
-
149
-
150
-
151
- private static final long serialVersionUID = 1L;
152
-
153
-
154
-
155
- /**
156
-
157
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
158
-
159
- */
160
-
161
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
162
-
163
-
164
-
165
- //つぶやきリストを取得して、リクエストスコープに保存
166
-
167
- GetMutterListLogic getMutterListLogic=new GetMutterListLogic();
168
-
169
- List<Mutter>mutterList=getMutterListLogic.execute();
170
-
171
- request.setAttribute("mutterList", mutterList);
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
- //ログインしているか確認するため
180
-
181
- //セッションスコープからユーザー情報を取得
182
-
183
- HttpSession session=request.getSession();
184
-
185
- User loginUser=(User)session.getAttribute("loginUser");
186
-
187
-
188
-
189
- if(loginUser==null){//ログインしていない場合
190
-
191
- //リダイレクト
192
-
193
- response.sendRedirect("/docoTsubuu/");
194
-
195
- }else{//ログイン済みの場合
196
-
197
- //フォワード
198
-
199
- RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
200
-
201
- dispatcher.forward(request, response);
202
-
203
- }
204
-
205
-
206
-
207
- }
208
-
209
- protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
210
-
211
- //リクエストパラメータの取得
212
-
213
- request.setCharacterEncoding("UTF-8");
214
-
215
- String text=request.getParameter("text");
216
-
217
-
218
-
219
- //入力値チェック
220
-
221
- if(text!=null&&text.length()!=0){
222
-
223
-
224
-
225
- //セッションスコープに保存されたユーザー情報を取得
226
-
227
- HttpSession session=request.getSession();
228
-
229
- User loginUser=(User)session.getAttribute("loginUser");
230
-
231
-
232
-
233
- //つぶやきをつぶやきリストに追加
234
-
235
- Mutter mutter=new Mutter(loginUser.getName(),text);
236
-
237
- PostMutterLogic postMutterLogic=new PostMutterLogic();
238
-
239
- postMutterLogic.execute(mutter);
240
-
241
-
242
-
243
- //つぶやきリストを習得して、リクエストスコープに保存
244
-
245
- GetMutterListLogic getMutterListLogic=new GetMutterListLogic();
246
-
247
- List<Mutter> mutterList=getMutterListLogic.execute();
248
-
249
- request.setAttribute("mutterList", mutterList);
250
-
251
-
252
-
253
-
254
-
255
- }else{
256
-
257
- //エラーメッセージをリクエストスコープに保存
258
-
259
-
260
-
261
- request.setAttribute("errorMsg", "つぶやきが入力されていません");
262
-
263
- }
264
-
265
- //メイン画面にフォワード
266
-
267
- RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
268
-
269
- dispatcher.forward(request, response);
270
-
271
-
272
-
273
- // TODO Auto-generated method stub
274
-
275
- response.getWriter().append("Served at: ").append(request.getContextPath());
276
-
277
- }
278
-
279
-
280
-
281
- }
282
-
283
-
291
+ <%@ page language="java" contentType="text/html; charset=UTF-8"
292
+
293
+ pageEncoding="UTF-8"%>
294
+
295
+ <%@page import ="model.User,model.Mutter,java.util.List" %>
296
+
297
+ <%
298
+
299
+ //セッションスコープに保存されたユーザー情報を取得
300
+
301
+ User loginUser=(User)session.getAttribute("loginUser");
302
+
303
+ //アプリケーションスコープに保存されたつぶやきリストを取得
304
+
305
+ List<Mutter>mutterList=(List<Mutter>)application.getAttribute("mutterList");
306
+
307
+ //リクエストスコープに保存されたエラーメッセージを取得
308
+
309
+ String errorMsg=(String)request.getAttribute("errorMsg");
310
+
311
+ %>
312
+
313
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
314
+
315
+ <html>
316
+
317
+ <head>
318
+
319
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
320
+
321
+ <title>どこつぶ</title>
322
+
323
+ </head>
324
+
325
+ <body>
326
+
327
+ <h1>どこ粒メイン</h1>
328
+
329
+ <p>
330
+
331
+ <%=loginUser.getName() %>さん、ログイン中
332
+
333
+ <a href="/docoTsubuu/Logout">ログアウト</a>
334
+
335
+ </p>
336
+
337
+ <p><a href="/docoTsubuu/Main">更新</a></p>
338
+
339
+ <form action="/docoTsubuu/Main"method="post">
340
+
341
+ <input type="text"name="text">
342
+
343
+ <input type="submit" value="つぶやく">
344
+
345
+ </form>
346
+
347
+ <%if(errorMsg!=null){ %>
348
+
349
+ <p><%=errorMsg %></p>
350
+
351
+ <%} %>
352
+
353
+ <% for(Mutter mutter:mutterList){%>
354
+
355
+ <p><%=mutter.getUserName() %> %>:<%=mutter.getText() %></p>
356
+
357
+ <%} %>
358
+
359
+ </body>
360
+
361
+ </html>
284
362
 
285
363
  ```
286
364
 
287
- このコードは main.jspです。
288
-
289
- ```java
290
-
291
- <%@ page language="java" contentType="text/html; charset=UTF-8"
292
-
293
- pageEncoding="UTF-8"%>
294
-
295
- <%@page import ="model.User,model.Mutter,java.util.List" %>
296
-
297
- <%
298
-
299
- //セッションスコープに保存されたユーザー情報を取得
300
-
301
- User loginUser=(User)session.getAttribute("loginUser");
302
-
303
- //アプリケーションスコープに保存されたつぶやきリストを取得
304
-
305
- List<Mutter>mutterList=(List<Mutter>)application.getAttribute("mutterList");
306
-
307
- //リクエストスコープに保存されたエラーメッセージを取得
308
-
309
- String errorMsg=(String)request.getAttribute("errorMsg");
310
-
311
- %>
312
-
313
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
314
-
315
- <html>
316
-
317
- <head>
318
-
319
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
320
-
321
- <title>どこつぶ</title>
322
-
323
- </head>
324
-
325
- <body>
326
-
327
- <h1>どこ粒メイン</h1>
328
-
329
- <p>
330
-
331
- <%=loginUser.getName() %>さん、ログイン中
332
-
333
- <a href="/docoTsubuu/Logout">ログアウト</a>
334
-
335
- </p>
336
-
337
- <p><a href="/docoTsubuu/Main">更新</a></p>
338
-
339
- <form action="/docoTsubuu/Main"method="post">
340
-
341
- <input type="text"name="text">
342
-
343
- <input type="submit" value="つぶやく">
344
-
345
- </form>
346
-
347
- <%if(errorMsg!=null){ %>
348
-
349
- <p><%=errorMsg %></p>
350
-
351
- <%} %>
352
-
353
- <% for(Mutter mutter:mutterList){%>
354
-
355
- <p><%=mutter.getUserName() %> %>:<%=mutter.getText() %></p>
356
-
357
- <%} %>
358
-
359
- </body>
360
-
361
- </html>
362
-
363
- ```
364
-
365
365
  皆さんどうぞよろしくお願いします。

1

原因を示すコードを追加しました。

2016/04/12 09:32

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -81,3 +81,285 @@
81
81
  ```
82
82
 
83
83
  javaでsnsを作っているのですが、NullPointerExceptionが出てしまいます。原因がわかりません、アドバイスの方をどうぞよろしくお願いします。
84
+
85
+
86
+
87
+ このコードはSerlvlet の Main.javaです。
88
+
89
+ ```java
90
+
91
+ package servlet;
92
+
93
+
94
+
95
+
96
+
97
+ import java.util.List;
98
+
99
+ import javax.servlet.RequestDispatcher;
100
+
101
+
102
+
103
+ import java.io.IOException;
104
+
105
+ import java.sql.DriverManager;
106
+
107
+
108
+
109
+ import javax.servlet.ServletException;
110
+
111
+ import javax.servlet.annotation.WebServlet;
112
+
113
+ import javax.servlet.http.HttpServlet;
114
+
115
+ import javax.servlet.http.HttpServletRequest;
116
+
117
+ import javax.servlet.http.HttpServletResponse;
118
+
119
+ import javax.servlet.http.HttpSession;
120
+
121
+
122
+
123
+ import com.mysql.jdbc.Connection;
124
+
125
+ import com.mysql.jdbc.PreparedStatement;
126
+
127
+
128
+
129
+ import model.GetMutterListLogic;
130
+
131
+ import model.Mutter;
132
+
133
+ import model.User;
134
+
135
+ import model.PostMutterLogic;
136
+
137
+
138
+
139
+ /**
140
+
141
+ * Servlet implementation class Main
142
+
143
+ */
144
+
145
+ @WebServlet("/Main")
146
+
147
+ public class Main extends HttpServlet {
148
+
149
+
150
+
151
+ private static final long serialVersionUID = 1L;
152
+
153
+
154
+
155
+ /**
156
+
157
+ * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
158
+
159
+ */
160
+
161
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
162
+
163
+
164
+
165
+ //つぶやきリストを取得して、リクエストスコープに保存
166
+
167
+ GetMutterListLogic getMutterListLogic=new GetMutterListLogic();
168
+
169
+ List<Mutter>mutterList=getMutterListLogic.execute();
170
+
171
+ request.setAttribute("mutterList", mutterList);
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+ //ログインしているか確認するため
180
+
181
+ //セッションスコープからユーザー情報を取得
182
+
183
+ HttpSession session=request.getSession();
184
+
185
+ User loginUser=(User)session.getAttribute("loginUser");
186
+
187
+
188
+
189
+ if(loginUser==null){//ログインしていない場合
190
+
191
+ //リダイレクト
192
+
193
+ response.sendRedirect("/docoTsubuu/");
194
+
195
+ }else{//ログイン済みの場合
196
+
197
+ //フォワード
198
+
199
+ RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
200
+
201
+ dispatcher.forward(request, response);
202
+
203
+ }
204
+
205
+
206
+
207
+ }
208
+
209
+ protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
210
+
211
+ //リクエストパラメータの取得
212
+
213
+ request.setCharacterEncoding("UTF-8");
214
+
215
+ String text=request.getParameter("text");
216
+
217
+
218
+
219
+ //入力値チェック
220
+
221
+ if(text!=null&&text.length()!=0){
222
+
223
+
224
+
225
+ //セッションスコープに保存されたユーザー情報を取得
226
+
227
+ HttpSession session=request.getSession();
228
+
229
+ User loginUser=(User)session.getAttribute("loginUser");
230
+
231
+
232
+
233
+ //つぶやきをつぶやきリストに追加
234
+
235
+ Mutter mutter=new Mutter(loginUser.getName(),text);
236
+
237
+ PostMutterLogic postMutterLogic=new PostMutterLogic();
238
+
239
+ postMutterLogic.execute(mutter);
240
+
241
+
242
+
243
+ //つぶやきリストを習得して、リクエストスコープに保存
244
+
245
+ GetMutterListLogic getMutterListLogic=new GetMutterListLogic();
246
+
247
+ List<Mutter> mutterList=getMutterListLogic.execute();
248
+
249
+ request.setAttribute("mutterList", mutterList);
250
+
251
+
252
+
253
+
254
+
255
+ }else{
256
+
257
+ //エラーメッセージをリクエストスコープに保存
258
+
259
+
260
+
261
+ request.setAttribute("errorMsg", "つぶやきが入力されていません");
262
+
263
+ }
264
+
265
+ //メイン画面にフォワード
266
+
267
+ RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
268
+
269
+ dispatcher.forward(request, response);
270
+
271
+
272
+
273
+ // TODO Auto-generated method stub
274
+
275
+ response.getWriter().append("Served at: ").append(request.getContextPath());
276
+
277
+ }
278
+
279
+
280
+
281
+ }
282
+
283
+
284
+
285
+ ```
286
+
287
+ このコードは main.jspです。
288
+
289
+ ```java
290
+
291
+ <%@ page language="java" contentType="text/html; charset=UTF-8"
292
+
293
+ pageEncoding="UTF-8"%>
294
+
295
+ <%@page import ="model.User,model.Mutter,java.util.List" %>
296
+
297
+ <%
298
+
299
+ //セッションスコープに保存されたユーザー情報を取得
300
+
301
+ User loginUser=(User)session.getAttribute("loginUser");
302
+
303
+ //アプリケーションスコープに保存されたつぶやきリストを取得
304
+
305
+ List<Mutter>mutterList=(List<Mutter>)application.getAttribute("mutterList");
306
+
307
+ //リクエストスコープに保存されたエラーメッセージを取得
308
+
309
+ String errorMsg=(String)request.getAttribute("errorMsg");
310
+
311
+ %>
312
+
313
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
314
+
315
+ <html>
316
+
317
+ <head>
318
+
319
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
320
+
321
+ <title>どこつぶ</title>
322
+
323
+ </head>
324
+
325
+ <body>
326
+
327
+ <h1>どこ粒メイン</h1>
328
+
329
+ <p>
330
+
331
+ <%=loginUser.getName() %>さん、ログイン中
332
+
333
+ <a href="/docoTsubuu/Logout">ログアウト</a>
334
+
335
+ </p>
336
+
337
+ <p><a href="/docoTsubuu/Main">更新</a></p>
338
+
339
+ <form action="/docoTsubuu/Main"method="post">
340
+
341
+ <input type="text"name="text">
342
+
343
+ <input type="submit" value="つぶやく">
344
+
345
+ </form>
346
+
347
+ <%if(errorMsg!=null){ %>
348
+
349
+ <p><%=errorMsg %></p>
350
+
351
+ <%} %>
352
+
353
+ <% for(Mutter mutter:mutterList){%>
354
+
355
+ <p><%=mutter.getUserName() %> %>:<%=mutter.getText() %></p>
356
+
357
+ <%} %>
358
+
359
+ </body>
360
+
361
+ </html>
362
+
363
+ ```
364
+
365
+ 皆さんどうぞよろしくお願いします。