title: Reading a csv file with Spring Boot application
body: <p>I am trying to convert a Java program I had written to read a csv file into a Spring Boot Application but keep getting a NullPointerException. I just want to print out the contents of the csv file.
  Here is the code:</p>
  
  <p>Beans.xml</p>
  
  <pre><code>&lt;?xml version = "1.0" encoding = "UTF-8"?&gt;
  
  &lt;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"&gt;
     &lt;bean id="Assembler" class="sample.spring.chapter01.Assembler"&gt;          
          &lt;property name="infopieces" value="classpath:1erelistearticle2.csv"&gt;&lt;/property&gt;         
     &lt;/bean&gt;
     &lt;bean id="PieceDAO" class="sample.spring.chapter01.PieceDAO"&gt;
          &lt;property name="listp" ref="Piece"&gt;&lt;/property&gt;
      &lt;/bean&gt;
     &lt;bean id = "Piece" class = "sample.spring.chapter01.Piece"/&gt;   
  &lt;/beans&gt;
  </code></pre>
  
  <p>Minapp.java</p>
  
  <pre><code>package 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");
  
          Assembler obj=(Assembler) context.getBean("Assembler");
  
  
          obj.display();
  
          try {
              ArrayList&lt;Piece&gt; listp=obj2.getList();
              for (Piece p:listp) {
                  System.out.println(p);
              }
          } catch (IOException ef) {
  
  
  
      }
  }
  </code></pre>
  
  <p>Assembler.java</p>
  
  <pre><code>package sample.spring.chapter01;
  
  import java.io.BufferedReader;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.IOException;
  
  import com.opencsv.CSVReader;
  
  import org.apache.commons.csv.CSVFormat;
  import org.apache.commons.csv.CSVParser;
  import org.apache.commons.csv.CSVRecord;
  import org.springframework.core.io.Resource;
  
  public class Assembler {
      private PieceDAO listp;
      //private CSVParser parser;
      private Resource infopieces;
  
      public void setlistp(PieceDAO lp) throws FileNotFoundException, IOException {
          fill();     
      }
  
      public void setinfopieces(Resource csvFile){
          this.infopieces = csvFile;
      }
  
      public PieceDAO getlistp() {
          return listp;
      }
  
      public CSVParser getinfocsv() throws FileNotFoundException, IOException{
           //   BufferedReader br = new BufferedReader(new FileReader("classpath:1erelistearticle.csv"));
  
              //BufferedReader br = new BufferedReader(new FileReader("classpath:1erelistearticle.csv"));
  
              InputStream is=infopieces.getInputStream();
              BufferedReader br = new BufferedReader(new InputStreamReader(is));              
  
              //ClassLoader classloader=Thread.currentThread().getContextClassLoader();
              //InputStream is=classloader.getResourceAsStream("classpath:1erelistearticle.csv");
              //CSVReader cr=new CSVReader(is);
              CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(br);
              //CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(is);
              //for (CSVRecord record: parser) {
              //    System.out.println(record.get("référence ascensoriste"));
              //}
              return parser;
      }
  
      public void fill() throws FileNotFoundException, IOException{
  
          CSVParser parser=getinfocsv();
          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.addPiece(lift_comp);
          }
      }
  
      public void display() {
          listp.output();
      }
  }
  </code></pre>