简体   繁体   English

自定义mousevent方法处的NullPointerException

[英]NullPointerException at custom mousevent method

My gui class for my sudoku app throws a null exception "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" at line 1431 我的数独应用程序的gui类在第1431行抛出空异常“线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException”

    private String click(int x, int y) {
        String dialog = null;
        if (5==5/*sudoku1.grid[x][y].isEditable()==true*/)     {
            dialog = JOptionPane.showInputDialog("Enter Input:");
            int input = Integer.parseInt(dialog);                
1431:       sudoku1.grid[x][y].setValue(input);


        }
        else    {
            //JOptionPane.showMessageDialog(...);
        }
    return dialog;
    }

At start of the class object sudoku1 is declared: 在类对象的开头声明sudoku1:

    public class ClassicGUI extends javax.swing.JFrame {
private boolean finished;
Sudoku sudoku1;
private final int size = 9;

and in main method run() created: 并在主要方法run()中创建:

    public void run() {
    new ClassicGUI().setVisible(true);
    System.out.println("Testing GUI...");
    sudoku1 = new Sudoku(size);
    sudoku1.makeGrid(size);
    }

grid object is a 2dimensional array of type Cell: 网格对象是单元格类型的二维数组:

  public void makeGrid(int size) {
      this.lines=size;
      grid = new Cell[lines][lines];
      for(int i=0;i<size;i++)
                for(int j=0;j<size;j++)    {
                    grid[i][j] = new Cell();
                    grid[i][j].setValue(0);
                }
      }

So appropriate methods for access to grid[x][y] are stated. 因此说明了访问grid [x] [y]的适当方法。 Also objects in the array are initialized in the makeGrid() method. 数组中的对象也可以通过makeGrid()方法进行初始化。 Why the Null Exception Error??? 为什么Null Exception错误???

PS The first if is commented because i had the same exception there :-S... x and y are coordinates ranging from 0-8 and are arguments to click method as shown below PS第一个if被注释,因为那里我有同样的例外:-S ... x和y是0-8的坐标,并且是click方法的参数,如下所示

private void jLabel28MouseClicked(java.awt.event.MouseEvent evt)   {                                      
    jLabel28.setText(click(3, 0));
}

Most likely, sudoku1.grid[x][y] is evaluating to null. sudoku1.grid[x][y]最有可能评估为空。 Then when you call .setValue(input) on null, you get the NPE. 然后,当您在null上调用.setValue(input)时,会得到NPE。

Try changing your code to have .setValue(input) on its own line. 尝试更改代码以使其自己的行具有.setValue(input) I'm not clear from your example exactly what data type sudoku1.grid[x][y] is supposed to evalueate to. 从您的示例中我还不清楚sudoku1.grid[x][y]应该算作哪种数据类型。

Edit: I suppose that sudoku1.grid could itself be null. 编辑:我想sudoku1.grid本身可以为null。 I assume this is a public member of your Sudoku class (you did not provide us with the class definition?). 我假设这是您Sudoku类的公开成员(您没有为我们提供类定义吗?)。

Bottom line is you are getting NullPointerException because you are calling a method on an object that is null. 最重要的是,您正在获取NullPointerException,因为您正在对null的对象调用方法。 If you are not sure how to use the debugger in your environment, another way to solve this is to isolate the problem by breaking down that line of code. 如果您不确定如何在环境中使用调试器,另一种解决方法是通过中断该行代码来隔离问题。 It does a few different things and you should try to reduce it to doing one thing at a time on each line, if you want to locate the problem. 它会做一些不同的事情,如果您要查找问题,则应尝试将其减少为每次执行一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM