更改场景时的JavaFX空指针表达式

更改场景时的JavaFX空指针表达式,java,javafx,Java,Javafx,我在更改JavaFX场景时遇到问题。当我尝试切换场景时,我得到一个空指针异常,但是我使用调试器调试它,它告诉我stage指针为空。我注意到它没有找到stage的start方法的构造函数声明,所以我为整个类创建了一个变量。我不确定为什么这个阶段指针是空的,因为我在类中声明了变量 以下是主控制器切换场景的方法: onstart.changeScene("password.fxml"); 下面是OnStart类,用于切换场景: public class OnStart extends Applica

我在更改JavaFX场景时遇到问题。当我尝试切换场景时,我得到一个空指针异常,但是我使用调试器调试它,它告诉我stage指针为空。我注意到它没有找到stage的start方法的构造函数声明,所以我为整个类创建了一个变量。我不确定为什么这个阶段指针是空的,因为我在类中声明了变量

以下是主控制器切换场景的方法:

onstart.changeScene("password.fxml");
下面是OnStart类,用于切换场景:

public class OnStart extends Application{
Parent root;
private Stage stage;
//private AnchorPane mainLayout;
//private HashMap<String, Pane> screenMap = new HashMap<>();
private Scene main;
FXMLLoader loader = new FXMLLoader();

@Override
public void start(Stage stage) throws Exception{
    this.stage = stage;
    root = loader.load(getClass().getResource("mainmenu.fxml"));

    main = new Scene(root);

    this.stage.setTitle("Photo Encryptor 9000");
    this.stage.setScene(main);
    this.stage.show();
}

public Parent getRoot() {
    return root;
}

public Scene getScene() { return main; }

public void changeScene(String fxml) throws IOException {
    root = loader.load(
            getClass().getResource(fxml));
    stage.getScene().setRoot(root);
}
}
主控制器:

public class MainController implements Initializable{

public String path, name;
private String passwordPlainText;
private boolean hasname;
private File f;
FileWriter out;
OnStart onstart = new OnStart();
BufferedImage image;
int widthofimage, heightofimage, numberofpixils;
AES aes = new AES();
@FXML
private TextField passwordtextbox;
FileReader read;


@FXML
private AnchorPane rootpane;

@Override
public void initialize(URL url, ResourceBundle rb){
}

@FXML
private void encryptpress(ActionEvent event) throws Exception {
    //byte[] converter; //sample of encrypting and decrypting
    //String output = "hello";
    //converter = output.getBytes();
    //System.out.println(output);
    //converter = aes.encrypt(output, "noooo");
    //System.out.println(converter);
    //output = aes.decrypt(converter, "noooo");
    //System.out.println(output);

    //File chooser
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Open Resource File");
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JPEG Files", "*.jpg"));
    f = fileChooser.showOpenDialog(null);    //f is the image
    //Checks if a jpg file was chosen
    if(f == null) {//if a file wasn't chosen...
        AlertBox.display("Error", "No file was chosen");
        return;
    }
    read = new FileReader();
    out = new FileWriter("passwords.txt", true);
    path = f.getAbsolutePath();//gets absolute path of file
    name = f.getName();//gets name of file
    System.out.println("Does file exist in passwords file: " + read.checkforfile(name)); //test - should output true if a file with name exists

    if( read.checkforfile(name) ){ //goes back to parent node
        if( read.hasHash(name) ){
            AlertBox.display("Error", "This filename already has a password.");
            //if true, then go back to main menu and display: "This filename already has a password."
            Parent parent = onstart.getRoot(); //Not sure if this gets the pointer to the root or just a copy of the root
            Scene parentscene = onstart.getScene();
            Stage window = (Stage) ((Node)event.getSource()).getScene().getWindow();
            window.setScene(parentscene);
            window.show();
            return;
        }
        hasname = true; //maybe close this automatically
        //skips the addname process if hasname is true
    }else { //adds the name of the image to the file if the image does not already exist

        hasname = false; // waits to write the name just incase the user exits out of the program early without choosing a password
    }

    if(hasname == false){
        out.write(name);
    }
    //System.out.println(name);
    out.write(name);

    image = ImageIO.read(f);
    if(onstart.getRoot() != null) {System.out.println("Root is clear");}
    if(onstart.getStage() != null) {System.out.println("Stage is clear");}
    if(onstart.getScene() != null) {System.out.println("Scene is clear");}
    onstart.changeScene("password.fxml");//////////////Error



    //This copies the file and places it in the same directory
    String newfile = "Encrypted" + f.getName();
    File EncryptedImage = new File(newfile);
    BufferedImage originalImage = ImageIO.read(f);
    BufferedImage newImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    widthofimage = originalImage.getWidth();
    heightofimage = originalImage.getHeight();
    numberofpixils = widthofimage * heightofimage;
    int add = 0;
    byte[] bytes = new byte[numberofpixils];
    for (int x = 0; x < originalImage.getWidth(); x++) {
        for (int y = 0; y < originalImage.getHeight(); y++) {
            bytes[add] = (byte) originalImage.getRGB(x, y);
            add++;
        }
    }
    String s = new String(bytes);
    String passwordtest = "password";
    //s = aes.encrypt(s, passwordtest); //passwordplaintext is real passowrd holder
    bytes = s.getBytes();

    add = 0;

    for (int x = 0; x < originalImage.getWidth(); x++) {
        for (int y = 0; y < originalImage.getHeight(); y++) {
            newImage.setRGB(x, y, bytes[add]);
            add++;
        }
    }
    ImageIO.write(newImage, "JPG", EncryptedImage);
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //ImageIO.write(newImage, "JPG", f2);//this creates a new image that is now encrypted
    //System.out.println(image);

}


/*@FXML
private void dgxsd(ActionEvent event){
    String test = "Hello";
    String passwordtest = "password";
    System.out.println(test);
    test = aes.encrypt(test, passwordtest); //passwordplaintext is real passowrd holder
    System.out.println(test);
    test = aes.decrypt(test, passwordtest);
    System.out.println(test);
}*/


@FXML
private void decryptpress(ActionEvent event) throws IOException {
    //if passwords.txt is not empty: AnchorPane pane = FXMLLoader.load(getClass().getResource("password2.fxml"));
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Open Resource File");
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JPEG Files", "*.jpg"));
    File f = fileChooser.showOpenDialog(null);
    if(f == null) {//if a file wasn't chosen...
        AlertBox.display("Error", "No file was chosen");
        return;
    }
    read = new FileReader();
    out = new FileWriter("passwords.txt", true);
    path = f.getAbsolutePath();//gets absolute path of file
    name = f.getName();//gets name of file
    System.out.println("Does file exist: " + read.checkforfile(name)); //test - should output true if a file with name exists
}


@FXML
private void Createpassword(ActionEvent event) throws IOException {
    passwordPlainText = passwordtextbox.getText();
    System.out.println(passwordPlainText);
    BufferedImage image = ImageIO.read(f);


   // doAESEncryption(passwordPlainText);
}
public类MainController实现可初始化{
公共字符串路径、名称;
私有字符串密码明文;
私有布尔hasname;
私有文件f;
文件编写器输出;
OnStart OnStart=新的OnStart();
缓冲图像;
int图像宽度、图像高度、像素数;
AES=新AES();
@FXML
私有文本字段密码文本框;
文件阅读器读取;
@FXML
私有锚烷根窗格;
@凌驾
公共void初始化(URL、ResourceBundle rb){
}
@FXML
private void encryptpress(ActionEvent事件)引发异常{
//byte[]converter;//加密和解密示例
//字符串输出=“你好”;
//converter=output.getBytes();
//系统输出打印项次(输出);
//converter=aes.encrypt(输出,“noooo”);
//系统输出打印LN(转换器);
//输出=aes.decrypt(转换器,“noooo”);
//系统输出打印项次(输出);
//文件选择器
FileChooser FileChooser=newfilechooser();
setTitle(“打开的资源文件”);
fileChooser.getExtensionFilters().add(新的fileChooser.ExtensionFilter(“JPEG文件”,“*.jpg”);
f=fileChooser.showOpenDialog(null);//f是图像
//检查是否选择了jpg文件
如果(f==null){//如果没有选择文件。。。
显示(“错误”,“未选择任何文件”);
返回;
}
read=新文件读取器();
out=newfilewriter(“passwords.txt”,true);
path=f.getAbsolutePath();//获取文件的绝对路径
name=f.getName();//获取文件名
System.out.println(“密码文件中是否存在文件:“+read.checkforfile(name));//如果存在名为的文件,则测试-应输出true
如果(read.checkforfile(name)){//返回到父节点
如果(读.hasHash(名称)){
显示(“错误”,“此文件名已经有密码。”);
//如果为true,则返回主菜单并显示:“此文件名已具有密码。”
Parent Parent=onstart.getRoot();//不确定这是得到指向根的指针还是根的副本
Scene parentscene=onstart.getScene();
阶段窗口=(阶段)((节点)事件.getSource()).getScene().getWindow();
window.setScene(父场景);
window.show();
返回;
}
hasname=true;//可能会自动关闭此
//如果hasname为true,则跳过addname过程
}else{//如果映像不存在,则将该映像的名称添加到文件中
hasname=false;//等待写入名称,以防用户提前退出程序而不选择密码
}
if(hasname==false){
写出(姓名);
}
//System.out.println(名称);
写出(姓名);
图像=图像读取(f);
如果(onstart.getRoot()!=null){System.out.println(“根已清除”);}
如果(onstart.getStage()!=null){System.out.println(“阶段已清除”);}
如果(onstart.getScene()!=null){System.out.println(“场景已清除”);}
onstart.changeScene(“password.fxml”);//错误
//这将复制文件并将其放置在同一目录中
String newfile=“Encrypted”+f.getName();
文件加密图像=新文件(newfile);
BuffereImage originalImage=ImageIO.read(f);
BuffereImage newImage=新的BuffereImage(originalImage.getWidth(),originalImage.getHeight(),BuffereImage.TYPE_3BYTE_BGR);
widthofimage=originalImage.getWidth();
heightofimage=originalImage.getHeight();
numberofpixils=图像的宽度*图像的高度;
int add=0;
字节[]字节=新字节[numberofpixils];
对于(int x=0;xpublic class MainController implements Initializable{

public String path, name;
private String passwordPlainText;
private boolean hasname;
private File f;
FileWriter out;
OnStart onstart = new OnStart();
BufferedImage image;
int widthofimage, heightofimage, numberofpixils;
AES aes = new AES();
@FXML
private TextField passwordtextbox;
FileReader read;


@FXML
private AnchorPane rootpane;

@Override
public void initialize(URL url, ResourceBundle rb){
}

@FXML
private void encryptpress(ActionEvent event) throws Exception {
    //byte[] converter; //sample of encrypting and decrypting
    //String output = "hello";
    //converter = output.getBytes();
    //System.out.println(output);
    //converter = aes.encrypt(output, "noooo");
    //System.out.println(converter);
    //output = aes.decrypt(converter, "noooo");
    //System.out.println(output);

    //File chooser
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Open Resource File");
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JPEG Files", "*.jpg"));
    f = fileChooser.showOpenDialog(null);    //f is the image
    //Checks if a jpg file was chosen
    if(f == null) {//if a file wasn't chosen...
        AlertBox.display("Error", "No file was chosen");
        return;
    }
    read = new FileReader();
    out = new FileWriter("passwords.txt", true);
    path = f.getAbsolutePath();//gets absolute path of file
    name = f.getName();//gets name of file
    System.out.println("Does file exist in passwords file: " + read.checkforfile(name)); //test - should output true if a file with name exists

    if( read.checkforfile(name) ){ //goes back to parent node
        if( read.hasHash(name) ){
            AlertBox.display("Error", "This filename already has a password.");
            //if true, then go back to main menu and display: "This filename already has a password."
            Parent parent = onstart.getRoot(); //Not sure if this gets the pointer to the root or just a copy of the root
            Scene parentscene = onstart.getScene();
            Stage window = (Stage) ((Node)event.getSource()).getScene().getWindow();
            window.setScene(parentscene);
            window.show();
            return;
        }
        hasname = true; //maybe close this automatically
        //skips the addname process if hasname is true
    }else { //adds the name of the image to the file if the image does not already exist

        hasname = false; // waits to write the name just incase the user exits out of the program early without choosing a password
    }

    if(hasname == false){
        out.write(name);
    }
    //System.out.println(name);
    out.write(name);

    image = ImageIO.read(f);
    if(onstart.getRoot() != null) {System.out.println("Root is clear");}
    if(onstart.getStage() != null) {System.out.println("Stage is clear");}
    if(onstart.getScene() != null) {System.out.println("Scene is clear");}
    onstart.changeScene("password.fxml");//////////////Error



    //This copies the file and places it in the same directory
    String newfile = "Encrypted" + f.getName();
    File EncryptedImage = new File(newfile);
    BufferedImage originalImage = ImageIO.read(f);
    BufferedImage newImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    widthofimage = originalImage.getWidth();
    heightofimage = originalImage.getHeight();
    numberofpixils = widthofimage * heightofimage;
    int add = 0;
    byte[] bytes = new byte[numberofpixils];
    for (int x = 0; x < originalImage.getWidth(); x++) {
        for (int y = 0; y < originalImage.getHeight(); y++) {
            bytes[add] = (byte) originalImage.getRGB(x, y);
            add++;
        }
    }
    String s = new String(bytes);
    String passwordtest = "password";
    //s = aes.encrypt(s, passwordtest); //passwordplaintext is real passowrd holder
    bytes = s.getBytes();

    add = 0;

    for (int x = 0; x < originalImage.getWidth(); x++) {
        for (int y = 0; y < originalImage.getHeight(); y++) {
            newImage.setRGB(x, y, bytes[add]);
            add++;
        }
    }
    ImageIO.write(newImage, "JPG", EncryptedImage);
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //ImageIO.write(newImage, "JPG", f2);//this creates a new image that is now encrypted
    //System.out.println(image);

}


/*@FXML
private void dgxsd(ActionEvent event){
    String test = "Hello";
    String passwordtest = "password";
    System.out.println(test);
    test = aes.encrypt(test, passwordtest); //passwordplaintext is real passowrd holder
    System.out.println(test);
    test = aes.decrypt(test, passwordtest);
    System.out.println(test);
}*/


@FXML
private void decryptpress(ActionEvent event) throws IOException {
    //if passwords.txt is not empty: AnchorPane pane = FXMLLoader.load(getClass().getResource("password2.fxml"));
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Open Resource File");
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JPEG Files", "*.jpg"));
    File f = fileChooser.showOpenDialog(null);
    if(f == null) {//if a file wasn't chosen...
        AlertBox.display("Error", "No file was chosen");
        return;
    }
    read = new FileReader();
    out = new FileWriter("passwords.txt", true);
    path = f.getAbsolutePath();//gets absolute path of file
    name = f.getName();//gets name of file
    System.out.println("Does file exist: " + read.checkforfile(name)); //test - should output true if a file with name exists
}


@FXML
private void Createpassword(ActionEvent event) throws IOException {
    passwordPlainText = passwordtextbox.getText();
    System.out.println(passwordPlainText);
    BufferedImage image = ImageIO.read(f);


   // doAESEncryption(passwordPlainText);
}
OnStart onstart = new OnStart();
public void setOnStartReference(OnStart onStartReference){
    onstart = onStartReference;
}
OnStart onstart;
MainController controller = loader.getController();
controller.setOnStartReference(this);