2

This class reads a file CSV.

public class ReadCSVFile {

    private static final String SEMICOLON_DELIMITER = ";";


    public Map<Integer,Company> listFromFile(String csvFile) throws IOException {

        BufferedReader br = null;

        br = new BufferedReader(new 
        InputStreamReader(ReadCSVFile.class.getResourceAsStream(csvFile)));

        Map<Integer,Company> companyHashMap = new HashMap();

        String line;

        br.readLine();

        while ((line = br.readLine()) != null) {

            int pos = line.indexOf(SEMICOLON_DELIMITER);
            String companyCode = line.substring(0,pos);
            String companyName = line.substring(pos +1, line.length());

            companyHashMap.put(Integer.parseInt(companyCode), new Company(Integer.parseInt(companyCode), companyName));
        }

        return companyHashMap;
    }
}

This is the test for the class ReadCSVFile:

public class ReadCSVFileTest {

private ReadCSVFile readCSVFile;

@Before
public void before(){

    readCSVFile = new ReadCSVFile();
}

@Test
public void shouldExtractCompanyFromCSV() throws IOException {

    Map<Integer, Company> result = readCSVFile.listFromFile("test_company_list.csv");
    Assert.assertEquals(2,result.size());
    Assert.assertEquals("Goldman Sachs Group Inc",result.get(65).getCompanyName());
    Assert.assertEquals("Repsol YPF SA (Please refer to Repsol SA and YPF SA)",result.get(66).getCompanyName());
}

at the end this is the file to read test_company_list.csv and that I used to compare the result of the test:

 RepRisk Company ID;Company Name
65;Goldman Sachs Group Inc
66;Repsol YPF SA (Please refer to Repsol SA and YPF SA)

The test fails, I have this message:

java.lang.NullPointerException
    at java.io.Reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
    at app.ReadCSVFile.listFromFile(ReadCSVFile.java:21)
    at ReadCSVFileTest.shouldExtractCompanyFromCSV(ReadCSVFileTest.java:23)

What is wrong in my program? I think JUnit is correct as set up.

The line ReadCSVFile.java:21is this one:

br = new BufferedReader(new InputStreamReader(ReadCSVFile.class.getResourceAsStream(csvFile)));

instead the line (ReadCSVFileTest.java:23):

 Map<Integer, Company> result = readCSVFile.listFromFile("test_company_list.csv");
Ahmad Al-Kurdi
  • 2,248
  • 3
  • 23
  • 39
StoreCode
  • 155
  • 11

2 Answers2

2

Please read getResourceAsStream documentation.

  • @param name name of the desired resource
    • @return A {@link java.io.InputStream} object or {@code null} if
    • no resource with this name is found

Are you sure about the csvFile file that you send, is it in the correct path? it seems you have to use absolute name

Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72
Ahmad Al-Kurdi
  • 2,248
  • 3
  • 23
  • 39
  • I used relative path, copied by IntelliJ. It is a strange behaviour of the test, because 2 days ago it works correctly, but I didn't change nothing. – StoreCode Nov 28 '17 at 12:04
0

I fixed it using this instruction:

        br = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(csvFile)));

following the solution of this link:click here

StoreCode
  • 155
  • 11