0

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".

dsh
  • 12,037
  • 3
  • 33
  • 51
jesric1029
  • 698
  • 3
  • 10
  • 33
  • Can you paste the entire stack trace that gets dumped into the logs? I see that the null pointer is an issue, but I suspect that its a symptom of your buffered reader not properly initializing. – Nick DeFazio Oct 30 '15 at 16:43
  • The exception is being thrown from this line: `br.close();` in your nested try/catch block inside `finally`. It means `br` is never successfully initialized by the time the exception is thrown. – TayTay Oct 30 '15 at 16:44
  • As a possible workaround to what @Tgsmith61591 mentioned - look into try with resource statements -https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html – Nick DeFazio Oct 30 '15 at 16:47
  • Thanks guys, actually the answer was quite obvious and a silly mistake. As it turns out I was declaring the GUI object inside of the main class and then calling the method. This resulted in the JFileChooser file and directory being set for that object. I then had created another method inside of the writer class that called a new GUI object, so it was attempting to pull null values. The solution was to run the GUI method inside the same class I was creating the buffered writer. – jesric1029 Oct 30 '15 at 17:46

0 Answers0