私はcsvファイルを読み込むために書いたJavaプログラムをSpringブートアプリケーションに変換しようとしていますが、引き続きNullPointerExceptionが発生しています。私はちょうどcsvファイルの内容を印刷したいと思う。ここ はコードです:Springブートアプリケーションでcsvファイルを読む
beans.xmlの
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="PieceDAO" class="sample.spring.chapter01.PieceDAO">
<property name="listp" ref="Piece"></property>
</bean>
<bean id = "Piece" class = "sample.spring.chapter01.Piece"/>
</beans>
Minapp.java
パッケージsample.spring.chapter01。
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
public class MinApp {
private static ApplicationContext context;
private static Logger logger = Logger.getLogger(MinApp.class);
public static void main(String[] args) throws FileNotFoundException, IOException{
context=new ClassPathXmlApplicationContext("Beans.xml");
Resource resource =context.getResource("classpath:1erelistearticle2.csv");
PieceDAO obj=(PieceDAO) context.getBean("PieceDAO");
obj.fill(resource);
obj.display();
}
}
PieceDAO.java
package sample.spring.chapter01;
import java.util.List;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.springframework.core.io.Resource;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class PieceDAO {
private List<Piece> listp;
//private Resource resource;
public PieceDAO() {
}
public void setlistp(Piece p) throws FileNotFoundException, IOException{
listp=new ArrayList<Piece>();
//this.fill(resource);
}
public Piece getlistp(Piece p) {
for (Piece currp:listp) {
if (currp.equals(p)) {
return currp;
}
}
return null;
}
public void fill(Resource resource) throws FileNotFoundException, IOException{
InputStream is=resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(br);
for (CSVRecord record:parser) {
String ref=record.get("référence ascensoriste").trim();
String asc=record.get("ascensoriste").trim();
String desc=record.get("Description article").trim();
String prix=record.get("Pv");
String category=record.get("Familie").trim();
//System.out.println(category);
Piece lift_comp=new Piece();
lift_comp.setasc(asc);
lift_comp.setdesc(desc);
lift_comp.setref(ref);
lift_comp.setprice(prix);
lift_comp.settype(category);
lift_comp.setinfo();
listp.add(lift_comp);
}
}
public void display() {
for (Piece p:listp) {
System.out.println(p);
}
}
}
あなたは例外が発生している行をplsで指摘できますか? –