Javaを使って簡素なCUIのDBアプリを作成しています。
現在、ユーザ入力値をDBに入れるInsertの部分に取り組んでいるのですが、
下記エラーが出てしまいます。
エラー内容
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.jdbc.budgettracker.core.BudgetTrackerDto.setId(int)" because "budgetTrackerDto" is null at BudgetTrackerCui/com.jdbc.budgettracker.main.BudgetTrackerMain.main(BudgetTrackerMain.java:100)
BudgetTrackerMain.javaの100行目「budgetTrackerDto.setId(insertScannerInt);」の部分で、int型の変数の値をdtoにセットする形にしたいのですが、上記エラーが出てしまい、原因が掴みきれておりません。
97行目の「System.out.println(insertScannerInt);」でユーザ入力値の数値が渡せているかと推測しているのですが。。。
お手数ですが、解決の為、ヒントを頂けると幸いです。
よろしくお願いいたします。
BudgetTrackerMain.java
package com.jdbc.budgettracker.main; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import com.jdbc.budgettracker.core.BudgetTrackerDto; import com.jdbc.budgettracker.dao.BudgetTrackerDao; public class BudgetTrackerMain { @SuppressWarnings("null") public static void main(String[] args) throws FileNotFoundException, IOException, ParseException { // TODO Auto-generated method stub BudgetTrackerDao budgetTrackerDao = null; BudgetTrackerDto budgetTrackerDto = null; Map<Integer, String> initialSwitchMap = new HashMap<>(); initialSwitchMap.put(1, "Select"); initialSwitchMap.put(2, "Insert"); initialSwitchMap.put(3, "Update"); initialSwitchMap.put(4, "Delete"); initialSwitchMap.put(5, "Test"); Scanner initialSwitchScanner = new Scanner(System.in); int initialNumInt = 0; do { // String[] initialList = new String[] {initialSwitchMap.get(1), // initialSwitchMap.get(2), initialSwitchMap.get(3), initialSwitchMap.get(4)}; for (Map.Entry<Integer, String> list : initialSwitchMap.entrySet()) { System.out.println(list.getKey() + ":" + list.getValue()); } System.out.print("Select a number: "); String initialNumStr = initialSwitchScanner.next(); initialNumInt = Integer.parseInt(initialNumStr); } while (initialNumInt >= 5 || initialNumInt <= 0); switch (initialNumInt) { case 1: // Select System.out.println("You chose " + initialSwitchMap.get(1)); budgetTrackerDao = new BudgetTrackerDao(); Map<Integer, String> selectSwitchMap = new HashMap<>(); selectSwitchMap.put(1, "Select All"); selectSwitchMap.put(2, "Select by Date"); selectSwitchMap.put(3, "Select by Store"); selectSwitchMap.put(4, "Select by Product"); selectSwitchMap.put(5, "Select by Type"); selectSwitchMap.put(6, "Select by Price"); Scanner selectScanner = new Scanner(System.in); int selectScannerNumInt = 0; do { // String[] selectScanList = new String[] {selectSwitchMap.get(1), // selectSwitchMap.get(2), selectSwitchMap.get(3), selectSwitchMap.get(4), // selectSwitchMap.get(5)}; for (Map.Entry<Integer, String> selectScanlist : selectSwitchMap.entrySet()) { System.out.println(selectScanlist.getKey() + ":" + selectScanlist.getValue()); } System.out.print("Select a number: "); String selectScannerNumStr = selectScanner.next(); selectScannerNumInt = Integer.parseInt(selectScannerNumStr); } while (selectScannerNumInt > 7 || selectScannerNumInt <= 0); do { if (selectScannerNumInt == 1) { System.out.println("Select All----------"); List<BudgetTrackerDto> btd = budgetTrackerDao.selectAll(); for (BudgetTrackerDto b : btd) { System.out.println(b.getId() + ", " + b.getDate() + ", " + b.getStoreName() + ", " + b.getProductName() + ", " + b.getProductType() + ", " + b.getPrice()); } } } while (selectScannerNumInt > 7 || selectScannerNumInt <= 0); break; case 2: // Insert int insertcannerInt = 0; System.out.println("You chose " + initialSwitchMap.get(2)); Scanner insertScanner = new Scanner(System.in); System.out.print("Input an ID: "); int insertScannerInt = insertScanner.nextInt(); System.out.println(insertScannerInt); // insertcannerInt = Integer.parseInt(insertScannerStr); // System.out.println(insertcannerInt); budgetTrackerDto.setId(insertScannerInt); System.out.print("Input Date (yyyy-MM-dd): "); String insertScannerStr = insertScanner.next(); insertScannerStr = insertScanner.next(); Date insertDate=(Date) new SimpleDateFormat("yyyy-MM-dd").parse(insertScannerStr); budgetTrackerDto.setDate(insertDate); System.out.print("Input a store name: "); insertScannerStr = insertScanner.next(); budgetTrackerDto.setStoreName(insertScannerStr); System.out.print("Input a product name: "); insertScannerStr = insertScanner.next(); budgetTrackerDto.setProductName(insertScannerStr); System.out.print("Input a product type: "); insertScannerStr = insertScanner.next(); budgetTrackerDto.setProductType(insertScannerStr); System.out.print("Input price: "); insertScannerStr = insertScanner.next(); insertcannerInt = Integer.parseInt(insertScannerStr); // System.out.println(insertcannerInt); budgetTrackerDto.setPrice(insertcannerInt); budgetTrackerDao.insertIntoTable(budgetTrackerDto); } } }
BudgetTrackerDto.java
package com.jdbc.budgettracker.core; import java.sql.Date; public class BudgetTrackerDto { private int id; private Date date; private String storeName; private String productName; private String productType; private int price; public BudgetTrackerDto() { } public BudgetTrackerDto(int id, Date date, String storeName, String productName, String productType, int price) { super(); this.id = id; this.date = date; this.storeName = storeName; this.productName = productName; this.productType = productType; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String getStoreName() { return storeName; } public void setStoreName(String storeName) { this.storeName = storeName; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getProductType() { return productType; } public void setProductType(String productType) { this.productType = productType; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/12/06 18:48
2021/12/06 19:48 編集
2021/12/08 00:43
2021/12/08 12:28
2021/12/08 13:28 編集
2021/12/12 01:41
2021/12/12 04:02