前提・実現したいこと
javaを使用して、
spring boot + jpa + PostgreSQLで勉強のため、DB検索結果を
一覧画面表示するWebアプリを作っています。
JpaでDB検索を行う箇所(findAll()部分)でNullPointerExceptionが発生してしまいます。
対処法を教えていただきたいです。
発生している問題・エラーメッセージ
2020-04-10 15:24:24.937 INFO 14544 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-04-10 15:24:24.942 INFO 14544 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-04-10 15:24:24.952 INFO 14544 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 10 ms
2020-04-10 15:24:34.999 ERROR 14544 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at com.example.MyApp2.service.searchService.bookFind(searchService.java:25) ~[classes/:na]
at com.example.MyApp2.controller.testController.searchExec(testController.java:48) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_202]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_202]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_202]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_202]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at
該当のソースコード
java
1<searchService.java> 2package com.example.MyApp2.service; 3 4import java.util.List; 5 6import javax.transaction.Transactional; 7 8import org.springframework.beans.factory.annotation.Autowired; 9import org.springframework.stereotype.Service; 10 11import com.example.MyApp2.entity.tblBook; 12import com.example.MyApp2.repository.repoBook; 13 14 15@Service 16@Transactional 17public class searchService { 18 19 20 @Autowired repoBook rps; 21 22 23 public List<tblBook> bookFind() { 24 25 //検索実行 26 List<tblBook> bkList = rps.findAll(); 27 28 return bkList; 29 30 } 31 32}
Java
1<testController.java> 2package com.example.MyApp2.controller; 3 4import java.util.List; 5 6import org.springframework.stereotype.Controller; 7import org.springframework.ui.Model; 8import org.springframework.web.bind.annotation.RequestMapping; 9import org.springframework.web.bind.annotation.RequestMethod; 10import org.springframework.web.bind.annotation.RequestParam; 11 12import com.example.MyApp2.entity.tblBook; 13import com.example.MyApp2.service.searchService; 14 15 16@Controller 17public class testController { 18 19 20 // 21 //初期表示時 22 // 23 @RequestMapping("/initShow") 24 public String init(Model model) { 25 26 model.addAttribute("name","okuda aaaabbbb"); 27 model.addAttribute("mail", "xxxx@yahoo.co.jp"); 28 29 30 return "init"; 31 32 } 33 34 35 // 36 //検索実行ボタン押下時 37 // 38 @RequestMapping(value="/initShow",params="searchBtn" ,method=RequestMethod.POST) 39 public String searchExec(@RequestParam("searchWord") String reqSearchWord,Model model) { 40 41 model.addAttribute("reqSearchWord", reqSearchWord); 42 43 44 //bookModel bkModel = new bookModel(); 45 searchService srv = new searchService(); 46 47 //検索結果取得する 48 //List<bookModel> bookList = new ArrayList<bookModel>(); 49 List<tblBook> bookList = srv.bookFind(); 50// List<bookModel> bookList = srv.bookFind(); 51 52 //画面にリストを返す 53 model.addAttribute("bookList", bookList); 54 55 56 57 return "init"; 58 59 60 } 61 62 63}
Java
1<tblBook.java エンティティ> 2package com.example.MyApp2.entity; 3 4import java.io.Serializable; 5 6import javax.persistence.Column; 7import javax.persistence.Entity; 8import javax.persistence.Id; 9import javax.persistence.Table; 10 11import lombok.Data; 12 13@Entity 14@Data 15@Table(name="t_mybook") 16public class tblBook implements Serializable{ 17 18 private static final long serialVersionUID = 1L; 19 20 //ID 21 @Id 22 private Integer id; 23 24 25 @Column(name="bookname") 26 private String bookname; 27 28 29}
Java
1<repoBook.java リポジトリ> 2package com.example.MyApp2.repository; 3 4import org.springframework.data.jpa.repository.JpaRepository; 5import org.springframework.stereotype.Repository; 6 7import com.example.MyApp2.entity.tblBook; 8 9 10// 11//リポジトリ 12// 13 14@Repository 15public interface repoBook extends JpaRepository<tblBook,Integer>{ 16 17}
Java
1<MyApp2Application.java> 2package com.example.MyApp2; 3 4import org.springframework.boot.SpringApplication; 5import org.springframework.boot.autoconfigure.SpringBootApplication; 6import org.springframework.context.annotation.ComponentScan; 7 8@SpringBootApplication 9@ComponentScan 10public class MyApp2Application { 11 12 public static void main(String[] args) { 13 SpringApplication.run(MyApp2Application.class, args); 14 } 15 16}
properties
1<application.properties> 2spring.jpa.database=POSTGRESQL 3spring.datasource.url=jdbc:postgresql://localhost:5432/mydb 4spring.datasource.username=postgres 5spring.datasource.password=iwtawya7 6spring.datasource.driver-class-name=org.postgresql.Driver 7 8 9spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false 10 11spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
試したこと
・@SpringBootApplicationアノテーションを記載しているMyApp2Application.javaに
@ComponentScanアノテーションを追加。→変化なし。
・各パッケージ名を変更
変更前:com.example.demo
変更後:com.example.MyApp2
→変化なし。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー