简体   繁体   中英

getAbsolutePath() NullPointerException

I'm working on a Halo: CE custom game launcher in Java, and I'm setting up a preferences system using the Properties class in Java, so the user can set custom game paths. I use a JFileChooser to select a file and then write that path to the config file.

But, the program gives a Null Pointer Exception at this line: (This is in the event listener function)

if(source == fovChooseButton)
    {
        int returnVal = chooseFile.showOpenDialog(settingsWindow);
        if(returnVal == JFileChooser.APPROVE_OPTION)
        {
            File selected = chooseFOV.getSelectedFile();

            try
            {
                config.setProperty("STLPath", selected.getAbsolutePath()); //This line gives the exception
                config.store(new FileOutputStream(CONFIG_FILE), null);


            }

            catch(Exception e)
            {
                handleException(e);
            }
        }
    }

I do have another JFileChooser, and it does not throw any exceptions. Here's the code for the other one:

    if(source == fileChooseButton)
    {
        int returnVal = chooseFile.showOpenDialog(settingsWindow);
        if(returnVal == JFileChooser.APPROVE_OPTION)
        {
            File selected = chooseFile.getSelectedFile();

            try
            {
                config.setProperty("GamePath", selected.getAbsolutePath());

                config.store(new FileOutputStream(CONFIG_FILE), null);

            }

            catch(Exception e)
            {
                handleException(e);
            }
        } // end if

    }

All handleException() does is display a dialog window with the stack trace.

Help?

Your prompting the User for a file with chooseFile afterwards you are trying to read the file from the other filechooser chooseFOV

int returnVal = chooseFile.showOpenDialog(settingsWindow);
    if(returnVal == JFileChooser.APPROVE_OPTION)
    {
        File selected = chooseFOV.getSelectedFile();

int returnVal = chooseFile.showOpenDialog(settingsWindow); File selected = chooseFOV.getSelectedFile();

You have two variables and probably want to use chooseFile on the second line as well.

What's chooseFOV ? You seem to be using chooseFile for the dialog, so it's the one that's got a selection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM