I just recently learned how to incorporate time into my programs. Specifically using the Timer and TimerTask classes to make a specific action occur after a certain amount of times, and repeat every amount of time. I first tried it, and just made a simple program that counted to 10 then exited. That worked fine. Now my goal is to create an applet that draws red circles on the screen repetitively. Here is my code:
import java.util.Timer; import java.util.TimerTask; import java.util.Date; import javax.swing.*; import java.awt.*; public class TimerTest extends JApplet { public static Graphics pa; public void paint(Graphics page) { pa = page; pa.setColor(Color.red); pa.fillOval(100,100,50,50); Timer timer = new Timer(); timer.schedule(new CountDown(), 1000); } private static class CountDown extends TimerTask { public static Graphics pa; public void paint(Graphics page) { pa = page; pa.setColor(Color.red); pa.fillOval(100,100,50,50); } public void run() { pa.fillOval(200,200,50,50); } } }
This generates a NullPointerException error at runtime. I have done some research on this exception and discovered that this occurs when I have a variable that points to nothing. I can not see what is causing this error. Note: I know that thus far this code will only draw 2 total circles. I plan to adjust the code later, for now I just want to make sure that at least one circle (aside from the first one) will appear.