java.lang.NullPointerException |尝试从另一个类数组中获取值

时间:2013-10-16 02:11:50

标签: java nullpointerexception

我正在与朋友一起制作游戏,我通过在游戏中实施保存功能遇到了一些问题。但是每次我尝试从数组中获取特定值时,我都会继续获取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)

1 个答案:

答案 0 :(得分:1)

你已经声明了board数组,但只在构造函数中初始化它,而不是为静态方法调用调用它,声明如下:

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

另外,考虑一下 - 在静态函数调用方法中,“setupGame”没有被调用,是否需要?