gpt4 book ai didi

java - 尝试使用另一个类中的方法时,线程 "main"java.lang.NullPointerException 中出现异常?

转载 作者:行者123 更新时间:2023-12-02 08:43:14 27 4
gpt4 key购买 nike

我是 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 类中定义的方法有关?或者也许我还没有初始化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;
}
}

最佳答案

MyBlock 构造函数中,更改:

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

至:

block = new int[maxRows][maxColumns];

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

关于java - 尝试使用另一个类中的方法时,线程 "main"java.lang.NullPointerException 中出现异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61246228/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com