1

I am trying to create a painting app using JavaFX and it crashes whenever I run it with a controller, however it loads the GUI without the controller linked, I am confused as to what is causing this

Here is my code:

Paint package paint;

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


public class Paint extends Application {

public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("PaintScreen.fxml"));
    stage.setTitle("Profile Picture Creator");
    stage.setScene(new Scene(root));
    stage.show();
}

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

}

Edit: PaintController

   package paint;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class PaintController implements Initializable{

private ColorPicker colorPicker;

private Canvas canvas;

private TextField brushThickness;

boolean brushButton = false;

GraphicsContext drawTool;

public void initialize(URL url, ResourceBundle rb) {

    drawTool = canvas.getGraphicsContext2D();
    canvas.setOnMouseDragged(e -> {
        double size = Double.parseDouble(brushThickness.getText());
        double x = e.getX() - size / 2;
        double y = e.getY() - size / 2;

        if (brushButton && !brushThickness.getText().isEmpty()) {
            drawTool.setFill(colorPicker.getValue());
            drawTool.fillRoundRect(x, y, size, size, size, size);
        }
    });
}

public void brushbutton(ActionEvent e) {
    brushButton = true;
}

}

PaintScreen (fxml file)

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ColorPicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<AnchorPane xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="paint.PaintController">
   <children>
      <BorderPane layoutX="-4.0" layoutY="-4.0" prefHeight="600.0" prefWidth="800.0">
         <top>
            <VBox prefHeight="71.0" prefWidth="800.0" BorderPane.alignment="CENTER">
               <children>
                  <HBox prefHeight="100.0" prefWidth="200.0">
                     <children>
                        <VBox prefHeight="200.0" prefWidth="100.0">
                           <children>
                              <Label text="Activate Brush!!!">
                                 <VBox.margin>
                                    <Insets left="10.0" top="10.0" />
                                 </VBox.margin>
                              </Label>
                              <Button mnemonicParsing="false" onAction="#brushbutton" text="Brush">
                                 <VBox.margin>
                                    <Insets left="30.0" top="10.0" />
                                 </VBox.margin>
                              </Button>
                           </children>
                           <HBox.margin>
                              <Insets left="10.0" />
                           </HBox.margin>
                        </VBox>
                        <VBox prefHeight="200.0" prefWidth="100.0">
                           <children>
                              <Label text="Choose A Colour">
                                 <VBox.margin>
                                    <Insets left="5.0" top="10.0" />
                                 </VBox.margin>
                              </Label>
                              <ColorPicker fx:id="colorPicker">
                                 <VBox.margin>
                                    <Insets top="10.0" />
                                 </VBox.margin>
                              </ColorPicker>
                           </children>
                           <HBox.margin>
                              <Insets left="10.0" />
                           </HBox.margin>
                        </VBox>
                        <VBox prefHeight="200.0" prefWidth="100.0">
                           <children>
                              <Label text="Brush Thickness">
                                 <VBox.margin>
                                    <Insets left="10.0" top="10.0" />
                                 </VBox.margin>
                              </Label>
                              <TextField fx:id="brushThickness" promptText="Enter a number...">
                                 <VBox.margin>
                                    <Insets top="10.0" />
                                 </VBox.margin>
                              </TextField>
                           </children>
                           <HBox.margin>
                              <Insets left="10.0" />
                           </HBox.margin>
                        </VBox>
                        <VBox prefHeight="200.0" prefWidth="100.0">
                           <children>
                              <Button fx:id="clearPicture" mnemonicParsing="false" text="Clear">
                                 <VBox.margin>
                                    <Insets left="15.0" top="25.0" />
                                 </VBox.margin>
                                 <padding>
                                    <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
                                 </padding>
                              </Button>
                           </children>
                           <HBox.margin>
                              <Insets left="10.0" />
                           </HBox.margin>
                        </VBox>
                        <Button fx:id="savePicture" mnemonicParsing="false" text="Save">
                           <HBox.margin>
                              <Insets left="250.0" top="25.0" />
                           </HBox.margin>
                           <padding>
                               <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
                           </padding>
                        </Button>
                     </children>
                  </HBox>
               </children>
            </VBox>
         </top>
         <center>
            <Canvas fx:id="canvas" height="520.0" width="770.0" BorderPane.alignment="CENTER" />
         </center>
      </BorderPane>
   </children>
</AnchorPane>

Here is the error I get when running with the controller:

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: 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$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException: 
/C:/Users/c_tho/workspace2.0/Paint/bin/paint/PaintScreen.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at paint.Paint.start(Paint.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Caused by: java.lang.NullPointerException
at paint.PaintController.initialize(PaintController.java:28)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more
Exception running application paint.Paint

0 Answers0