User
User

Reputation: 1488

"AWT-EventQueue-0" NullpointerException inconsistent error message

I'm getting this error message when im starting my Java application:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at engine.Display.GamePanel.paintComponent(GamePanel.java:102)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1100(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

The application doesn't close and it lets me run it normally and there is no visual buggs etc etc but i still get this error message SOMETIMES when i start the application.

The only peice of my own code it points me to is line 102 in my GamePanel class which is my paintComponent method here:

    @Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    //g.drawImage(test, picX, picY, picSizeX, picSizeY, null);
    mao.Draw(picX, picY, g);
}

And inside the mao.Draw we have this:

    public void Draw(int x, int y, Graphics g){
    Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(characterImage, x, y, null);
        g2d.drawRect(x, y, getWidth(), getHeight());
}

I have tinkered around with this problem but i cannot seem to find the error.

Upvotes: 1

Views: 1484

Answers (2)

user85421
user85421

Reputation: 29680

The error is being caused in line 102 of GamePanel.java which I believe is

  mao.Draw(picX, picY, g);

because mao is not yet initialized, that is, has the value null when the method is called. Probably your application is not fully initialized but the GamePanel is already being displayed.

You can do the following to check if that is the case:

if (mao != null) {
    mao.Draw(picX, picY, g);
}

Upvotes: 2

icza
icza

Reputation: 417797

The exception is NullPointerException in your paintComponent() method.

So the problem is there, not in your Draw() method. In paintComponent() you access the mao identifier which I assume is an attribute (of the GamePanel class).

All painting in Swing is done on the EDT thread, and I assume you initialize and set this mao attribute NOT on the EDT thread => this is a synchronization issue. Either assign the mao attribute on the EDT thread, or make it synchronized so that multiple threads accessing it properly see its true value.

You get this exception once or rare occasions and your application works, because only the paintComponent() will be aborted, but subsequent attempts to paint your component might succeed (if the mao becomes finally visible by the EDT thread or was modified meanwhile).

Upvotes: 1

Related Questions