有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

线程“main”java中出现异常。当试图在另一个类中使用方法时,lang.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()方法中。我想这可能是因为我不理解如何正确地使用我在myBlock类中定义的方法?或者我没有正确初始化block

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

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

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

共 (1) 个答案

  1. # 1 楼答案

    MyBlock构造函数中,更改:

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

    致:

    block = new int[maxRows][maxColumns];
    

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