0

I found this piece of code, but it won't run a new blank window and keep getting NullPointerException error. p.s. I'm new to programming. Any help would be appreciated thanks.

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        BorderPane root = new BorderPane();

        try {

            Scene scene = new Scene(root,640,480);
            scene.getStylesheets().add(getClass().getResource("/application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();

        } catch(Exception e) {
            e.printStackTrace();
        }

        root.setCenter(new RootLayout());
    }
    public static void main(String[] args) {
        launch(args);
    }
}

Justin
  • 3
  • 3
  • in which line do you get the exception? And what is the corresponding text? – bautista Jun 14 '19 at 12:01
  • It must be at getResource(), you will get the null pointer exception. – Dushyant Tankariya Jun 14 '19 at 12:02
  • You can study this post for NullPointerException error. https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Amin Alizadeh Jun 14 '19 at 12:02
  • line: scene.getStylesheets().add(getClass().getResource("/application.css").toExternalForm()); – Justin Jun 14 '19 at 12:03
  • I have the application.css in the same project but different directory folder so its not within the src folder – Justin Jun 14 '19 at 12:04
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – George Z. Jun 14 '19 at 12:04
  • When you're getting an exception and you want help debugging it you should include the entire [stack trace](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) in the question (typically formatted as a code block). It also helps if you indicate which line is throwing the error in your [mre] (e.g. via a code comment). You can add this information via an [edit]. – Slaw Jun 14 '19 at 12:04
  • @Justin which means you are not able to get application.css from your resource directory. You can add stylesheet by writting directly, `scene.getStylesheets().add("application.css");` – Dushyant Tankariya Jun 14 '19 at 12:04
  • If `getClass().getResource(...)` is returning `null` then that means it can't find the resource using the given path. Either the path is wrong or the resource isn't on the classpath at runtime. Please show your project structure. And are you using any build tools (e.g. Maven, Gradle, etc.)? – Slaw Jun 14 '19 at 12:06
  • Possible duplicate of [Scene.getStylesheets().add() not working inside jar file](https://stackoverflow.com/questions/16287053/scene-getstylesheets-add-not-working-inside-jar-file) – Dushyant Tankariya Jun 14 '19 at 12:06
  • @DushyantTankariya so do i write "resources/application.css"? I've tried this but still get error – Justin Jun 14 '19 at 12:09
  • I've updated my last two comments see the changes are `scene.getStylesheets().add("application.css");` And the question is possible duplicate of [getStyleSheets().add()](https://stackoverflow.com/questions/16287053/scene-getstylesheets-add-not-working-inside-jar-file) – Dushyant Tankariya Jun 14 '19 at 12:11
  • @Slaw im using Intellij – Justin Jun 14 '19 at 12:13
  • @DushyantTankariya I've tried that but still doesn't work – Justin Jun 14 '19 at 12:13
  • Have you changed your CSS location too? And please update your question and add some directory structure too so we can identify where your CSV is, and how you can resolve your problem. – Dushyant Tankariya Jun 14 '19 at 12:15
  • Yeah I moved it to the same folder where main.java is – Justin Jun 14 '19 at 12:16
  • Note the use of an IDE does not preclude the use of a build tool. However, if you're using IntelliJ by itself, is your resources directory marked as such? – Slaw Jun 14 '19 at 12:20
  • This is a little bit too much but i'm trying to work this guys code by copying the codes from resources and application folder https://github.com/joelgraff/java_fx_node_link_demo/tree/master/Drag_demo_pts_1_to_3/DragDemo – Justin Jun 14 '19 at 12:23
  • It's just a little query and me messing around with javafx so you don't have to answer. I didn't think it would turn this big of a deal and don't want to waste anyone's time anymore. – Justin Jun 14 '19 at 12:26

1 Answers1

0

I had the same common problem when I started with JavaFX but I can explain It, It throws null pointer exception because it is not able to find your CSS file from the specified location.

I've found that you are getting nullpointer exception at the below line, scene.getStylesheets().add(getClass().getResource("/application.css").toExternalForm());

There is also another way to add your CSS file to scene

1) scene.getStylesheets().add("application.css");

2) scene.getStylesheets().add(this.getClass().getResource("/application.css").toString());

3) Package should be inside src directory and css also should be in src directory.
scene.getStylesheets().add(<packageName>.<ClassName>.class.getResource("/application.css").toExtern‌​alForm());
Dushyant Tankariya
  • 1,432
  • 3
  • 11
  • 17