Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have method written for downloading files from server and saving it to the classpath of the project. I can see the files in project folder after its been downloaded. But when I call read method on the files I get exception. So when I terminate the execution in between (the files are still in the folder) and try to run the program again it works fine now. I am not sure what is the issue because I have tried putting wait after the files are downloaded and it does not work. Somehow the files are not readable just after download. Can anyone tell me what can be the issue?

public Map<String, String> readCsv(String csvName) {
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";
    String csvFileName = csvName;

    Map<String, String> abcCsv = new HashMap<>();
    InputStream inputStream = getClass().getClassLoader().getResourceAsStream(csvFileName);

    try {
        br = new BufferedReader(new InputStreamReader(inputStream));
        while ((line = br.readLine()) != null) {

            String[] abc = line.split(cvsSplitBy);

            abcCsv.put(
                    (abc[0]).trim().replaceAll(REP1, "").replaceAll(REP2, " ").replaceAll(REP6, ""),
                    (abc[1]).trim().replaceAll(REP1, "").replaceAll(REP2, " ").replaceAll(REP7, "")
                            .replaceAll(REP4, ""));
        }

    } catch (FileNotFoundException e) {
        LOGGER.error("File not found exception thrown ", e);
    } catch (IOException e) {
        LOGGER.error("I/O exception thrown ", e);
    } catch (ArrayIndexOutOfBoundsException e) {
        LOGGER.error("Array index out of bounds.");
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                LOGGER.error("I/O exception thrown ", e);
            }

        }
    }
    return abcCsv;

}


What I have tried:

replaced getResource or getResourceAsStream; used new FileInputStream. but still get the same error.
Posted
Updated 13-Jun-16 6:17am
Comments
Dave Kreskowiak 13-Jun-16 11:03am    
and the Exception message would be ????
Member 12581170 13-Jun-16 11:22am    
Console Output:

java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at com.db.awm.api.support.RestAPIHelper.readCsvPerson(RestAPIHelper.java:181)
at com.db.awm.api.support.RestAPIHelper.getDataFromCsv(RestAPIHelper.java:84)
at com.db.awm.api.support.RestAPIUtil.validateCsvAndBasePerson(RestAPIUtil.java:466)
at com.db.awm.api.support.RestAPIUtil.validateCsvAndBase(RestAPIUtil.java:436)
at com.db.awm.step.definitions.api.RestApiCommonSteps.shouldExecuteAPI(RestApiCommonSteps.java:82)
Sergey Alexandrovich Kryukov 13-Jun-16 11:16am    
Does your code catch this exception, or is it some other, uncaught exception? How about comprehensive exception information?
—SA
DaveAuld 13-Jun-16 12:36pm    
Are you sure the writer is releasing the resources correctly after downloading the files, doesn't sound like it to me.

1 solution

Your code makes some assumptions, such as it has no idea if anything was returned by .getResourceAsStream(csvFileName). inputStream could end up being null and you wouldn't know it until the code crashed.

Your code doesn't check to see if the file exists before trying to open it.

Your code also assumes that every line it reads is OK and has the correct number of fields in it. You don't check for these things at all.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900