NPE即使分配了阵列

时间:2017-12-14 10:02:51

标签: java arrays object nullpointerexception

这是我第一次做代码,我遇到了一个问题,我无法解决。即使我在另一个对象中分配了这样的数组,我也会得到NPE。

    int size= 80;
    final int village=11;
    Police test;
      Road Road;

    I i;
    Map map;
    void setup() {
    size(880,560,P2D);
      test=new Police();
      i=new I();
      map=new Map();
      test.state=test.INIT;

    }

    void draw() {
      map.gameLevel=village;
      background(125);
      map.drawMap();
      i.move();  
      i.display();
      if (test.beSeen(test.x, test.y, i.x, i.y)) {
        test.move(4);
        test.display(test.policeAngry);    
        test.state=test.ANGRY;
      } else {
        test.move(2);
        test.display(test.police);

        test.state=test.INIT;
      }
    }

这是关于主类,但我怀疑它是否相关。

    class Map {
      int map[][]=new int [1][1];
      final int GROUND=0;
      final int HOUSE=1;
      int gameLevel;
      final int village=11;
      PImage ground;
      PImage house;
      int maxNumX, maxNumY;



      Map() {
            Road= new Road();
        switch(gameLevel) {
        default:
          initMap(1, 1);

        case village:
          initMap(11, 7);
          break;
        }
        ground = loadImage("img/road/road"+Road.Checkopening(1,1)+"-01.png");
        ground = loadImage("img/road/road"+20+"-01.png");
        house = loadImage("img/build.png");
      }

      void initMap(int XNum, int YNum) {
        map=new int [XNum][YNum];
        for (int drawX=0; drawX<XNum; drawX++) {
          for (int drawY=0; drawY<YNum; drawY++) {
            map[drawX][drawY]=GROUND;
          }
          maxNumX=XNum;
          maxNumY=YNum;
        }
      }

      void drawMap() {
        for (int drawX=0; drawX<map.length; drawX++) {
          for (int drawY=0; drawY<map[0].length; drawY++) {
            if (map[drawX][drawY]==GROUND) {
              image(ground, drawX*size, drawY*size, size, size);
            } else if (map[drawX][drawY]==HOUSE) {
              image(house, drawX*size, drawY*size, size, size);
            }
          }
        }
        switch (gameLevel) {
        case village:
          for (int drawX=0; drawX<map.length; drawX++) {
            map[drawX][0]=GROUND;
            map[drawX][(map[0].length)-1]=GROUND;
          }
          for (int drawY=0; drawY<map[0].length; drawY++) {
            map[0][drawY]=GROUND;
            map[map.length-1][drawY]=GROUND;
          }
          for (int drawX=1; drawX<=9; drawX++) {
            for (int drawY=1; drawY<=5; drawY++) {
              map[drawX][drawY]=HOUSE;
            }
          }
          for (int drawY=1; drawY<=5; drawY++) {
            map[5][drawY]=GROUND;
          }
    // printing checker
        for (int drawX=0; drawX<map.length; drawX++) {
          for (int drawY=0; drawY<map[0].length; drawY++) {
            println(map[drawX][drawY]);
          }
          }
          break;
        }
      }
    }

这是关于地图类的。我尝试制作一个处理地图信息的二维数组。因此map [] []创建并分配了一个数字。只要我使用GROUND和HOUSE存储特定类型,就可以了。但后来我想让GROUND成为一个类,并添加不同的行为,从而产生不同的输出。然后就会发生这种情况。

    class Road{
      int opening;
        final int GROUND=0;

      Road() {

      }
      int Checkopening(int XNum,int YNum) {
        int directionNum=0;
        if (map.map[XNum+1][YNum]==map.GROUND || !(XNum==map.maxNumX)) {
          // this is where it goes NPE
          directionNum+=1;
        }
        if (map.map[XNum-1][YNum]==map.GROUND) {
          directionNum+=1;
        }
        if (map.map[XNum][YNum+1]==map.GROUND) {
          directionNum+=1;
        }
        if (map.map[XNum][YNum-1]==map.GROUND) {
          directionNum+=1;
        }
        directionNum=20;
        //this is for testing, just to let it run this line of code, and see if it works. 
        return directionNum;
      }
    }

当我打开调试器并停在该行错误代码时,它显示“map”为null。但是当我让它停在map的初始化处理时,它会显示所有map [] []数组的指定数字。顺便说一句,我也得到这个“这个$”。我在网上搜索但不太明白........我将非常感谢所有的帮助:P

0 个答案:

没有答案