How to solve java.lang.IllegalAccessException (JavaFX) on Eclipse
I try to tidy up the file for my app by creating seperate packages according to the functionality and encounter this error:
java.lang.IllegalAccessException: class javafx.fxml.FXMLLoader$ValueElement (in module javafx.fxml) cannot access class application.login.Login (in module eSentral_Desktop_App) because module eSentral_Desktop_App does not export application.login to module javafx.fxml
My question is how to fix this and what cause it to happen?
Here are some extra Information:
This is my class hierarchy:
this is my Main.java code:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import jfxtras.styles.jmetro.JMetro;
import jfxtras.styles.jmetro.Style;
import javafx.scene.Parent;
import javafx.scene.Scene;
import java.io.IOException;
import application.login.Users;
import application.listing.Homepage;
public class Main extends Application{
public static Object currentUser = new Object();
double x,y = 0;
private static Stage stg;
@Override
public void start(Stage primaryStage) throws Exception{
try {
stg = primaryStage;
Parent root = FXMLLoader.load(getClass().getResource("login/LoginUI.fxml"));
Scene scene = new Scene(root);
JMetro jMetro = new JMetro(scene, Style.LIGHT);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public void changeScene(String fxml) throws IOException {
Parent pane = FXMLLoader.load(getClass().getResource(fxml));
stg.getScene().setRoot(pane);
}
public static void main(String[] args) {
launch(args);
}
}
This is my LoginUI.fxml code:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Font?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="700.0" prefWidth="800.1" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.login.Login">
<top>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="115.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<children>
<ImageView fitHeight="80.0" fitWidth="249.0" layoutX="276.0" layoutY="18.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../resources/ic_eSentral_Logo.png" />
</image>
</ImageView>
</children>
</AnchorPane>
</top>
<bottom>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="133.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="btnRegister" layoutX="248.0" layoutY="63.0" mnemonicParsing="false" onMouseEntered="#MouseEnter" onMouseExited="#MouseExit" prefHeight="44.0" prefWidth="318.0" style="-fx-background-color: #169fe3;" text="Register a new account" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Label alignment="CENTER" layoutX="311.0" layoutY="24.0" text="Don't have an account yet?" textFill="#1286bc">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
</children>
</AnchorPane>
</bottom>
<center>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="487.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<children>
<Label fx:id="lblSuccess" alignment="CENTER" layoutX="247.0" layoutY="2.0" prefHeight="24.0" prefWidth="318.0" textFill="#00ff26">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
<TextField fx:id="txtUsername" layoutX="247.0" layoutY="77.0" prefHeight="44.0" prefWidth="318.0" promptText="Email">
<font>
<Font size="15.0" />
</font>
</TextField>
<Label fx:id="lblEmail" alignment="CENTER" layoutX="247.0" layoutY="121.0" prefHeight="24.0" prefWidth="318.0" textFill="#ec1e1e">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
<PasswordField fx:id="txtPassword" layoutX="247.0" layoutY="145.0" prefHeight="44.0" prefWidth="318.0" promptText="Password">
<font>
<Font size="15.0" />
</font>
</PasswordField>
<Label fx:id="lblPassword" alignment="CENTER" layoutX="247.0" layoutY="189.0" prefHeight="24.0" prefWidth="318.0" textFill="#ec1e1e">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
<Label fx:id="lblForgot" alignment="CENTER" layoutX="326.0" layoutY="226.0" onMouseClicked="#forgetPassword" onMouseEntered="#MouseEnter" onMouseExited="#MouseExit" text="Forget your password? " textFill="#1286bc" underline="true">
<font>
<Font size="15.0" />
</font>
</Label>
<Label fx:id="lblForgot1" alignment="CENTER" layoutX="380.0" layoutY="35.0" text="Login" textFill="#1286bc">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Label>
<Button fx:id="btnLogin" layoutX="247.0" layoutY="274.0" mnemonicParsing="false" onAction="#login" onMouseEntered="#MouseEnter" onMouseExited="#MouseExit" prefHeight="44.0" prefWidth="318.0" style="-fx-background-color: #169fe3;" text="Log in" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button fx:id="btnFB" alignment="CENTER" layoutX="247.0" layoutY="380.0" mnemonicParsing="false" onMouseEntered="#MouseEnter" onMouseExited="#MouseExit" prefHeight="44.0" prefWidth="318.0" style="-fx-background-color: #4766a9;" text="Log in with Facebook" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
<graphic>
<ImageView fitHeight="35.0" fitWidth="63.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../resources/ic_facebook.png" />
</image>
</ImageView>
</graphic>
</Button>
<Separator layoutX="247.0" layoutY="338.0" prefHeight="28.0" prefWidth="130.0" />
<Separator layoutX="434.0" layoutY="338.0" prefHeight="28.0" prefWidth="130.0" />
<Label layoutX="392.0" layoutY="337.0" text="OR" textFill="#1286bc">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
</center>
</BorderPane>
This is module-info code:
module eSentral_Desktop_App {
requires javafx.controls;
requires java.net.http;
requires javafx.fxml;
requires javafx.base;
requires javafx.graphics;
requires com.google.gson;
requires java.logging;
requires java.desktop;
requires org.jfxtras.styles.jmetro;
opens application to javafx.graphics, javafx.fxml;
}
Thank you to jewelsea, this problem is solve. All I need to do is add
opens application.login to javafx.fxml;
into module-info.java.
It will look like this:
module eSentral_Desktop_App {
requires javafx.controls;
requires java.net.http;
requires javafx.fxml;
requires javafx.base;
requires javafx.graphics;
requires com.google.gson;
requires java.logging;
requires java.desktop;
requires org.jfxtras.styles.jmetro;
opens application.login to javafx.fxml;
opens application to javafx.graphics, javafx.fxml;
}