0

Sorry for the weird title, didn't knew what exactly to write. I am working on a JavaFX application with Java8. For this purpose, I have the javafx-maven-plugin from com.zenjava. I have a simple code for testing, where there is a Main class, and one css page for styling. I have the same project without maven, and it works fine. But with Maven I am unable to get it running. I get an NPE for the CSS file-. I have tried multiple options already. Any help would be nice. Thank you.

Error log :

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at View.Main.start(Main.java:110)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
    ... 1 more

POM.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>MavenFX</groupId>
    <artifactId>MavenFX</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>

    </dependencies>

    <build>
        <plugins>
      <plugin>

          <groupId>com.zenjava</groupId>
          <artifactId>javafx-maven-plugin</artifactId>
          <version>8.2.0</version>
          <configuration>
              <mainClass>View.Main</mainClass>
          </configuration>
      </plugin>
        </plugins>
    </build>
</project>

Main class :

package View;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Reflection;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {


    String user = "JavaFX2";
    String pw = "password";
    String checkUser, checkPw;


    @Override
    public void start(Stage primaryStage) throws Exception{
        primaryStage.setTitle("Test Login");

        BorderPane bp = new BorderPane();
        bp.setPadding(new Insets(10,50,50,50));

        //Adding HBox
        HBox hb = new HBox();
        hb.setPadding(new Insets(20,20,20,30));

        //Adding GridPane
        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(20,20,20,20));
        gridPane.setHgap(5);
        gridPane.setVgap(5);

        //Implementing Nodes for GridPane
        Label lblUserName = new Label("Username");
        final TextField txtUserName = new TextField();
        Label lblPassword = new Label("Password");
        final PasswordField pf = new PasswordField();
        Button btnLogin = new Button("Login");
        final Label lblMessage = new Label();

        //Adding Nodes to GridPane layout
        gridPane.add(lblUserName, 0, 0);
        gridPane.add(txtUserName, 1, 0);
        gridPane.add(lblPassword, 0, 1);
        gridPane.add(pf, 1, 1);
        gridPane.add(btnLogin, 2, 1);
        gridPane.add(lblMessage, 1, 2);


        //Reflection for gridPane
        Reflection r = new Reflection();
        r.setFraction(0.7f);
        gridPane.setEffect(r);

        //DropShadow effect
        DropShadow dropShadow = new DropShadow();
        dropShadow.setOffsetX(5);
        dropShadow.setOffsetY(5);

        //Adding text and DropShadow effect to it
        Text text = new Text("Test Login");
        text.setFont(Font.font("Courier New", FontWeight.BOLD, 28));
        text.setEffect(dropShadow);

        //Adding text to HBox
        hb.getChildren().add(text);

        //Add ID's to Nodes
        bp.setId("bp");
        gridPane.setId("root");
        btnLogin.setId("btnLogin");
        text.setId("text");

        //Action for btnLogin
        btnLogin.setOnAction(event -> {
            checkUser = txtUserName.getText();
            checkPw = pf.getText();
            if(checkUser.equals(user) && checkPw.equals(pw)){
                lblMessage.setText("Congratulations!");
                lblMessage.setTextFill(Color.GREEN);
            }
            else{
                lblMessage.setText("Incorrect user or pw.");
                lblMessage.setTextFill(Color.RED);
            }
            txtUserName.setText("");
            pf.setText("");
        });

        //Add HBox and GridPane layout to BorderPane Layout
        bp.setTop(hb);
        bp.setCenter(gridPane);

        //Adding BorderPane to the scene and loading CSS
        Scene scene = new Scene(bp);

        scene.getStylesheets().add(getClass().getClassLoader().getResource("UI/login.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setTitle("Test Login");
        /*primaryStage.titleProperty().bind(
                scene.widthProperty().asString().
                        concat(" : ").
                        concat(scene.heightProperty().asString()));*/
        primaryStage.setResizable(false);
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

CSS file :

#root {
    -fx-background-color:  linear-gradient(lightgray, gray);
    -fx-border-color: white;
    -fx-border-radius: 20;
    -fx-padding: 10 10 10 10;
    -fx-background-radius: 20;
}

#bp {
    -fx-background-color:  linear-gradient(gray,DimGrey );

}

#btnLogin {
    -fx-background-radius: 30, 30, 29, 28;
    -fx-padding: 3px 10px 3px 10px;
    -fx-background-color: linear-gradient(orange, orangered );
}

#text {
    -fx-fill:  linear-gradient(orange , orangered);
}

If I run mvn jfx:jar, I get the jar file, but it also has the same problem. Why is that if it's a Maven project, JavaFX has problem finding files. ANy help would be nice. Thank you.

1 Answer 1

1

You can try to output the path to your resources file:

       System.err.println(getClass().getClassLoader().getResource("UI/login.css"));

and to see where it points. It is possible that maven changed your resource location. I am not sure, but you can try. Please let me know.

Sign up to request clarification or add additional context in comments.

2 Comments

Your code directly doesn't work. I had to toString it. After that, it's just printing true. How does that help?
You were right, maven moved it. I simply moved the file to resources and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.