0

I'm trying to draw a circle to the jframe but am getting an error. What is wrong with my code?

   import java.awt.Graphics;
      import java.awt.Color;
    import javax.swing.JFrame;

   public class Racing {
  public static void main (String[]args) {
 Racing r = new Racing();
r.graphics(null);

}
 public void graphics(Graphics g) {
g.setColor(Color.GREEN);
g.drawOval(480,480,200,200);

 }
 public Racing(){
 JFrame frame = new JFrame("Infinite race");
 frame.setVisible(true);
  frame.setLocationRelativeTo(null);
 frame.setSize(960, 960);

}
  }
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • 1
    You're obviously making wild guesses. Don't do that -- read the tutorials. Start here: [Painting with Swing](https://docs.oracle.com/javase/tutorial/uiswing/painting/). Also 1) don't draw in the JFrame itself but instead in a JPanel. 2) Draw in the JPanel's paintComponent method. 3) Search this site for examples of Swing graphics, 4) and the tutorials as I've linked to above. Question closed. – Hovercraft Full Of Eels Nov 08 '15 at 22:43
  • Please read [ask] and [edit] your question accordingly. – Felix Kling Nov 08 '15 at 22:44
  • Exception in thread "main" java.lang.NullPointerException at Racing.graphics(Racing.java:12) at Racing.main(Racing.java:8) -im new to coding – Picklejar 75 Nov 08 '15 at 22:45
  • You're getting a NullPointerException, a NPE, because you're passing in a null reference here, `r.graphics(null);` and then within the method trying to call a method off of that null. The tutorials will show you the correct way to do all of this. Luck. – Hovercraft Full Of Eels Nov 08 '15 at 23:29

0 Answers0