前提・実現したいこと
配列がnullだった場合にIllegalArgumentExceptionのエラーメッセージをmainメソッドに投げて、「引数はNULLです。」と表示させたい。
発生している問題・エラーメッセージ
本来配列の中身がnullだった場合には、「引数はNULLです。」とエラーメッセージを返したいが要素数が0の時に返されるエラーメッセージ「引数の要素数が不正です。」が返されてしまう。
java
1package com.ams.work210; 2 3public class Work210Test { 4 public static void main(String[] args) { 5 Work210 work210 = new Work210(); 6 System.out.println("[createRecordメソッド]"); 7 String[][] record = { { null }, {}, { "X" }, { "X", "X", "X", "X" }, { "X", "X", "X" }, 8 { "4903110006770", "毎朝のパン", "138" }, { "4903110006770", null, "138" }, 9 { "4903110006770", "毎朝の食パン", null } }; 10 11 for (int i = 0; i < record.length; i++) { 12 String eMessage = ""; 13 System.out.println("ケースNo" + (i + 1)); 14 15 for (int j = 0; j < record[i].length; j++) { 16 System.out.println("入力値:" + record[i][j]); 17 } 18 19 ProductBean productBean = new ProductBean(); 20 21 try { 22 productBean = work210.createRecord(record[i]); 23 } catch (IllegalArgumentException e) { 24 eMessage = e.getMessage(); 25 } 26 27 if (eMessage.length() != 0) { 28 System.out.println("結果:" + eMessage + "\n"); 29 } else { 30 System.out.println("結果:" + productBean + "\n"); 31 } 32 33 } 34 } 35} 36
java
1package com.ams.work210; 2 3import java.math.BigDecimal; 4 5public class Work210 { 6 7 public ProductBean createRecord(String[] record) throws IllegalArgumentException { 8 9 10 if (record == null) { 11 throw new IllegalArgumentException("引数がNULLです。"); 12 } 13 14 boolean hantei = record.length >= 4 || record.length == 0 || record.length < 3; 15 if (hantei == true) { 16 throw new IllegalArgumentException("引数の要素数が不正です。"); 17 } 18 19 try { 20 if (record[2] != null) { 21 Integer.parseInt(record[2]); 22 } 23 } catch (IllegalArgumentException e) { 24 throw new IllegalArgumentException("引数の第3要素が数値に変換できません"); 25 } 26 27 ProductBean productBean = new ProductBean(); 28 String code = record[0]; 29 String name = record[1]; 30 String keepPrice = record[2]; 31 32 if (record[2] == null) { 33 productBean.setCode(code); 34 productBean.setName(name); 35 productBean.setPrice(null); 36 37 return productBean; 38 39 } else { 40 BigDecimal price = new BigDecimal(keepPrice); 41 productBean.setCode(code); 42 productBean.setName(name); 43 productBean.setPrice(price); 44 45 return productBean; 46 } 47 48 } 49} 50
java
1package com.ams.work210; 2 3import java.math.BigDecimal; 4 5public class ProductBean { 6 private String code = ""; 7 private String name = ""; 8 private BigDecimal price = new BigDecimal(0); 9 10 public ProductBean() { 11 }; 12 13 public String getCode() { 14 return code; 15 } 16 17 public void setCode(String code) { 18 this.code = code; 19 } 20 21 public String getName() { 22 return name; 23 } 24 25 public void setName(String name) { 26 this.name = name; 27 } 28 29 public BigDecimal getPrice() { 30 return price; 31 } 32 33 public void setPrice(BigDecimal price) { 34 this.price = price; 35 } 36 37 @Override 38 public String toString() { 39 return "code=\"" + code + "\"\n" + "name=\"" + name + "\"\n" + "price=" + price; 40 } 41 42} 43
試したこと
条件式を"record[0] == null"のように変えてみたが、ArrayIndexOutOfBoundsExceptionエラーが返されてしまう。
> productBean = work210.createRecord(record[i]);
この行で渡してるrecord[i]は、コードを見る限り全て配列で、Nullは渡されてないです。
Nullを渡したときのコードをお願いします。
配列サイズが0なのであって配列の中身が null ではありません。
回答2件
あなたの回答
tips
プレビュー