NullPointerException problem
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:
Code java:
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.
Re: NullPointerException problem
More likely than not, variables are getting hidden in your example. When you extend another class, the public variables and methods from the parent class are inherited, I believe the variable being static doesn't change that habit. So in your example, you should be able to do away with the public static Graphics pa; variable in the CountDown class and referencing a pa variable will still be fine.
Where exactly are you getting the null pointer exception? Also, why is your Graphics pa object static?
Re: NullPointerException problem
Below are some references for how to paint using Java2D:
Learning Java 2D, Part 1
Trail: 2D Graphics (The Java™ Tutorials)
I point you to these because you are painting in a very strange manner.
1) use a javax.swing.Timer, not a java.util.Timer. The reasons for this revolve around the single threaded Swing model.
2) If you are using Swing (eg Components that begin with the letter J), override paintComponent rather than paint, and call the super method regardlenss of which method you override
3) Don't keep references to a Graphics object. You have no way of controlling what happens to these during the course of your program.
4) You never know when paint (or paintComponent) will be called, and you are constructing a Timer every time paint is called. If you truly wish this to occur every second, then start a repeating timer to fire every second, and call repaint() rather than painting directly to a Graphics object (which may or may not still reference the current Graphics context)
This may not cover everything, but I do recommend considering the above links and each of the points above.
Re: NullPointerException problem
The exception occurs here:
Code java:
public void run()
{
pa.fillOval(200,200,50,50);
}
I made Graphics pa object static to get rid of an "unable to reference a nonstatic variable to a static method" error. I doubt that I was actually supposed to do that, but it got me around the error so. :/
Re: NullPointerException problem
I assume it complained about that because CountDown is a static class. Forget my previous comment, I misread your code and thought CountDown was extending TimerTest (it is actually extending TimerTask).
I assume you get this error because the run() method is called before the paint() method. Try calling repaint() before the pa.fillOval() call. You should also include the @Override tag whenever you are overriding an inherited method. I don't believe it will make the program do anything extra, but it makes it more readable at the very least.
Re: NullPointerException problem
Okay, I changed the
Code jave:
pa.fillOval(100,100,50,50);
to just repaint(). This got rid of my NullPointException error but now, the initial circle is drawn and nothing else happens.
This is what my code looks like now:
Code java:
import java.util.Timer;
import java.util.TimerTask;
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 class CountDown extends TimerTask
{
public Graphics pa;
public int x, y, width, height;
public void paintComponent (Graphics page)
{
x = 100;
y = 100;
width = 50;
height = 50;
pa = page;
pa.setColor(Color.red);
pa.fillOval(x,y,width,height);
}
public void run()
{
x+=50;
y+=50;
repaint();
}
}
}
Re: NullPointerException problem
call pa.fillOval(100,100,50,50) after the repaint() call in your run method.
Also, word of advice, you most likely want to call super.paintComponent(page); as the first thing in your paintComponent() method.