Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+15 votes

When I try to use the command on the canvas I am unable to use it, tried using loads of imports and still no use, any suggestions?

asked in CSC305 Fall 2022 by (1 point)

1 Answer

+7 votes

I bet you were getting NullPointerExceptions? (Please note that it I'd helpful to provide your exact error message when posting a question!)

Assuming that is so, the most likely problem is that you need to wait until after the Canvas object has been created and assigned to whatever @FXML instance variable field you used for it, before you can call the method to get the GraphicsContext2D object for it.

Specifically, you'll want to get the graphics from the canvas in the special

@FXML
private void initialize() { ...

method.

Another possible mistake would be not having the Canvas's fxid exactly match the name of the @FXML field, which could also leave the Canvas object null, even within the initialize method.

answered by (3.5k points)
...