I am trying to separate an old program I wrote into multiple classes. The old program had a method that was 4000 lines so obviously this won't suffice. Right now I'm trying to get past a persistent null point error.
What I want to accomplish is to have a GUI class, buffered reader/writer, and main. I want the main class to first call the GUI which will allow the user to select a file and save directory. I then want the reader/writer to scan the file and save the number of lines starting with "5" as a variable to be used later. I will add a second scan of the file later to manipulate the data but for now I'm simply trying to scan for the number of lines beginning with five and I keep getting this pesky null pointer & have no idea why..
Main Class
package nachamulti;
public class NachaMain
{
public static void main(String args[]){
NachaWriter writer = new NachaWriter();
NachaGUI gui = new NachaGUI();
gui.getFile();
gui.setDirectory();
writer.getTotalBatches();
System.out.println(writer.fiveTotal);
}
}
GUI Class
package nachamulti;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class NachaGUI{
JFileChooser uploadFile;
JFileChooser saveFile;
public void getFile(){
JFileChooser chooser = new JFileChooser();
uploadFile = chooser;
FileNameExtensionFilter filter = new FileNameExtensionFilter("ACH Files", "ach");
chooser.setFileFilter(filter);
chooser.setDialogTitle("Please choose ACH file to upload");
int returnVal = chooser.showOpenDialog(chooser);
if(returnVal == JFileChooser.APPROVE_OPTION){
}
}
public void setDirectory(){
JFileChooser chooser2 = new JFileChooser();
saveFile = chooser2;
chooser2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnValue2 = chooser2.showDialog(chooser2, "Directory to save");
if(returnValue2 == JFileChooser.APPROVE_OPTION){
}
}
Buffered Reader Class
package nachamulti;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class NachaWriter
{
BufferedReader br = null;
BufferedWriter bw = null;
BufferedReader br1 = null;
String five = null;
String eight = null;
String one = null;
ArrayList<String> sixList = new ArrayList<String>();
int fiveCounter=0;
int fiveTotal=0;
int nineCounter=0;
NachaGUI gui = new NachaGUI();
public void getTotalBatches(){
try{
String sCurrentLine;
br = new BufferedReader(new FileReader(gui.uploadFile.getSelectedFile()));
while((sCurrentLine=br.readLine()) != null){
if(sCurrentLine.startsWith("5")){
fiveTotal++;
}
}
}catch(IOException ex){
ex.printStackTrace();
}finally{
try{
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
Stack Trace
Exception in thread "main" java.lang.NullPointerException
at nachamulti.NachaWriter.getTotalBatches(NachaWriter.java:43)
at nachamulti.NachaMain.main(NachaMain.java:16)
The input file does not much matter. As long as it has lines of text and at least some begin with the number "5".