“线程中的异常”;“主要”;尝试在另一个类中使用方法时出现java.lang.NullPointerException?

“线程中的异常”;“主要”;尝试在另一个类中使用方法时出现java.lang.NullPointerException?,java,class,methods,nullpointerexception,Java,Class,Methods,Nullpointerexception,我不熟悉Java和OOP。我很难理解我遇到的错误: Exception in thread "main" java.lang.NullPointerException at main.MyBlock.displayBlock(ProgFunAssignment1.java:66) at main.ProgFunAssignment1.main(ProgFunAssignment1.java:38) clearBlock()方法也会发生这种情况。我假设这可能是因为我不理解如何正确

我不熟悉Java和OOP。我很难理解我遇到的错误:

Exception in thread "main" java.lang.NullPointerException
    at main.MyBlock.displayBlock(ProgFunAssignment1.java:66)
    at main.ProgFunAssignment1.main(ProgFunAssignment1.java:38)
clearBlock()
方法也会发生这种情况。我假设这可能是因为我不理解如何正确地使用我在main中的myBlock类中定义的方法?或者我没有正确初始化

我已经查阅了一些关于同一错误的其他答案,但似乎找不到任何对我来说有足够意义的答案。我已将代码粘贴到下面,请帮助我!解释这个错误的真正含义也很好,我想真正理解我做错了什么

请向我询问您可能需要的任何额外信息,有时我并不擅长包含所有信息

package main;
import java.util.Scanner;
import java.lang.*;

public class ProgFunAssignment1 {

    public static void main(String[] args) {
         Scanner console = new Scanner(System.in);
         int maxRows;
         int maxColumns;

         // initial value selection (TASK D)     
         do { System.out.println("Please enter the desired row and column lengths of your block");
         maxRows = console.nextInt();
         maxColumns = console.nextInt();
         if ((maxRows < 3 || maxRows > 10) || (maxColumns < 3 || maxColumns > 10)) {
             System.out.println("Values invalid, please enter values between 3 and 10");}
         } while ((maxRows < 3 || maxRows > 10) || (maxColumns < 3 || maxColumns > 10));

         MyBlock block = new MyBlock(maxRows, maxColumns);


         //Menu creation (TASK E)
         int choice;
         int colPosition;
         int rowPosition;

        do { System.out.println("Please select one of the following options \n 1. Add a House \n 2. Display Block \n 3. Clear Block \n 4. Quit");
         choice = console.nextInt();

         if (choice == 1) {
             System.out.println("Please enter position coordinates of the house:");
             rowPosition = console.nextInt();
             colPosition = console.nextInt();
             // If coordinates not valid, go back to menu
         }
         else if (choice == 2) {
             block.displayBlock();
         }
         else if (choice == 3) {
             block.clearBlock();
         }
         else if (choice == 4) {
             System.exit(0);
         }
         else {
             System.out.println("Choice was not valid, please try again.");
             }} while (choice < 0 || choice > 4);    
    }
}

class MyBlock {
    private int[][] block;
    boolean vacant;


    public MyBlock(int maxRows, int maxColumns) {

        int block[][] = new int[maxRows][maxColumns];
        vacant = true;

    }

    public void displayBlock() {
        // content of displayBlock() method here
        for (int[] x : block)
        {
           for (int y : x)
           {
                System.out.print(y + " ");
           }
           System.out.println();
        }

        }

    public void clearBlock() {
        // content of clearBlock() method here
        vacant = true;
        for (int i = 0; i < block.length; i++) {
            for (int j = 0; j< block[i].length; j ++)
                block[i][j] = 0;
        }
    }

    public boolean buildHouse(int rowPos, int colPos, int rows, int columns) {
        // content of buildHouse() method here

        // useful code
        if (true) 
            return true;
        else 
            return false;
}
} 
packagemain;
导入java.util.Scanner;
导入java.lang.*;
公共类ProgFunAssignment1{
公共静态void main(字符串[]args){
扫描仪控制台=新扫描仪(System.in);
int最大行;
int-maxColumns;
//初始值选择(任务D)
do{System.out.println(“请输入所需的块行和列长度”);
maxRows=console.nextInt();
maxColumns=console.nextInt();
如果((maxRows<3 | | maxRows>10)| |(maxColumns<3 | | maxColumns>10)){
System.out.println(“值无效,请输入3到10之间的值”);}
}而((maxRows<3 | | maxRows>10)| |(maxColumns<3 | | maxColumns>10));
MyBlock block=新的MyBlock(maxRows,maxColumns);
//菜单创建(任务E)
智力选择;
int colPosition;
内行位置;
do{System.out.println(“请选择下列选项之一\n 1.添加房屋\n 2.显示块\n 3.清除块\n 4.退出”);
choice=console.nextInt();
如果(选项==1){
System.out.println(“请输入房屋的位置坐标:”);
rowPosition=console.nextInt();
colPosition=console.nextInt();
//如果坐标无效,请返回菜单
}
else if(选项==2){
block.displayBlock();
}
else if(选项==3){
block.clearBlock();
}
else if(选项==4){
系统出口(0);
}
否则{
System.out.println(“选择无效,请重试。”);
}}而(选择<0 | |选择>4);
}
}
类MyBlock{
私有int[][]块;
布尔空;
公共MyBlock(int-maxRows、int-maxColumns){
int块[][]=新的int[maxRows][maxColumns];
空=真;
}
公共void displayBlock(){
//这里是displayBlock()方法的内容
对于(int[]x:block)
{
对于(整数y:x)
{
系统输出打印(y+“”);
}
System.out.println();
}
}
公共无效clearBlock(){
//这里是clearBlock()方法的内容
空=真;
对于(int i=0;i
MyBlock
构造函数中,更改:

int block[][] = new int[maxRows][maxColumns];
致:


您只是用一个本地属性隐藏实例属性。

//如果(true)返回true,则有用的代码;否则返回false;->这远远不是有用的代码。
int block[][]=newint[maxRows][maxColumns]这与您认为的不同。所以在你的方法中,
循环开始了。@FedericoklezCulloca-darn,我认为它没有。你能给我指出使用这些变量初始化数组的正确方向吗?@FedericoklezCulloca我花了很长时间才意识到这有什么不对。。。Jaimee lee,您有两个名为
block
的变量,一个在类级别,另一个在构造函数中。他们不一样。是的,我不是很清楚公平。
block = new int[maxRows][maxColumns];