前提・実現したいこと
Eclipse,Springを使ってJAVAの勉強をしている(超)初心者です。
元々、受講していた通信教育の課題がDBを使用しておらず、
内部にテーブルを保持している構造だったので、それを
MySQL,jDBCで外部化しようとしています。
一覧表示、更新、参照機能だけのシンプルな画面です。
まずは一覧機能を実装しようとしたところ、NULL例外で以下のメッセージが
発生しました。エラーの原因個所は特定できました。
List<Map<String, Object>> list1 = jdbcTemplate.queryForList("select * from unit02.addressbook");
ただ、この記述のどこに問題があってNULLが発生しているのかがわかりません。
DBにはデータも存在します。
どなたか、ご教授いただけないでしょうか。
発生している問題・エラーメッセージ
HTTPステータス 500 - Request processing failed; nested exception is java.lang.NullPointerException type 例外レポート メッセージ Request processing failed; nested exception is java.lang.NullPointerException 説明 The server encountered an internal error that prevented it from fulfilling this request. The server encountered an internal error that prevented it from fulfilling this request.
該当のソースコード
Java
1package jp.practice.address; 2 3import java.util.List; 4import java.util.Map; 5 6import org.springframework.stereotype.Controller; 7import org.springframework.ui.Model; 8import org.springframework.web.bind.annotation.RequestMapping; 9/**e n d*/ 10 11@Controller 12@RequestMapping(value = "/book") 13public class AddressBookController { 14 private static final String INIT = "init"; 15 private static final String REFER = "refer"; 16 private static final String UPDATE = "update"; 17 private static final String LIST = "list"; 18 private static final String INIMSG = "従業員番号を入力してください"; 19 private static final String REFMSG = "情報が取得できました"; 20 private static final String UPDMSG = "情報を更新してください"; 21 private static final String ERRMSG = "該当するデータはありません"; 22/**start ****/ 23// @Autowired 24// private JdbcTemplate jdbcTemplate; 25/**e n d*/ 26 @RequestMapping(value = "/start") 27 public String init(AddressBookForm form, Model model) { 28/**start ****/ 29// List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from unit02.addressbook"); 30// for (int idx1 = 0; idx1 < 13; idx1++){ 31// System.out.print(idx1); 32// model.addAttribute("id", list.get(idx1).get("id") ); 33// model.addAttribute("name", list.get(idx1).get("name") ); 34// model.addAttribute("tel", list.get(idx1).get("tel") ); 35// model.addAttribute("address", list.get(idx1).get("address") ); 36// } 37/**e n d*/ 38 model.addAttribute("message", INIMSG); 39 return INIT; 40 } 41 42 @RequestMapping(params = "reference") 43 public String refer(AddressBookForm form, Model model) { 44 Employee employee = RecordManager.selectEmployee(form.getId()); 45 if (employee != null) { 46 form.setName(employee.getName()); 47 form.setPhone(employee.getPhone()); 48 form.setAddress(employee.getAddress()); 49 model.addAttribute("message", REFMSG); 50 return REFER; 51 } else { 52 model.addAttribute("message", ERRMSG); 53 return INIT; 54 } 55 } 56 57 @RequestMapping(params = "toInit") 58 public String toInit(AddressBookForm form, Model model) { 59 model.addAttribute("message", INIMSG); 60 return INIT; 61 } 62 63 @RequestMapping(params = "update") 64 public String update(AddressBookForm form, Model model) { 65 Employee employee = RecordManager.selectEmployee(form.getId()); 66 if (employee != null) { 67 form.setName(employee.getName()); 68 form.setPhone(employee.getPhone()); 69 form.setAddress(employee.getAddress()); 70 model.addAttribute("message", UPDMSG); 71 return UPDATE; 72 } else { 73 model.addAttribute("message", ERRMSG); 74 return INIT; 75 } 76 } 77 78 @RequestMapping(params = "reflection") 79 public String reflection(AddressBookForm form, Model model) { 80 Employee employee = new Employee(form.getId(), form.getName(), form.getPhone(), form.getAddress()); 81 RecordManager.updateEmployee(employee); 82 model.addAttribute("message", INIMSG); 83 return INIT; 84 } 85 86 @RequestMapping(params = "list") 87 public String list(Model model) { 88 List<Map<String, Object>> employeeList = RecordManager.getEmployeeList(); 89 model.addAttribute("employeeList", employeeList); 90 model.addAttribute("employeeCount", employeeList.size()); 91 return LIST; 92 }
Java
1package jp.practice.address; 2 3import java.util.ArrayList; 4import java.util.List; 5import java.util.Map; 6 7import org.springframework.beans.factory.annotation.Autowired; 8import org.springframework.jdbc.core.JdbcTemplate; 9/**e n d*/ 10 11public class RecordManager { 12/**start ****/ 13 @Autowired 14 private static JdbcTemplate jdbcTemplate; 15/**e n d*/ 16// /** 初期値データ */ 17 private static List<Employee> list = new ArrayList<>(); 18 static { 19// list.add(new Employee("200101", "佐藤一郎", "0569-000-1111", "尾張県愛知市舟船町596")); 20// list.dd(new Employee("200102", "鈴木次男", "0242-000-2222", "三河県三河市海池2727")); 21// list.add(new Employee("200103", "田中信三郎", "0722-000-3333", "和泉県泉市浜川55")); 22// list.add(new Employee("200104", "高橋四郎", "0729-000-4444", "摂津府兵庫市沼地11")); 23// list.add(new Employee("200105", "渡辺五郎", "0982-000-5555", "筑紫県筑後市泉町2")); 24// list.add(new Employee("200106", "伊藤六次", "0562-000-6666", "信濃県信州市泊町333")); 25// list.add(new Employee("200107", "山本七三", "086-000-7777", "土佐県四国市港町4")); 26// list.add(new Employee("200108", "中村八吉", "0489-000-8888", "薩摩県種島市潮見55")); 27// list.add(new Employee("200109", "小林九兵", "0827-000-9999", "周防県周防市川原66")); 28// list.add(new Employee("200110", "加藤十重", "048-000-1010", "武蔵県北むさし市河岸777")); 29// list.add(new Employee("200111", "ジャック", "0273-000-1111", "相模県相模市堤5")); 30// list.add(new Employee("200112", "山田十二", "075-000-1212", "駿河県南駿河市河口35")); 31 } 32 33 private RecordManager() { 34 } 35 36 /** 37 * 指定したID のレコードを取得 38 * 39 * @param id 40 * 従業員番号 41 * @return Employee 42 */ 43 44 public static Employee selectEmployee(String id) { 45 int index = list.indexOf(new Employee(id, "", "", "")); 46 if (index < 0) { 47 return null; 48 } 49 return list.get(index); 50 } 51 /** 52 * 指定した従業員情報を反映 53 * 54 * @param emp 55 * 従業員情報 56 */ 57 public static void updateEmployee(Employee emp) { 58 int index = list.indexOf(emp); 59 list.set(index, emp); 60 } 61/**start ****/ 62 public static List<Map<String, Object>> getEmployeeList() { 63 List<Map<String, Object>> list1 = jdbcTemplate.queryForList("select * from unit02.addressbook"); 64 65 return list1; 66/**e n d*/ 67 }
JSP
1<%@ page session="false" language="java" 2 contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 3<%@ page import="java.time.LocalDate"%> 4<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 5<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 6<!DOCTYPE html> 7<html> 8<head> 9<link rel="stylesheet" type="text/css" 10 href="/address/resources/css/common.css" /> 11<title>住所録(初期画面)</title> 12</head> 13<body> 14 <form:form modelAttribute="addressBookForm" action="/address/book"> 15 <div class="header"> 16 <span class="titleName">住所録</span> 17 <div class="date"><%=LocalDate.now()%></div> 18 </div> 19 <div class="main"> 20 <div class="message"> 21 <c:out value="${message}" /> 22 </div> 23 <div> 24 <span class="itemName">従業員番号:</span> 25 <form:input path="id" size="31" /> 26 </div> 27 </div> 28 <div class="footer"> 29 <div> 30 <input type="submit" name="reference" value="参照" /> 31 </div> 32 <div> 33 <input type="submit" name="update" value="更新" /> 34 </div> 35 <div> 36 <input type="submit" name="list" value="一覧" /> 37 </div> 38 </div> 39 <!-- start --> 40 <!-- <p> DB's id: ${id} name: ${name} tel: ${tel} address: ${address} . </p> --> 41 <!-- end --> 42 </form:form> 43</body> 44</html>
JSP
1<%@ page session="false" language="java" 2 contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 3<%@ page import="java.time.LocalDate"%> 4<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 5<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 6<!DOCTYPE html> 7<html> 8<head> 9<link rel="stylesheet" type="text/css" 10 href="/address/resources/css/common.css" /> 11<title>住所録(一覧画面)</title> 12</head> 13<body> 14 <form:form modelAttribute="addressBookForm" action="/address/book"> 15 <div class="header"> 16 <span class="titleName">住所録</span> 17 <div class="date"><%=LocalDate.now()%></div> 18 </div> 19 <div> 20 社員数 : 21 <c:out value="${employeeCount}"></c:out> 22 人 23 </div> 24 <table> 25 <tr> 26 <th>従業員番号</th> 27 <th>名前</th> 28 <th>電話番号</th> 29 <th>住所</th> 30 </tr> 31 <c:forEach items="${employeeList}" var="employee" varStatus="status"> 32 <c:choose> 33 <c:when test="${status.count % 2 == 0}"> 34 <tr class="even"> 35 <td>${employee.id}</td> 36 <td>${employee.name}</td> 37 <td>${employee.phone}</td> 38 <td>${employee.address}</td> 39 </tr> 40 </c:when> 41 <c:otherwise> 42 <tr> 43 <td>${employee.id}</td> 44 <td>${employee.name}</td> 45 <td>${employee.phone}</td> 46 <td>${employee.address}</td> 47 </tr> 48 </c:otherwise> 49 </c:choose> 50 </c:forEach> 51 </table> 52 <div class="listFooter"> 53 <input type="submit" name="toInit" value="戻る" /> 54 </div> 55 </form:form> 56</body> 57</html>
試したこと
AddressBookController.javaの@Controller直下だと、エラーにならずDBの内容を表示できます。
(現在はコメント化しています)
回答3件
あなたの回答
tips
プレビュー