0

I'm working on a game with a friend right now and I got some problems by implementing a save feature to the game. But I keep getting NullPointerExceptions every time I try to get a specific value from an array.

SaveGame.java:

public class SaveGame {
    static int newValue;
    //unnecessary stuff hidden...

    public static void SaveGame() {
        //The class which uses getTokenPositionList()
    }

    //Saves the positioning of the tokens within a String
    static String getTokenPositionList() {
        String tokenPositionList = "";
        for (int row = 0; row < 10; row++) {
            for (int col = 0; col < 10; col++) {
                tokenPositionList = tokenPositionList + KodeKs.KodeKsData.getKodeKsData(row, col);
            }
        }
        return tokenPositionList;
    }  // end getTokenPositionList()
}

KodeKs.java:

public class KodeKs extends JPanel {
    public KodeKs() {
        //lots of unessecary stuff hidden
        public static class KodeKsData {
            public static int[][] board;
            public KodeKsData() {
                board = new int[10][10];
                setUpGame();
            }
            public static int getKodeKsData(int row, int col) {
                return board[row][col];

            }
            //more stuff hidden
        }
    }
}

And this is the error-code I get when running this stuff:

> Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
>     at KodeKs$KodeKsData.getKodeKsData(KodeKs.java:597)     at
> SaveGame.getTokenPositionList(SaveGame.java:61)     at
> SaveGame.SaveGame(SaveGame.java:33)     at
> KodeKsToolBar$3.actionPerformed(KodeKsToolBar.java:48)  at
> javax.swing.AbstractButton.fireActionPerformed(Unknown Source)  at
> javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)  at
> javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)  at
> javax.swing.DefaultButtonModel.setPressed(Unknown Source)   at
> javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown
> Source)     at java.awt.Component.processMouseEvent(Unknown Source)     at
> javax.swing.JComponent.processMouseEvent(Unknown Source)    at
> java.awt.Component.processEvent(Unknown Source)     at
> java.awt.Container.processEvent(Unknown Source)     at
> java.awt.Component.dispatchEventImpl(Unknown Source)    at
> java.awt.Container.dispatchEventImpl(Unknown Source)    at
> java.awt.Component.dispatchEvent(Unknown Source)    at
> java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)   at
> java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)    at
> java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)    at
> java.awt.Container.dispatchEventImpl(Unknown Source)    at
> java.awt.Window.dispatchEventImpl(Unknown Source)   at
> java.awt.Component.dispatchEvent(Unknown Source)    at
> java.awt.EventQueue.dispatchEventImpl(Unknown Source)   at
> java.awt.EventQueue.access$200(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.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown
> Source)     at java.awt.EventQueue$4.run(Unknown Source)    at
> java.awt.EventQueue$4.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)
1
  • What line is 597 in KodeKsData? Commented Oct 16, 2013 at 2:17

1 Answer 1

1

You have declared board array, but init it only in constructor, which is not called for static method call, declare it like:

public static int[][] board = new int[10][10];

also, consider - during static function call method "setupGame" is not called, is it expected?

Sign up to request clarification or add additional context in comments.

1 Comment

Or use the static initializer static { board = new int[10][10]; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.