简体   繁体   English

Java的帮助! 实现某个对象的二维数组,该对象具有多个私有数据类型和对象

[英]Java help! implementing a 2d array of a certain object, the object has multiple private data types and objects

I'm trying to make a 2d array of an object in java. 我正在尝试在Java中创建对象的2D数组。 This object in java has several private variables and methods in it, but won't work. Java中的此对象中包含几个私有变量和方法,但是不起作用。 Can someone tell me why and is there a way I can fix this? 有人可以告诉我原因,有办法解决这个问题吗? This is the exeception I keep getting for each line of code where I try to initialize and iterate through my 2d object. 这是我不断尝试在2d对象中进行初始化和迭代的每一行代码的一种理解。

"Exception in thread "main" java.lang.NullPointerException at wumpusworld.WumpusWorldGame.main(WumpusWorldGame.java:50) Java Result: 1" “ wumpusworld.WumpusWorldGame.main(WumpusWorldGame.java:50)的线程“ main”中的java.lang.NullPointerException异常Java结果:1”

Here is my main class: 这是我的主要课程:

public class WumpusWorldGame {

    class Agent {

        private boolean safe;
        private boolean stench;
        private boolean breeze;

        public Agent() {
            safe = false;
            stench = false;
            breeze = false;
        }

    }

    /**
     * @param args
     *            the command line arguments
     * @throws java.lang.Exception
     */
    public static void main(String [] args) {
        // WumpusFrame blah =new WumpusFrame();
        // blah.setVisible(true);

        Scanner input = new Scanner(System.in);

        int agentpts = 0;
        System.out.println("Welcome to Wumpus World!\n ******************************************** \n");

//ArrayList<ArrayList<WumpusWorld>> woah = new ArrayList<ArrayList<WumpusWorld>>();

        for (int i = 0 ; i < 5 ; i++) {

            WumpusWorldObject [] [] woah = new WumpusWorldObject [5] [5];

            System.out.println( "*********************************\n Please enter the exact coordinates of the wumpus (r and c).");
            int wumpusR = input.nextInt();
            int wumpusC = input.nextInt();

            woah[wumpusR][wumpusC].setPoints(-3000);
            woah[wumpusR][wumpusC].setWumpus();
            if ((wumpusR <= 5 || wumpusC <= 5) && (wumpusR >= 0 || wumpusC >= 0)) {
                woah[wumpusR][wumpusC].setStench();
            }
            if (wumpusC != 0) {
                woah[wumpusR][wumpusC - 1].getStench();
            }
            if (wumpusR != 0) {
                woah[wumpusR - 1][wumpusC].setStench();
            }
            if (wumpusC != 4) {
                woah[wumpusR][wumpusC + 1].setStench();
            }
            if (wumpusR != 4) {
                woah[wumpusR + 1][wumpusC].setStench();
            }

            System.out.println( "**************************************\n Please enter the exact coordinates of the Gold(r and c).");
            int goldR = input.nextInt();
            int goldC = input.nextInt();
            woah[goldR][goldC].setGold();
            System.out.println("***************************************\n How many pits would you like in your wumpus world?");
            int numPits = input.nextInt();
            for (int k = 0 ; k < numPits ; k++) {
                System.out.println("Enter the row location of the pit");
                int r = input.nextInt();
                System.out.println("Enter the column location of the pit");
                int c = input.nextInt();
                woah[r][c].setPit();
                if ((r <= 4 || c <= 4) && (r >= 0 || c >= 0)) {
                    woah[r][c].setBreeze();
                }
                if (c != 0) {
                    woah[r][c - 1].setBreeze();
                }
                if (r != 0) {
                    woah[r - 1][c].setBreeze();
                }
                if (c != 4) {
                    woah[r][c + 1].setBreeze();
                }
                if (r != 4) {
                    woah[r + 1][c].setBreeze();
                }
            }
            for (int x = 0 ; x < 4 ; x++) {
                int j = 0;
                while (j < 4) {
                    agentpts = agentpts + woah[x][j].getPoints();
                    Agent [] [] k = new Agent [4] [4];

                    if (woah[x][j].getWumpus() == true) {
                        agentpts = agentpts + woah[x][j].getPoints();
                        System.out.println("You just got ate by the wumpus!!! THE HORROR!! Your score is " + agentpts);
                    }
                    if (woah[x][j].getStench() == true) {
                        k[x][j].stench = true;
                        System.out.println("You smell something funny... smells like old person.");
                    }
                    if (woah[x][j].getBreeze() == true) {
                        k[x][j].breeze = true;
                        System.out.println("You hear a breeze. yeah");
                    }
                    if (woah[x][j].getPit() == true) {
                        agentpts = agentpts + woah[x][j].getPoints();
                        System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH! you dumb bith, your dead now.");
                    }
                    // if breeze or stench, if breeze and stench, if nothing, etc then move.
                    k[x][j].safe = true;
                    // if(k[i][j].isSafe()!=true){

                    // } else { }
                }
            }
        }
    }
}

Here is my class object that I'm trying to implement: 这是我要实现的类对象:

package wumpusworld;

/**
 *
 * @author Jacob
 */

public class WumpusWorldObject {
private boolean stench;
private boolean breeze;
private boolean pit;
private boolean wumpus;
private boolean gold;
private int points;
private boolean safe; 

public WumpusWorldObject(){   
}
    public boolean getPit() { 
      return pit;
    }

    public void setPit() {
        this.pit = true;
    }

    public boolean getWumpus() {
        return wumpus;
    }

    public void setWumpus() {
        this.wumpus = true;

    }

    public int getPoints() {
        return points;
    }

    public void setPoints(int points) {
        this.points = points;
    }
    public boolean getStench() {
        return stench;
    }

    public void setStench() {
        this.stench = true;
    }

    public boolean getBreeze() {
        return breeze;
    }

    public void setBreeze() {
        this.breeze = true;
    }

    public boolean getSafe() {
        return safe;
    }

    public void setSafe() {
        this.safe = true;
    }
    public void setGold(){
        this.gold=true;
    }
}

Creating array doesn't mean it will be automatically filled with new instances of your class. 创建数组并不意味着它将自动用您的类的新实例填充。 There are many reasons for that, like 原因有很多,例如

  • which constructor should be used 应该使用哪个构造函数
  • what data should be passed to this constructor. 什么数据应该传递给此构造函数。

This kind of decisions shouldn't be made by compiler, but by programmer, so you need to invoke constructor explicitly. 这种决定不应该由编译器来决定,而应该由程序员来决定,因此您需要显式调用构造函数。

After creating array iterate over it and fill it with new instances of your class. 创建数组后,对其进行迭代,并用类的新实例填充它。

for (int i=0; i<yourArray.length; i++)
    for (int j=0; j<yourArray[i].length; j++)
        yourArray[i][j] = new ...//here you should use constructor 
AClass[][] obj = new AClass[50][50];

is not enough, you have to create instances of them like 还不够,您必须创建它们的实例,例如

obj[i][j] = new AClass(...);

In your code the line 在您的代码行中

woah[wumpusR][wumpusC].setPoints(-3000);

must be after 必须在之后

woah[wumpusR][wumpusC] = new WumpusWorldObject();

.

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

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