I'm creating function which draws a rect on the screen.
The code is below in
What I have tried
My Error
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.awt.Graphics.fillRect(int, int, int, int)" because the return value of "javax.swing.JPanel.getGraphics()" is null
at myCanvas.fillRect(myCanvas.java:57)
at Main.main(Main.java:15)
For some reason the `super.getGraphics()` is returning null, and I'm unable to fix it.
Similar type of usage of graphics like this, I saw in this [website][1] in example program 2.
How should I fix this error?
What I have tried:
public class myCanvas extends JPanel{
public void fillRect(int w, int h){
super.getGraphics().fillRect(penX,penY, w, h);
}
}