Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Problem using the JFileChooser

  1. #1
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Problem using the JFileChooser

    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.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem using the JFileChooser

    "this.calc" is null
    Where is the variable: calc assigned a value? I do not see where calc is declared or assigned a value in the posted code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem using the JFileChooser

    Hi Norm, calc is what I called the instance,

    public class ReadFiles {
     
        Calculations calc;
        //String fullPath = "C:/temp/NewCrudeOil.csv"; // create a method so this is dynamic 
     
        File file;
        Path filePath;
     
        Scanner scan;
        int printArea = 0;
        int rowCount;
        int columnCount = 0;
        String line = "";
        int countColumn;
        int countLine = 0;
        String[][] fileArray;
     
        Header header = () -> System.out.printf("%-14s %-14s %-14s %-14s %-14s %-14s %-14s", "Date", "Open", "High", "Low", "Close", "Adj Close", "Volume");
     
        public ReadFiles(){
        file = new File(calc.newFilePath);
        filePath = Paths.get(calc.newFilePath);
        }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem using the JFileChooser

    You missed this question in my last post:
    Where is the variable: calc assigned a value?

    Its value will be null if it is not assigned a value.

    Reaching into another class for a value is a poor technique. The value should be passed to the constructor or a method, NOT accessed directly.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem using the JFileChooser

    Hi Norm,
    Lets see if I understood you correctly,

    1) "Where is the variable: calc assigned a value?"

    I assigned the variable right inside the JFileChooser code, the two lines within the 'if' statement.
    ** first line gets the path and the second line converts it to a string.

        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);
            }

    2) Its value will be null if it is not assigned a value.

    In the ReadFiles constructor is where I am trying to pull the value to,

        public ReadFiles(){
        file = new File(calc.newFilePath);
        filePath = Paths.get(calc.newFilePath);
        }

    3) To quickly touch upon your statement, "Reaching into another class for a value is a poor technique. The value should be passed to the constructor or a method, NOT accessed directly.",

    are you suggesting I use getters and setters for this. I never thought of that, may have to give that a try, or are you suggesting I put the JFileChooser code in a method that 'return' a value..

    If I am misunderstanding you, I do apologize.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem using the JFileChooser

    I assigned the variable right inside the JFileChooser code
    Do you know what an assignment statement is? It is a statement where a variable is assigned a value:
    theVariable = theValue; // assign theValue to theVariable

    I do not see any code that assigns a value to calc. I would expect the statement to look like this:
    calc = theValueForCalc;


    The names of the classes do not reflect their purposes and usages.
    Calculations should be a class that does calculations. I don't understand why it would contain the name of a file.
    ReadFiles is probably misnamed. It should be named ReadFile. The file it is supposed to read would be passed in a variable to its constructor or its methods.

    are you suggesting I put the JFileChooser code in a method that 'return' a value..
    More work needs to be done on the program's design before any more code is written.
    You are asking about where to put some code without having a design for the program. Design first, then code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Question about JFileChooser
    By rolfis in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 23rd, 2013, 07:24 PM
  2. JFileChooser
    By Karthik Prabhu in forum AWT / Java Swing
    Replies: 4
    Last Post: June 23rd, 2012, 04:47 AM
  3. JFileChooser Please Help
    By mulligan252 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: October 9th, 2011, 11:03 AM
  4. JFileChooser frustrations!
    By chrisivey1980 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 3rd, 2011, 05:32 AM
  5. JFileChooser
    By FretDancer69 in forum AWT / Java Swing
    Replies: 2
    Last Post: February 3rd, 2010, 06:35 AM