java中的Nullpointers以及如何克服这些问题?

时间:2014-03-23 14:20:42

标签: java nullpointerexception

比如说,如果在一个程序中你得到一个空指针错误,为你的程序添加一段代码使程序运行正常但没有那段代码,程序没有按预期工作,允许空指针错误发生是一个好主意,如果是这样,有没有办法在它显示到控制台之前捕获它。我所知道的一种方式是,使用try和catch但是在我过去的经验中这没有用,我尝试使用它可能是错误的,但这就是我尝试的方式。

try {
    // line / s of code to catch the error from

} catch (java.lang.NullPointerException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

已编辑:我收到的错误列表:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at playAGame.endGameResult(playAGame.java:204)
    at playAGame.checkingWinner(playAGame.java:159)
    at playAGame.callCheckWinner(playAGame.java:179)
    at playAGame.moveSetup(playAGame.java:66)
    at playAGame$1.actionPerformed(playAGame.java:52)
    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.AWTEventMulticaster.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)

这是因为这一行:button = new JButton[button.length][button.length];

我正在创建一个TicTacToe游戏,如果我从代码中删除该行,游戏就无法正常工作。

编辑:我相信这是提供空指针的行之一,如果我错了,请纠正我。基本上,我已经创建了一个方法来检查三个给定的按钮是否具有值X,如果它确实将win变量触发为true。这就是我在检查是否有人赢得了TicTacToe游戏的方式。

public void checkingWinner(JButton button1, JButton button2, JButton button3) { 

        if(buttonA.getText().equals(buttonB.getText()) && buttonB.getText().equals(buttonC.getText()) && buttonA.getText().equals("X"))
        { 
win = true;
System.out.pl("winner is X");   


    }

2 个答案:

答案 0 :(得分:0)

在没有看到整个代码的情况下很难分辨,但是可能是这样的情况,即您对数组进行的初始化会引发异常,因为它是第一次#&# 39;正在被初始化,在此期间被引用。

如果是这种情况,你应该通过使用宽度和高度的常量而不是自引用来解决这个问题:

public static final int HEIGHT = ...;
public static final int WIDTH = ...;
...
button = new JButton[HEIGHT][WIDTH];

答案 1 :(得分:-2)

您确定代码行是否抛出NullPointerException?因为它适合我。

public class Main {
    static String a;
    public static void main(String args[]) {
        try {
            a.charAt(0);
        } catch (java.lang.NullPointerException e) {
            System.out.println("Thrown");
            e.printStackTrace();
        }
    }
}