Weird NullPointerException

When I run my Autoplayer, I’m getting randomly a NullPointer Exception.
Random means, that if I run my Autoplayerview multiple times on the same seed, sometimes this exception occurs and sometimes not.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "tetris.game.pieces.Piece$PieceType.ordinal()" because "pieceType" is null
        at tetris.view.ViewHelper.getColorForPieceType(ViewHelper.java:29)
        at tetris.view.BoardComponent.paintBoard(BoardComponent.java:84)
        at tetris.view.TetrisComponent.paintBoard(TetrisComponent.java:1)
        at tetris.view.TaskedTetrisComponent.paintBoard(TaskedTetrisComponent.java:1)
        at tetris.view.AutoplayerView.paintBoard(AutoplayerView.java:1)
        at tetris.view.BoardComponent.paintComponent(BoardComponent.java:108)
        at tetris.view.TetrisComponent.paintComponent(TetrisComponent.java:40)
        at java.desktop/javax.swing.JComponent.paint(JComponent.java:1119)
        at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5311)
        at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1657)
        at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1632)
        at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1570)
        at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1337)
        at java.desktop/javax.swing.JComponent._paintImmediately(JComponent.java:5259)
        at java.desktop/javax.swing.JComponent.paintImmediately(JComponent.java:5069)
        at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:879)
        at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:862)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
        at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:862)
        at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:835)
        at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:784)
        at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1898)
        at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:771)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
        at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:741)
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

I’ve tried to analyse this error but I cannot find the .ordinal() method and I checked my pieceType Implementation which seems to be fine.
Interestingly none of the mentioned lines of code in the exception are implemented by myself.

1 Like

PieceType is an enum, and those have methods like ordinal(), which give you the “index” of that enum.

In your case, since the enum instance (i.e. the variable pieceType) is null. Reading the code and using the debugger will tell you why.