Java game : is it possible that update is called before re-paint is completed ?

I am sometimes getting a null-pointer exception when changing levels in the game. Only sometimes. I don’t understand what can be the possible reason. Even after getting NPE the game runs fine. I think that it is caused maybe because the update method is called while the game is still repainting. So I want to know is it possible in java that update is called before re-paint is completed ?

We would have to see your game loop to be certain, but it sounds like a race condition.
This is common if you don’t realize that threads are involved in the GUI framework.

But I have created only I thread - for the game loop

The game loop is :-

@Override
	public void run() {
		while(isRunning) {
			if(pause == false) {
				upd();
				repaint();
				try {
					Thread.sleep(4);
				}
				catch(InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

We would have to see your game loop. Including your update method and paint method.

But Swing creates several threads, one of which is the repaint thread. Otherwise the program would appear to freeze whenever you clicked buttons or something.

Yep, that loop uses at least 2 threads. Your thread running that loop, and the Swing EDT thread, which calls paint() sometime after repaint() is called on your thread.

Demonstration:


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

public class Race {
	
	public static String str = "Hello World";
	
	@SuppressWarnings("serial")
	public static void main(String[] a) throws InterruptedException {
		JFrame f = new JFrame() {
			@Override
			public void paint(Graphics g) {
				g.drawString(str, 0, 0);
			}
		};
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
		
		while (true) {
			update();
			f.repaint();
			Thread.sleep(15);
		}
	}
	
	public static void update() throws InterruptedException {
		str = null;
		Thread.sleep(1); // slight pause while 'update' occurs
		str = "Hello World";
	}
}

Try resizing the frame with the mouse a bit (to force repaints) and see that very quickly there is an NPE thrown by drawString().

Wait… I remember having to make my own threads for that kind of stuff. Are you sure?

Yes it is! Thus, my game is really updating before repaint is completed, since paint is called in different thread. Is there any way to avoid this?

Somehow synchronize access to those shared resources.

Either using synchronized blocks/methods, Locks, or perhaps AtomicReferences.
There are other ways, but these are relatively straightforward.

This way you restrict access to the resource to only 1 thread at a time, so any updates to the resources (which may involve momentary nulls) appear as instantaneous to other threads.

EDIT: or simply don’t store temporary values (esp. null) in shared references. This is probably the best solution, but it’s good to be acquainted with synchronization since there isn’t a workaround sometimes.

It’s insanely difficult to get the synchronization right if you have 2 or more
threads marching through the same data structures, some making changes,
some trying to generate a new display.

A better strategy is to centralize drawing and mutating in one thread.
Other threads that feed in events of various types, such as network activity,
mouse activity, and display update requests, should only queue these events
to have their effects one at a time.