【问题标题】:Objects created in method not available在方法中创建的对象不可用
【发布时间】:2021-03-07 12:32:29
【问题描述】:

我是第一次为学校项目使用 JavaFX,所以我很可能做错了什么。

在我的控制器类中,我有一个方法 (customerSU),它创建了一个新的客户和课程对象,我使用这些新客户和课程对象的其他方法有一个错误“无法解析符号 'cust1'”。

package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import javax.swing.*;
import java.io.IOException;

public class Controller {

 @FXML
 Button btn1Home, btn2Home, btn1SHome, btn1THome, btnStudentSU, btn1SHHome, btnBookLesson, btnAttendLesson, btnCancelLessons;

public void homeToSHome() throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("StudentSignIn.fxml"));

    Stage window = (Stage) btn1Home.getScene().getWindow();
    window.setScene(new Scene(root, 600, 400));
}

 public void homeToTHome() throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("TutorSignIn.fxml"));

    Stage window = (Stage) btn2Home.getScene().getWindow();
    window.setScene(new Scene(root, 600, 400));
}

public void SHomeToHome() throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("Home.fxml"));

    Stage window = (Stage) btn1SHome.getScene().getWindow();
    window.setScene(new Scene(root, 600, 400));
}

public void THomeToHome() throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("Home.fxml"));

    Stage window = (Stage) btn1THome.getScene().getWindow();
    window.setScene(new Scene(root, 600, 400));
}

public void SHHomeToHome()throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("Home.fxml"));

    Stage window = (Stage) btn1SHHome.getScene().getWindow();
    window.setScene(new Scene(root, 600, 400));
}

public void customerSU() throws IOException {
    Customer cust1 = new Customer(SUFirstName.getText(), SUSurname.getText(), SUEmail.getText());
    Lesson lesson1 = new Lesson();


    Parent root = FXMLLoader.load(getClass().getResource("StudentHome.fxml"));

    Stage window = (Stage) btnStudentSU.getScene().getWindow();
    window.setScene(new Scene(root, 600, 400));
}

public void bookLesson() throws IOException {
    String response;
    response = JOptionPane.showInputDialog("Please enter the name of the lesson you wish to sign up for.");

    lesson1.BookALesson(cust1, response);
}

public void attendLesson() throws  IOException {
    lesson1.attendALesson();
}

public void customerLessonDetails() throws IOException {
    lesson1.lessonDetails();
}

public void cancelLessons() throws IOException {
    lesson1.cancelLessons();
}
}

感谢您的帮助!

【问题讨论】:

  • 由于您在 costerSu() 方法中声明了 cust1,因此它仅在该方法中可见,而对于其他方法它们不存在,您面临此错误的唯一原因是您试图从中访问它另一种方法 bookLesson .
  • 仔细查看错误信息。编译器不仅会指出错误,还会告诉错误发生的位置。这就是你需要关注的地方。
  • @Dren 如何让它对其他方法可见?
  • minimal reproducible example 请(注意 M,同时可重现!)并遵守 java 命名约定
  • 您需要了解variable scope。另外,我认为在 JavaFX 应用程序中使用 JOptionPane 不是一个好主意。也许你应该改用Alert

标签: java intellij-idea javafx


【解决方案1】:

您无法从另一种方法访问在一种方法中声明的局部变量cust1。如果您想在不同的方法中使用它,请将其声明为类字段。

【讨论】:

  • 抱歉,这是一个愚蠢的问题,但我该怎么做呢?
  • 如果您想在不同的方法中使用它,请将其声明为类字段 可能但非常次优。如果你真的需要访问来自另一个类,添加api :)
  • @OP - 不傻,只是表明现在是学习 Java 语言基础教程的好时机 :)
猜你喜欢
  • 1970-01-01
  • 2011-07-24
  • 2022-01-02
  • 1970-01-01
  • 2016-09-19
  • 2010-12-18
  • 1970-01-01
  • 2021-01-16
  • 1970-01-01
相关资源
最近更新 更多