0

我现在正在和朋友一起开发游戏,但在游戏中实现了保存功能时遇到了一些问题。但是每次我尝试从数组中获取特定值时,我都会不断收到 NullPointerExceptions。

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
        }
    }
}

这是我在运行这些东西时得到的错误代码:

> 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)
4

1 回答 1

1

您已经声明了板数组,但仅在构造函数中初始化它,静态方法调用不调用它,声明如下:

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

另外,考虑 - 在静态函数调用方法“setupGame”期间没有被调用,这是预期的吗?

于 2013-10-16T02:20:05.590 回答