質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

2回答

15290閲覧

Spring bootでNullPointerExceptionが発生する

chihuahua_house

総合スコア23

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2019/06/06 13:14

Spring bootを使用してコマンドアプリの作成を行っています。

ymlファイルから各種情報を@Dataを使用し、取得を行いたいのですが
mainクラスからは取得が可能なのにsubクラスから取得を行うと
Caused by: java.lang.NullPointerException: null のエラーが発生してしまいます。

Springを初めて使用しており、DIとは何かの状態です。

原因が分かる方、ご教授お願いします。

Main

1package com.xxxx.xxxx.ems; 2 3import org.slf4j.Logger; 4import org.slf4j.LoggerFactory; 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.boot.ApplicationArguments; 7import org.springframework.boot.ApplicationRunner; 8import org.springframework.boot.SpringApplication; 9import org.springframework.boot.WebApplicationType; 10import org.springframework.boot.autoconfigure.SpringBootApplication; 11 12 13@SpringBootApplication 14public class Ems2Application implements ApplicationRunner{ 15 16 /** RegisterAccelDataService. */ 17 18 /** Logger. */ 19 private Logger logger = LoggerFactory.getLogger(this.getClass()); 20 21 @Autowired 22 public Config_Settings configSettings; 23 24 25 public static void main(String[] args) { 26 // SpringBoot起動 27 final SpringApplication springApplication = new SpringApplication(); 28 springApplication.setWebApplicationType(WebApplicationType.NONE); 29 SpringApplication.run(Ems2Application.class, args); 30 } 31 32 public void run(ApplicationArguments args) throws Exception { 33//ここは取得できる 34 logger.info(configSettings.getEMS0001()); 35 DBAccesser db = new DBAccesser(); 36 db.dbaccesser(); 37 logger.info(configSettings.getEMS0002()); 38 } 39} 40

Sub

1package com.xxxx.xxxx.xxxx; 2 3import java.sql.Connection; 4import java.sql.DriverManager; 5import java.sql.ResultSet; 6import java.sql.SQLException; 7import java.sql.Statement; 8import java.util.concurrent.TimeUnit; 9 10import org.springframework.beans.factory.annotation.Autowired; 11import org.springframework.stereotype.Service; 12 13 14 15@Service 16public class DBAccesser { 17 18 /** 19 * データベース操作クラス 20 */ 21 22 @Autowired 23 public Config_Settings configSettings; 24 25 private Connection connection = null; 26 27 private static final int RETRY_NUM = 5; 28 private static final int RETRY_INTERVAL = 3; 29 30 //public static Logger logger = LoggerFactory.getLogger(class()); 31 public void dbaccesser() throws Exception { 32 33 34//ここでnullになる 35 String jdbc = configSettings.getJDBC(); 36 String dbuser = configSettings.getUSERNAME(); 37 String dbpass = configSettings.getPASS(); 38 39 40 for (int retryNum = 1; retryNum < RETRY_NUM; retryNum++) { 41 try { 42 connection = DriverManager.getConnection(jdbc,dbuser,dbpass); 43 Statement progress_Select = connection.createStatement(); 44 ResultSet result = progress_Select.executeQuery(configSettings.getPROGRESS()); 45 46 while(result.next()) { 47 48 System.out.println(result.getDate(3)); 49 RateCsv.csvAcceser(result.getString(1),result.getString(2),result.getDate(3)); 50 } 51 52 break; 53 54 }catch (SQLException e) { 55 try { 56 //logger.info(configReader.getEms0003()); 57 TimeUnit.SECONDS.sleep(RETRY_INTERVAL); 58 } catch (Exception e1){ 59 e1.printStackTrace(); 60 } 61 }finally { 62 connection.close(); 63 } 64 } 65 } 66} 67

Access

1package com.xxxx.yyyy.xxxx; 2 3import org.springframework.boot.context.properties.ConfigurationProperties; 4//import org.springframework.stereotype.Component; 5import org.springframework.stereotype.Component; 6 7import lombok.Data; 8 9@Component 10@ConfigurationProperties(prefix = "settings") 11@Data 12 13public class Config_Settings { 14 15 private String EMS0001; 16 17 private String EMS0002; 18 19 private String EMS0003; 20 21 private String PROGRESS; 22 23 private String JDBC; 24 25 private String USERNAME; 26 27 private String PASS; 28}

Application.yml

1settings: 2 EMS0001: "登録処理を開始します" 3 JDBC: "jdbc:postgresql://localhost:5432/postgres" 4 USERNAME: "postgres" 5 PASS: "xxxx"

Error

1Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 22019-06-06 21:22:22.274 ERROR 19344 --- [ main] o.s.boot.SpringApplication : Application run failed 3 4 5Caused by: java.lang.NullPointerException: null 6org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE] 7 ... 5 common frames omitted 8 9

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

1T2R3M4

2019/06/06 13:15

To display the conditions report re-run your application with 'debug' enabled. はやってみましたか。
guest

回答2

0

解答ありがとうございます。
仰る通りでnewせずにインスタンスを作成する事により値の取得が出来ました。

ありがとうございます。

投稿2019/06/06 22:53

chihuahua_house

総合スコア23

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

java

1DBAccesser db = new DBAccesser();

が一番の原因です。

Spring経由でインスタンス取得していないので、Springの設定アノテーションを指定しても無視されます。
DBAccesserクラスも、@Service と宣言されていますので、Spring管理下に入ってますので、

java

1@Autowired 2DBAccesser db;

で宣言すれば、Springの設定が付与されたクラスのインスタンスが取得できます。

ただし、SpringBootの @SpringBootApplication で宣言したパッケージないしはそのサブパッケージにあるクラスでないと設定は自動的に反映されません。少なくともデフォルトの設定では、package com.xxxx.xxxx.ems; か、そのサブパッケージに配置しましょう。

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-spring-beans-and-dependency-injection.html

投稿2019/06/06 13:45

A-pZ

総合スコア12011

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問