Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 没有返回正确的输出 publicmap getTimetable(){ 返回时间表; } 公共void加载文件(){ JFileChooser fileChooser=新的JFileChooser(); int status=fileChooser.showOpenDialog(null); if(status==JFileChooser.APPROVE\u选项) { File selectedFile=fileChooser.getSelectedFile(); 试一试{ filePath=selectedFile.getPath(); fileName.setText(selectedFile.getName()); this.timeline=tr.read(文件路径); }捕获(IOEX异常){ JOptionPane.showMessageDialog(null,“访问文件时出现问题”+selectedFile.getAbsolutePath(),nameOfFile,JOptionPane.ERROR\u消息); }捕获(格式化异常){ showMessageDialog(null,“无效文件格式”,文件名,JOptionPane.ERROR\u消息); } } }_Java_Variables_Input - Fatal编程技术网

Java 没有返回正确的输出 publicmap getTimetable(){ 返回时间表; } 公共void加载文件(){ JFileChooser fileChooser=新的JFileChooser(); int status=fileChooser.showOpenDialog(null); if(status==JFileChooser.APPROVE\u选项) { File selectedFile=fileChooser.getSelectedFile(); 试一试{ filePath=selectedFile.getPath(); fileName.setText(selectedFile.getName()); this.timeline=tr.read(文件路径); }捕获(IOEX异常){ JOptionPane.showMessageDialog(null,“访问文件时出现问题”+selectedFile.getAbsolutePath(),nameOfFile,JOptionPane.ERROR\u消息); }捕获(格式化异常){ showMessageDialog(null,“无效文件格式”,文件名,JOptionPane.ERROR\u消息); } } }

Java 没有返回正确的输出 publicmap getTimetable(){ 返回时间表; } 公共void加载文件(){ JFileChooser fileChooser=新的JFileChooser(); int status=fileChooser.showOpenDialog(null); if(status==JFileChooser.APPROVE\u选项) { File selectedFile=fileChooser.getSelectedFile(); 试一试{ filePath=selectedFile.getPath(); fileName.setText(selectedFile.getName()); this.timeline=tr.read(文件路径); }捕获(IOEX异常){ JOptionPane.showMessageDialog(null,“访问文件时出现问题”+selectedFile.getAbsolutePath(),nameOfFile,JOptionPane.ERROR\u消息); }捕获(格式化异常){ showMessageDialog(null,“无效文件格式”,文件名,JOptionPane.ERROR\u消息); } } },java,variables,input,Java,Variables,Input,我遇到的问题是,在getTimetable中返回的时间表没有返回任何内容,即使我在loadFile方法中将其设置为某个内容。如有任何建议,将不胜感激 没有足够的代码可以确定,但我建议: public Map<Route, List<Service>> getTimetable() { return timetable; } public void loadFile() { JFileChooser fileChooser = new JFileChoo

我遇到的问题是,在getTimetable中返回的时间表没有返回任何内容,即使我在loadFile方法中将其设置为某个内容。如有任何建议,将不胜感激

没有足够的代码可以确定,但我建议:

public Map<Route, List<Service>> getTimetable() {
    return timetable;
}


public void loadFile() {
    JFileChooser fileChooser = new JFileChooser();
    int status = fileChooser.showOpenDialog(null);
    if (status == JFileChooser.APPROVE_OPTION)
    {
        File selectedFile = fileChooser.getSelectedFile();
        try {
            filePath= selectedFile.getPath();
            fileName.setText(selectedFile.getName());
            this.timetable = tr.read(filePath);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Problem accessing file "+ selectedFile.getAbsolutePath(), nameOfFile, JOptionPane.ERROR_MESSAGE);
            } catch (FormatException e) {
                JOptionPane.showMessageDialog(null, "Invalid file format", nameOfFile, JOptionPane.ERROR_MESSAGE);
            }
        } 
    }
publicmap getTimetable(){
loadFile();
返回时间表;
}

或者,您以前调用过加载文件吗?在同一个实例中?

请给出一个简短但完整的示例来说明问题。我们目前没有足够的信息。我可以猜:
tr.read(filePath)抛出您在代码中捕获但不处理的异常…您的
catch
子句中到底是什么?。它很可能类似于忽略任何错误*/
,在这种情况下,您永远不会知道
this.timeline=tr.read(filePath)
失败,并且没有初始化您的
timeline
成员变量。catch语句捕获了我刚刚从代码中输入的所有问题。当我在loadFile方法中执行System.out.println(时间表)时,我会打印出时间表。但是,如果我在getTimetable方法中执行相同操作,则不会打印任何时间表。请返回
this.timeline
from
getTimetable()
Hiya谢谢您的回复!我已经尝试了您的代码建议,但它似乎没有返回任何内容
public Map<Route, List<Service>> getTimetable() {
loadFile();
    return timetable;
}