Hey there guys, this is my first post here. Really pretty website I must say. I guess you'd expect a programming website to be well built. Haha.
But anyways, I've been having some problems recently with a JFrame. I'm a beginner, but I usually do pretty well with resolving errors. This one though, is giving me problems. I've been trying for a couple hours to get it to work, to no avail. I'm not one to think down of getting help for others, so what do you guys think? Can you help me?
Here's my code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Timer;
public class WindowProperties extends JFrame implements ActionListener, KeyListener {
	public static int velX = 0;
	public static int velY = 0;
	public static int x = 5;
	public static int y = 5;
	public static boolean velTimer = true;
	public static boolean draw = false;
	public WindowProperties() {
	public void setupWindow() {
		Timer velLoop = new Timer();
		//while(velTimer == true) 
		velLoop.schedule(new VelTask(), 1, 100);
		JFrame frame = new JFrame();
		Graphics g = frame.getGraphics();
		if(draw == true) updatePlayer(g);
	public static void updatePlayer(Graphics g) {
		g.fillOval(x, y, 1, 1);
		draw = false;
	public void actionPerformed(ActionEvent e) {
	public void keyPressed(KeyEvent e) {
		int code = e.getKeyCode();
		if(code == KeyEvent.VK_W || code == KeyEvent.VK_UP) up();
		if(code == KeyEvent.VK_S || code == KeyEvent.VK_DOWN) down();
		if(code == KeyEvent.VK_A || code == KeyEvent.VK_LEFT) left();
		if(code == KeyEvent.VK_D || code == KeyEvent.VK_RIGHT) right();
	public void keyReleased(KeyEvent e) {}
	public void keyTyped(KeyEvent e) {}
	public static void up() {
		velX = 0;
		velY = -1;
	public static void down() {
		velX = 0;
		velY = 1;
	public static void left() {
		velX = -1;
		velY = 0;
	public static void right() {
		velX = 1;
		velY = 0;
	public void paint(Graphics g) {
	   g.fillOval(x, y, 5, 5);
	class VelTask extends TimerTask {
		public void run() {
			draw = true;

Oh, I also have an error. It occurs when the program is ran. This is only one of the three classes in the project, just tell me if you need the other ones.

Exception in thread "main" java.lang.NullPointerException
	at WindowProperties.updatePlayer(WindowProperties.java:33)
	at WindowProperties.setupWindow(WindowProperties.java:30)
	at StartWindow.main(StartWindow.java:5)

Because of the error, I think, the timer doesn't loop, which doesn't change the coordinates of where the small circle should be drawn. I think this has to do with the g.fillOval() command, but that's tied in to graphical help, which I've been asking for, Haha.

Also, if you guys could give me any pointers or show me how I could improve something (Syntax, style, or even program wise), that'd be amazing.

Well, that was long.. I think. I've tried to not be too noobish. Thank you guys for reading, or any help you may provide. :)

5 Years
Discussion Span
Last Post by JamesCherrill

1) line frame.setVisible(true); must be last line for JFrame constuctor

2) never use Graphics g = frame.getGraphics();

3) use KeyBindings rather than KeyListener


Swing graphics doesn't work the way you think. You don't control the Graphics and you you don't draw whenever you want. All these things are controlled by Swing. To draw you own stuff you override public void paintComponent(Graphics g) and put your drawing code there. Swing will call that, with the appropriate g, when needed. You can tell Swing that you want to repaint by calling repaint() and Swing will schedule a call to your paintCompnent.

See http://docs.oracle.com/javase/tutorial/uiswing/painting/
and http://www.oracle.com/technetwork/java/painting-140037.html#swing

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.