2

All seems to be in order, but somehow it isn't. Here is the code;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

     String csvFilename = "src/example.csv"; 
            CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
            String[] row = null;
            String total = "";
            while((row = csvReader.readNext()) != null) {

                for( int i = 0 ; i < 200 ; i++ ){  // no higher than num of columns to be found or error

                    String saveAway = row[i];
                    //tabl[saveInThisRow][i] = row[i];
                    tabl[saveInThisRow][i] = saveAway.replace('_', ' ');

                }
                saveInThisRow++;
                if(saveInThisRow == 50) { saveInThisRow = 0; break; }

            }
            //saveInThisRow = 0;
            // ctrl-i  = auto format

            csvReader.close();

The path is as far as I can tell correct (its in the src), maybe there is is something wrong with the csv instead? It worked nicely in Eclipse, but now in Intellij its broken... whats going on here?

This is the stack trace;

java.io.FileNotFoundException: src\example.csv (The system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at java.io.FileReader.<init>(FileReader.java:58)
    at net.klingt.example.LoadCsv.readCsv(LoadCsv.java:61)
    at net.klingt.example.ProcessingExample.draw(ProcessingExample.java:252)
    at processing.core.PApplet.handleDraw(PApplet.java:2386)
    at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
    at processing.core.PApplet.run(PApplet.java:2256)
    at java.lang.Thread.run(Thread.java:745)

Now I have this;

 public void readCsv(   ) throws IOException, URISyntaxException { // throws IOException  ---  String[] args
        System.out.println(".............");
        System.out.println(System.getProperty("user.dir"));

        val = 20; // testing purposes
        String [][] tab  = new String [100][400];
        int saveInThisRow = 0;

        File file = new File(getClass().getResource("src/resources/GEMSTONES05.csv").toURI());
        String csvFilename = "src/resources/GEMSTONES05.csv";

        CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
        String[] row = null;
        String total = "";
        while((row = csvReader.readNext()) != null) {

            for( int i = 0 ; i < 20 ; i++ ){  // no higher than num of columns to be found or error
                String saveAway = row[i];
                parent.println("CVS read  " + saveAway);
                //tabl[saveInThisRow][i] = row[i];
                tabl[saveInThisRow][i] = saveAway.replace('_', ' ');
            }
            saveInThisRow++;
            if(saveInThisRow == 20) { saveInThisRow = 0; break; }

        }

        csvReader.close();

        for( int i = 0 ; i < 300 ; i++){

        }

    }

I am a bit lost how & where to use "file", here is the stack trace

C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.0.3\jre\jre\bin
Exception in thread "Animation Thread" java.lang.NullPointerException
    at net.klingt.example.LoadCsv.readCsv(LoadCsv.java:41)
    at net.klingt.example.ProcessingExample.draw(ProcessingExample.java:253)
    at processing.core.PApplet.handleDraw(PApplet.java:2386)
    at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
    at processing.core.PApplet.run(PApplet.java:2256)
    at java.lang.Thread.run(Thread.java:745)

I no longer have the file not found error, instead now its an null pointer exception....

  • Have you checked the current working directory? I don't really know eclipse, but it might be that eclipse runs your application in a different working directory than intellij http://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java – ipa May 16 '15 at 11:41
  • When I right click on the cvs and choose file_path, it shows that its located in the src, so it should be correct yes (?) – Comic_Interlude May 16 '15 at 11:45
  • 1
    These are two different things. Your application can be called using the option -Duser.dir='/some/path/to/anywhere' (depends on the config of your IDE or maven). Then 'new FileReader(csvFilename)' tries to open the file '/some/path/to/anywhere/src/example.csv'. – ipa May 16 '15 at 11:50
  • could you post the exact stack trace in your question – Blip May 16 '15 at 11:52
  • I added the stack trace as an answer – Comic_Interlude May 16 '15 at 12:22

2 Answers2

4

try with this

File file = new File(getClass().getResource("/example.csv").toURI());

it gets the file from src folder of your project

and make sure that example.csv is present in src folder.

change you readCSV() method to the following..

public void readCsv(   ) throws IOException, URISyntaxException { // throws IOException  ---  String[] args
    System.out.println(".............");
    System.out.println(System.getProperty("user.dir"));

    val = 20; // testing purposes
    String [][] tab  = new String [100][400];
    int saveInThisRow = 0;

    File file = new File(getClass().getResource("/resources/GEMSTONES05.csv").toURI());
    //String csvFilename = "/resources/GEMSTONES05.csv";

    CSVReader csvReader = new CSVReader(new FileReader(file));
    String[] row = null;
    String total = "";
    while((row = csvReader.readNext()) != null) {

        for( int i = 0 ; i < 20 ; i++ ){  // no higher than num of columns to be found or error
            String saveAway = row[i];
            parent.println("CVS read  " + saveAway);
            //tabl[saveInThisRow][i] = row[i];
            tabl[saveInThisRow][i] = saveAway.replace('_', ' ');
        }
        saveInThisRow++;
        if(saveInThisRow == 20) { saveInThisRow = 0; break; }

    }

    csvReader.close();

    for( int i = 0 ; i < 300 ; i++){

    }

}

while getting the Resource, you don't have to write the src folder name.. Because at runtime, jvm will get the resources from src folder and look for the next path.

ELITE
  • 5,815
  • 3
  • 19
  • 29
  • thx for the reply, but now I get ; Exception in thread "Animation Thread" java.lang.NullPointerException, – Comic_Interlude May 16 '15 at 12:32
  • change variable from `File file = new File(getClass().getResource("src/resources/GEMSTONES05.csv").toURI());` to `File file = new File(getClass().getResource("/resources/GEMSTONES05.csv").toURI());` and in `CSVReader` pass the `FileReader` object as `new FileReader(file)`.. – ELITE May 16 '15 at 17:06
0

"src/example.csv" is a relative path, try absolute path, like c:/work/src/example.csv. FileNotFoundException means file can't be found, often happens when you misuse relative path. you can print System.getProperty("user.dir") to see current directory of the process, then use correct relative path.

mysh
  • 383
  • 2
  • 10
  • Even when I use an absolute path, it stild does not work, same error all over again, This might possible mean that the problem is not the path but something else, maybe the csv cannot be read because something is not okay within the csv? – Comic_Interlude May 16 '15 at 13:45
  • there is nothing to do with csv file, because exception thrown before open it. try copying the csv file to a directory that is hard to fail, like c:/ . – mysh May 16 '15 at 14:00