Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (798)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (865)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java game : is it possible that update is called before re-paint is completed ?  (Read 2348 times)
0 Members and 1 Guest are viewing this topic.
Offline RKJ

Senior Newbie





« Posted 2014-12-18 22:19:35 »

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 ?
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #1 - Posted 2014-12-18 22:25:55 »

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.
Offline RKJ

Senior Newbie





« Reply #2 - Posted 2014-12-18 22:28:02 »

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

The game loop is :-

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
@Override
   public void run() {
      while(isRunning) {
         if(pause == false) {
            upd();
            repaint();
            try {
               Thread.sleep(4);
            }
            catch(InterruptedException e) {
               e.printStackTrace();
            }
         }
      }
   }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KudoDEV

JGO Ninja


Medals: 79
Exp: 6 years


Game Dev Hobbyist


« Reply #3 - Posted 2014-12-18 22:29:58 »

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

Offline HeroesGraveDev

JGO Kernel


Medals: 382
Projects: 11
Exp: 4 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #4 - Posted 2014-12-18 22:30:08 »

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.

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #5 - Posted 2014-12-18 22:37:52 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
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().
Offline tdegroot96
« Reply #6 - Posted 2014-12-18 22:39:43 »

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.
Wait.. I remember having to make my own threads for that kind of stuff. Are you sure?
Offline RKJ

Senior Newbie





« Reply #7 - Posted 2014-12-18 22:46:39 »

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?
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #8 - Posted 2014-12-18 22:48:15 »

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.
Offline ddyer
« Reply #9 - Posted 2014-12-20 18:10:05 »


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.

Pages: [1]
  ignore  |  Print  
 
 

 
Riven (28 views)
2019-09-04 15:33:17

hadezbladez (3957 views)
2018-11-16 13:46:03

hadezbladez (1434 views)
2018-11-16 13:41:33

hadezbladez (3954 views)
2018-11-16 13:35:35

hadezbladez (765 views)
2018-11-16 13:32:03

EgonOlsen (4079 views)
2018-06-10 19:43:48

EgonOlsen (4647 views)
2018-06-10 19:43:44

EgonOlsen (2748 views)
2018-06-10 19:43:20

DesertCoockie (3643 views)
2018-05-13 18:23:11

nelsongames (3834 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!