前提・実現したいこと
JavaのOpenCsvを利用してCSVファイルを読み取り、CSVファイルの中のCodeが"m"から始まる人物を取得して、そのnameを小文字にする。
発生している問題・エラーメッセージ
UserCsvReader.java:15 が該当クラスの ".withType(User.class).build().parse();"の行です。
java.lang.RuntimeException: com.opencsv.exceptions.CsvBeanIntrospectionException: Basic instantiation of the given bean type (and subordinate beans created through recursion, if applicable) was determined to be impossible. at com.opencsv.bean.concurrent.IntolerantThreadPoolExecutor.checkExceptions(IntolerantThreadPoolExecutor.java:253) at com.opencsv.bean.concurrent.LineExecutor.checkExceptions(LineExecutor.java:67) at com.opencsv.bean.concurrent.IntolerantThreadPoolExecutor.areMoreResultsAvailable(IntolerantThreadPoolExecutor.java:303) at com.opencsv.bean.concurrent.IntolerantThreadPoolExecutor.tryAdvance(IntolerantThreadPoolExecutor.java:313) at com.opencsv.bean.concurrent.LineExecutor.tryAdvance(LineExecutor.java:24) at java.base/java.util.Spliterator.forEachRemaining(Spliterator.java:326) at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) at com.opencsv.bean.CsvToBean.parse(CsvToBean.java:117) at csv.UserCsvReader.readUserCsv(UserCsvReader.java:15) at csv.Main.main(Main.java:9)
Exception in thread "main" Exception in thread "pool-1-thread-8" Exception in thread "pool-1-thread-1" Exception in thread "pool-1-thread-12" Exception in thread "pool-1-thread-9" java.lang.RuntimeException: com.opencsv.exceptions.CsvBeanIntrospectionException: Basic instantiation of the given bean type (and subordinate beans created through recursion, if applicable) was determined to be impossible. at com.opencsv.bean.concurrent.ProcessCsvLine.run(ProcessCsvLine.java:111) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:834) Caused by: com.opencsv.exceptions.CsvBeanIntrospectionException: Basic instantiation of the given bean type (and subordinate beans created through recursion, if applicable) was determined to be impossible. at com.opencsv.bean.AbstractMappingStrategy.createBean(AbstractMappingStrategy.java:204) at com.opencsv.bean.AbstractMappingStrategy.populateNewBean(AbstractMappingStrategy.java:325) at com.opencsv.bean.concurrent.ProcessCsvLine.processLine(ProcessCsvLine.java:131) at com.opencsv.bean.concurrent.ProcessCsvLine.run(ProcessCsvLine.java:87) ... 3 more Caused by: java.lang.InstantiationException: csv.User at java.base/java.lang.Class.newInstance(Class.java:571) at com.opencsv.bean.AbstractMappingStrategy.createBean(AbstractMappingStrategy.java:196) ... 6 more Caused by: java.lang.NoSuchMethodException: csv.User.<init>() at java.base/java.lang.Class.getConstructor0(Class.java:3349) at java.base/java.lang.Class.newInstance(Class.java:556) ... 7 more java.lang.RuntimeException: com.opencsv.exceptions.CsvBeanIntrospectionException: Basic instantiation of the given bean type (and subordinate beans created through recursion, if applicable) was determined to be impossible. at com.opencsv.bean.concurrent.ProcessCsvLine.run(ProcessCsvLine.java:111) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:834)
該当のソースコード
java
1public class Main { 2 public static void main(String[] args) { 3 var users = new UserCsvReader().readUserCsv("users.csv"); 4 5 List<User> list = users.stream() 6 .filter(code -> { 7 if(code.getCode().startsWith("m")){ 8 return true; 9 } 10 return false; 11 }) 12 .collect(Collectors.toList()); 13 14 List<String> list2 = list.stream() 15 .map(name -> name.getName().toLowerCase()) 16 .collect(Collectors.toList()); 17 18 list2.stream() 19 .forEach((name) -> System.out.println(name)); 20 } 21}
Java
1package csv; 2 3import com.opencsv.bean.CsvToBeanBuilder; 4 5import java.io.FileNotFoundException; 6import java.io.FileReader; 7import java.util.ArrayList; 8import java.util.List; 9 10public class UserCsvReader { 11 12 public List<User> readUserCsv(String csvFilePath) { 13 try { 14 return new CsvToBeanBuilder<User>(new FileReader(csvFilePath)) 15 .withType(User.class).build().parse(); 16 } catch (FileNotFoundException e) { 17 e.printStackTrace(); 18 return new ArrayList<>(); 19 } 20 } 21}
Java
1package csv; 2 3import com.opencsv.bean.CsvBindByName; 4 5public class User { 6 @CsvBindByName 7 private String name; 8 @CsvBindByName 9 private String code; 10 @CsvBindByName 11 private String grade; 12 13 public User(String name, String code, String grade) { 14 this.name = name; 15 this.code = code; 16 this.grade = grade; 17 } 18 19 public String getName() { 20 return name; 21 } 22 23 public void setName(String name) { 24 this.name = name; 25 } 26 27 public String getCode() { 28 return code; 29 } 30 31 public void setCode(String code) { 32 this.code = code; 33 } 34 35 public String getGrade() { 36 return grade; 37 } 38 39 public void setGrade(String grade) { 40 this.grade = grade; 41 } 42}
csv
1name,code,grade 2K****,b2**,4 3M****,b2**,4 4M****,b2**,4 5Y****,b2**,4 6I****,m2**,1 7O****,m2**,1 8K****,m2**,1 9S****,m2**,1 10A****,m2**,2 11N****,m2**,2 12H****,m2**,2 13Y****,m2**,2
試したこと
CSVファイルは実在の名前が使われているのでアスタリスクでぼかしてあります。実際は内容に重複はありません。
また、エラーコードも上で記述したものが何回かループして長くなっています。
補足情報(FW/ツールのバージョンなど)
1macOS Mojave 10.14.6
IntelLiJ IDEA 2019.1.2
Java version 13.0.2
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/11/29 10:04