-2

I'm making a game with a timer and a JFrame (and many other things but only these 2 are causing problems), and after running the segments below, I got a weird error. At least for me who has never used these classes prior to this.

Start executing this

private static GameView window;
private static Timer time;
public static void main(String args[])
{
    window = new GameView(800,600);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);

    time = new Timer();
    time.schedule( new TimerTask(){
        public void run(){GameState.update(); 
        window.paintComponents(null);}
        },0, 40);

}

which calls this:

public void paintComponents (Graphics g)
{

    for(Bullet j : GameState.getEnBullets()){
        g.drawImage(j.getImage(),j.getX(), j.getY(), null);}
    for(Enemy j : GameState.getEnemies()){
        g.drawImage(j.getImage(),j.getX(), j.getY(), null);}
    for(Bullet j : GameState.getPlayBullets()){
        g.drawImage(j.getImage(),j.getX(), j.getY(), null);}
    this.paint(g);
}

And here's the error:

Exception in thread "Timer-0" java.lang.NullPointerException
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at Game.GameView.paintComponents(GameView.java:59)
    at Game.GameController$1.run(GameController.java:39)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)

I also get a blank JFrame window (GameView extends JFrame).

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Will
  • 179
  • 3
  • 3
  • 6
  • 1
    please check all comments by Andrew Thompson in your previous thread – mKorbel Jun 07 '11 at 09:50
  • @mKorbel: ITYM [Painting Graphics2D in a JFrame](http://stackoverflow.com/questions/6260436/painting-graphics2d-in-a-jframe). ;) @Will: For better help sooner, post an [SSCCE](http://pscode.org/sscce.html). – Andrew Thompson Jun 07 '11 at 10:17
  • @Andrew Thompson , @Will I hijacking both thread together with ... http://stackoverflow.com/questions/6260436/painting-graphics2d-in-a-jframe/6263897#6263897 – mKorbel Jun 07 '11 at 10:29
  • @Andrew Thompson: if I tried to post an SSCCEE, no one would want to read it, as it would require me posting **9 classes, each with about 200 lines of code.** If people are willing to do that to help me, then I'll do it. – Will Jun 07 '11 at 20:56
  • 1
    that's not an SSCCE since an SSCCE will be one file and will have at most 100 lines of code. Please read Andrew's link before answering. Creating an SSCCE will require significant effort on your part but it is effort worth expending as it will often lead you to solving your problem yourself, or if not will allow you to post code that we can test, analyze and change in order to help you get a solution. – Hovercraft Full Of Eels Jun 07 '11 at 21:51

3 Answers3

2

You get NPE because you are passing null as graphics in window.paintComponents(null); And then you are calling g.drawImage(j.getImage(),j.getX(), j.getY(), null); where g is null.

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
2

The method to override is paintComponent() not "paintComponents" (with an s).

You should never invoke the paintComponent() method directly. Instead you invoke the repaint() method on the component.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
camickr
  • 321,443
  • 19
  • 166
  • 288
2

You shouldn't be painting direclty in a JFrame at all but rather a JPanel or JComponent that is held by the JFrame. You should override the JPanel's paintComponent method as noted above (not the JFrame since it doesn't even have this method) and paint in there. Another thing, don't use a java.util.Timer but rather a javax.swing.Timer better known as a Swing Timer since this is a Swing application. Also you shouldn't call paint/paintComponent directly but rather have your GUI update class fields then call repaint() on the JPanel that you're doing your drawing on and then paintComponent will (usually) be called by the JVM. There are many examples of Swing animation here in this forum and I suggest you search out these examples and learn from them as I think that they can help you.

Edit: heck, you've already been told all of this in your previous threads. Why should we help you if you ignore our advice?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • What is the problem with using a util Timer? Can you support this? I've used them repeatedly with no problems. Killer Game Programming in Java shows in Chapt. 2 that they offer very good response, as opposed to the Swing Timer that can clog the EDT if too much is triggered by it. Also, painting can occur in a JFrame if you manage the panes, and in *some* cases might be more appropriate than adding another component. – Phil Freihofner Jun 22 '11 at 23:59
  • @Phil: a util Timer can be used if care is taken for Swing calls to be made only on the EDT. The Swing Timer takes care of this for you and so is more convenient but not absolutely necessary. Any time or CPU-intensive task must be performed on a background thread no matter which timer is used. – Hovercraft Full Of Eels Jun 23 '11 at 02:08
  • @Hover... I don't think it is even possible to make a Swing call that doesn't get automatically routed to the EDT. Or maybe my understanding of "Java Concurrency in Practice" chapter 9 is incorrect. – Phil Freihofner Aug 24 '11 at 19:40
  • @Phil: yes, your understanding of chapter 9 is incorrect. It is in fact all too easy to make Swing calls off of the EDT and that is why care must be taken to prevent this. – Hovercraft Full Of Eels Aug 24 '11 at 20:13