title: Exceptions popping up after button click(java fx)
body: <p>I am making an application where you have the option to draw a rectangle or a line, and there is also an option to clear the canvas. the clear canvas button works but I have a bunch of exceptions being displayed every time I click the clear canvas button. I am just trying to get rid of the exceptions by trying to pin down what it means</p>
  
  <p>I have tried double checking my errors within the code (any syntax or logic errors) but I still haven't pin pointed what's the problem. I am a rather new programmer so I might no have the sufficient knowledge to try to find it, I have also asked other people to take a look and they said everything seems fine though.</p>
  
  <p>Here are the errors in total after one button click (<a href="https://imgur.com/a/O46hcxr" rel="nofollow noreferrer">https://imgur.com/a/O46hcxr</a>).</p>
  
  <p>Here is the code on GitHub (<a href="https://github.com/DWER-afk/DWERR.git" rel="nofollow noreferrer">https://github.com/DWER-afk/DWERR.git</a>)</p>
  
  <p>All help is appreciated and thank you in advance for viewing it.(Just added code block and Line, Main, and rectangle are all separate classes by the way)</p>
  
  <pre><code>`import javafx.application.Application;
  import javafx.event.ActionEvent;
  import javafx.scene.Scene;
  import javafx.scene.canvas.Canvas;
  import javafx.scene.canvas.GraphicsContext;
  import javafx.scene.control.*;
  import javafx.scene.input.MouseEvent;
  import javafx.scene.layout.Pane;
  import javafx.scene.paint.Color;
  import javafx.stage.Stage;
  import org.w3c.dom.css.Rect;
  
  import java.util.ArrayList;
  
  
  
  public class Main extends Application {
  
      // Layout sizes
      int a = 0;
      final double SCREEN_WIDTH = 1000;
      final double SCREEN_HEIGHT = 800;
      final double CONTROL_HEIGHT = -175;
      private GraphicsContext gc, transgc;
      private double sx, sy;
      private double ex, ey;
      private Color lineColor = Color.BLUE;
      private ColorPicker lineColorPicker;
      private TextField tf;
      public double lines123 = 1.0;
  
      // Reference to the Model
      private ArrayList&lt;Rectangle&gt; rectangle1;
      private ArrayList&lt;Line&gt; lines;
      // TODO: Private Event Handlers and Helper Methods
  
      private void pressHandler(MouseEvent me) {
          sx = me.getX();
          sy = me.getY();
      }
  
      private void releaseHandler(MouseEvent me) {
          ex = me.getX();
          ey = me.getY();
          if(a == 0){Line l = new Line(sx,sy,ex,ey,lineColorPicker.getValue(),lines123);
              lines.add(l);
              for (Line l1 : lines)
                  l1.draw(gc);
              transgc.clearRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);}
          if(a == 1){Rectangle rl = new Rectangle(sx,sy,ex,ey,lineColorPicker.getValue(),lines123);
              rectangle1.add(rl);
  
              for (Rectangle r1 : rectangle1)
                  r1.draw(gc);
              transgc.clearRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);}
  
  
  
      }
  
  
      /**
       * This will draw a "Rubberband" line on the transparent surface above the drawing.
       *
       * @param me The mouse drag event - not used in method
       */
      private void dragHandler(MouseEvent me) {
          transgc.clearRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
          ex = me.getX();
          ey = me.getY();
          if(a == 0){
              Line tempLine = new Line(sx,sy,ex,ey,lineColorPicker.getValue(),lines123);
              tempLine.draw(transgc);
          }
          if (a == 1){
              Rectangle temprect = new Rectangle(sx,sy,ex,ey,lineColorPicker.getValue(),lines123);
              temprect.draw(transgc);
          }
  
      }
  
      /**
       * This is where you create your components and the model and add event
       * handlers.
       *
       * @param stage The main stage
       * @throws Exception
       */
      @Override
      public void start(Stage stage) throws Exception {
          Pane root = new Pane();
          Scene scene = new Scene(root, SCREEN_WIDTH, SCREEN_HEIGHT, Color.DARKGRAY); // set the size here
          stage.setTitle("Java Shape Drawer by Alexei Ougriniouk"); // set the window title here
          stage.setScene(scene);
          // TODO: Add your GUI-building code here
  
          // 1. Create the model - No model yet
  
          lines = new ArrayList&lt;&gt;();
          rectangle1 = new ArrayList&lt;&gt;();
          // 2. Create the GUI components - Just a CANVAS for now
  
          Canvas c = new Canvas(SCREEN_WIDTH, SCREEN_HEIGHT - CONTROL_HEIGHT);
          c.relocate(0, CONTROL_HEIGHT);
          Canvas transc = new Canvas(SCREEN_WIDTH, SCREEN_HEIGHT);
          transc.relocate(0, CONTROL_HEIGHT);
  
          // Add JavaFX controls to top pane ...
  
          Label colorLabel = new Label("Change line Colours");
          colorLabel.relocate(100, 625);
  
          lineColorPicker = new ColorPicker(lineColor);
          lineColorPicker.relocate(100, 650);
  
          Button resetButton = new Button("Reset Shape Colors");
          resetButton.relocate(100,750);
          resetButton.setOnAction(this::resetHandler);
  
          Button clearstuff =  new Button("Clear Canvas");
          clearstuff.relocate(450,675);
          clearstuff.setOnAction(this::clearHandler);
  
          Button linedraw =  new Button("line");
          linedraw.relocate(350,635);
          linedraw.setDefaultButton(true);
          linedraw.setOnAction(this::lineHandler);
  
          Button rectdraw =  new Button("rectangle");
          rectdraw.relocate(450,635);
          rectdraw.setOnAction(this::rectHandler);
  
          tf = new TextField("1");
          tf.relocate(250,750);
  
          // 3. Add components to the root
          root.getChildren().addAll(c, transc, colorLabel, lineColorPicker, resetButton, tf,clearstuff,linedraw,rectdraw);
  
          // Create the two graphics contexts
  
          gc = c.getGraphicsContext2D();
          transgc = transc.getGraphicsContext2D();
  
          gc.setFill(Color.WHITE);
          gc.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
          // 5. Add Event Handlers and do final setup
          transc.addEventHandler(MouseEvent.MOUSE_PRESSED, this::pressHandler);
          transc.addEventHandler(MouseEvent.MOUSE_RELEASED, this::releaseHandler);
          transc.addEventHandler(MouseEvent.MOUSE_DRAGGED, this::dragHandler);
          // 6. Show the stage
          stage.show();
      }
      private void clearHandler(ActionEvent actionEvent){
          gc.clearRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
          for(Line l : lines) {
              l.setLineColor(lineColorPicker.getValue());
              lines.clear();
          }
          for(Rectangle rl : rectangle1 ) {
              rl.setLineColor(lineColorPicker.getValue());
              rectangle1.clear();
          }
  
      }
  
  
      private void resetHandler(ActionEvent actionEvent) {
          for(Line l : lines) {
              l.setLineColor(lineColorPicker.getValue());
              l.draw(gc);
          }
          for(Rectangle l : rectangle1) {
              l.setLineColor(lineColorPicker.getValue());
              l.draw(gc);
          }
  
  
  
          try {
              int lineWidth = Integer.parseInt(tf.getText());
              if(lineWidth &gt; 0){
                  new Alert(Alert.AlertType.INFORMATION,
                          "VALUE APPROVED : Value in TextBox = " + String.valueOf(lineWidth)).showAndWait() ;
                  lines123 = (double) lineWidth;
              }
              if(lineWidth &lt;= 0){
                  new Alert(Alert.AlertType.ERROR,
                          "Could not convert " + tf.getText() + " to an integer due to it being negative").showAndWait();
              }
  
          }
          catch (NumberFormatException ex)
          {
              new Alert(Alert.AlertType.ERROR,
                      "Could not convert " + tf.getText() + " to an integer").showAndWait();
  
          }
  
      }
      private void lineHandler(ActionEvent actionEvent) {
          a = 0;
          if(a == 0){for(Line l : lines) {
              l.setLineColor(lineColorPicker.getValue());
              l.draw(gc);
          }}
  
  
      }
      private void rectHandler(ActionEvent actionEvent) {
          a = 1;
          if(a == 1){for(Rectangle l : rectangle1) {
              l.setLineColor(lineColorPicker.getValue());
              l.draw(gc);
          }}
      }
  
      /**
       * Make no changes here.
       * @param args unused
       */
      public static void main(String[] args) {
          launch(args);
      }
  }
  '
  </code></pre>
  
  <p>This is the Line class</p>
  
  <pre><code>public class Line {
      Main sc = new Main();
      private double sx, sy, ex, ey;
      private Color lineColor;
      double lineswidth = sc.lines123;
      public Line(double sx, double sy, double ex, double ey, Color lineColor, double lineswidth)
      {
          this.sx = sx;
          this.sy = sy;
          this.ex = ex;
          this.ey = ey;
          this.lineColor = lineColor;
          this.lineswidth = lineswidth;
      }
  
      /**
       * Draw a rectangle to the Graphics context
       * @param gc the graphics context of the FX component
       */
      public void draw(GraphicsContext gc) {
          gc.setStroke(lineColor);
          gc.setLineWidth(lineswidth);
          // For circle drawing must get the smaller of the two X values, same for Y
          gc.strokeLine (sx,sy,ex,ey);
      }
  
      public void setLineColor(Color newColor)
      {
          lineColor = newColor;
      }
  
  
      @Override
      public String toString() {
          return "Line{}" + super.toString();
      }
  
  }
  </code></pre>
  
  <p>This is the rectangle class</p>
  
  <pre><code>import javafx.scene.canvas.GraphicsContext;
  import javafx.scene.paint.Color;
  
  public class Rectangle {
      Main sc = new Main();
      private double sx, sy, ex, ey;
      private Color lineColor;
      double lineswidth = sc.lines123;
      public Rectangle(double sx, double sy, double ex, double ey, Color lineColor, double lineswidth)
      {
          this.sx = sx;
          this.sy = sy;
          this.ex = ex;
          this.ey = ey;
          this.lineColor = lineColor;
          this.lineswidth = lineswidth;
      }
  
      /**
       * Draw a rectangle to the Graphics context
       * @param gc the graphics context of the FX component
       */
      public void draw(GraphicsContext gc) {
          {
              gc.setStroke(Color.BLACK);
              gc.strokeRect(sx,sy,ex,ey);
              gc.setLineWidth(lineswidth);
              // For circle drawing must get the smaller of the two X values, same for Y
              gc.setFill(lineColor);
              gc.fillRect(sx,sy,ex,ey);
          }
  
      }
  
      public void setLineColor(Color newColor)
      {
          lineColor = newColor;
      }
  
  
      @Override
      public String toString() {
          return "Line{}" + super.toString();
      }
  
  }
  </code></pre>