Hello Everyone,
To explain my situation, I just did an assignment for my Java class, scored very well on it. Now I am onto my next assignment which is a continuation on the last.
My last assignment was to grab data from a CSV file and do some manipulations to the data. My last code, I had the path hard coded in like this,
String newPath = "C:/temp/CrudeOil.csv";
This assignment my instructor wants us to modify the last so that it will work with other CSV files. So I thought I would attempt the following,
public void fileExployer() { JFileChooser newFile = new JFileChooser(); newFile.setCurrentDirectory(new File("c:\\temp")); int response = newFile.showOpenDialog(null); if (response == JFileChooser.APPROVE_OPTION) { File file = new File(newFile.getSelectedFile().getAbsolutePath()); newFilePath = file.toString(); ReadFiles readFiles = new ReadFiles(); System.out.println(newFilePath); // using this just to test if it reads the selection } }
The path from the selection from the file explorer prints absolutely fine, so I assigned the 'newFilePath' as a global String variable, so that I could use it in another class that takes the CSV file and creates an array[][] from it. Problem is it keeps erroring out. Error reads as follows;
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot read field "newFilePath" because "this.calc" is null
I have the code for the file explorer in a class called Calulations in a method called fileExplorer and the code that needs the selection is in a class called ReadFiles, in the constructor for this class I have the following,
(at the start of the class I have declared the following,
File file;
Path filePath;
)
public ReadFiles(){ file = new File(calc.newFilePath); filePath = Paths.get(calc.newFilePath); }
My thought was when the 'OPEN' button was pressed a new instance of ReadFiles would start and the constructor would grab the string path of the selection.
PLEASE NOTE: my class has not touched upon the file explorer feature of java yet, I am taking upon myself to learn this and try and make this interactive and not only for one hard coded file.
If any this makes any sense and you think you can point me in the right direction, I would appreciate it.