2017-05-24 18 views
0

私は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); 
     } 
    } 

} 
+0

あなたは例外が発生している行をplsで指摘できますか? –

答えて

0

コードで問題がたくさんあります。私は最初のものは

コールlistp.output();を呼び出しますが、listpが割り当てられることはありません

obj.display(); 

だと思います。設定からもコードからも。

+0

ありがとうございます。私はlistp変数を正しく割り当てて、すべて正常に動作します。あなたはコードに多くの問題を述べました。あなたは指定できますか?ありがとう –

+0

いくつかの問題を修正しました。しかし実際には、外部から割り当てるよりもPieceDAOにリソースをAutowireするほうが良いです。前の処理の途中でリソースを割り当てると、スレッドセーフではありません。あなたは単に全リストを印刷することができます。それを繰り返す必要はありません。私はなぜ 'piece getlistp(Piece p)'メソッドでequals()でチェックしても、contains()にcontains()できないのでしょうか? – StanislavL

+0

btw:正しい答えとして受け入れる – StanislavL

0

問題は解決されました。 classとbeans.xmlは上記のように編集され、Springフレームワークを使用してcsvファイルを読み込みます。

関連する問題