Hello!
I'm trying to make a program where the ball moves to the right of the screen but my program compiles correctly. It's when I run it is when this pops up:
Exception in thread "main" java.lang.NullPoinerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at FirstGame.<init><FirstGame.java:18>
at FirstGame.main<FirstGame.java:27>
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; import java.util.concurrent.*; //Start class FirstGame extends JFrame { private PaintSurface canvas; public FirstGame() { JFrame frame = new JFrame(); frame.setSize(800, 600); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(canvas,BorderLayout.CENTER); ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3); executor.scheduleAtFixedRate(new paintSpriteThread(frame), 0L, 20L, TimeUnit.MILLISECONDS); } public static void main(String[] args) { new FirstGame(); } } class paintSpriteThread implements Runnable { JFrame c; public paintSpriteThread(JFrame c) { this.c = c; } public void run() { c.repaint(); } } class PaintSurface extends JComponent { int x = 400; int y = 300; int d = 20; public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); Shape ball = new Ellipse2D.Float(x,y,d,d); x+=1; g2.setColor(Color.RED); g2.fill(ball); } }
Welp, with every error I will learn something new!
Thank you to all who reply with helpfullness!
lil_misfit