gpt4 book ai didi

java - getAbsolutePath() 空指针异常

转载 作者:行者123 更新时间:2023-11-29 05:55:10 32 4
gpt4 key购买 nike

我正在使用 Java 开发 Halo: CE 自定义游戏启动器,我正在使用 Java 中的 Properties 类设置首选项系统,以便用户可以设置自定义游戏路径。我使用 JFileChooser 选择一个文件,然后将该路径写入配置文件。

但是,程序在这一行给出了一个空指针异常:(这是在事件监听器函数中)

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

我有另一个 JFileChooser,它不会抛出任何异常。这是另一个的代码:

    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

}

handleException() 所做的只是显示一个带有堆栈跟踪的对话窗口。

帮忙吗?

最佳答案

你提示用户使用 chooseFile 之后你试图从其他文件选择器 chooseFOV 读取文件

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

关于java - getAbsolutePath() 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12450788/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com