【问题标题】:Why is my array object moving out of bounds within my 8x8 grid?为什么我的数组对象在我的 8x8 网格中超出范围?
【发布时间】:2016-05-27 09:18:56
【问题描述】:

我有一个对象 (BigCrawler) 越界,每次它在我的北端和南端的 8x8 网格中的 0 和 7 命中时给我一个空指针异常,而不是在东侧和西侧。我的 NPE 是

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 8
        at GridPanel.containsToken(GridPanel.java:78)
        at BigCrawler.canMove(BigCrawler.java:63)
        at BigCrawler.move(BigCrawler.java:79)

我很肯定这符合我的逻辑。我有 3 个其他对象,它们实例化并有 2 个向东和向西移动,而另一个在我每次按下移动按钮时向北和向南移动。它们每个都工作得很好,但是由于某种原因,每当我的 BigCrawler 对象撞到北端或南端时,它都不会回头,而是会继续给 NPE。顺便说一句,第 79 行是发布的第三个 if 语句。我可以发布任何需要的代码,但我相信它在发布区域内。

我的 BigCrawler.java

//************************************************************************
//  BigCrawler.java
//
//
//************************************************************************


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class BigCrawler extends Crawler
{

//***********************************************************************************
//  Instance variables
//***********************************************************************************

private int legs;

//***********************************************************************************
//  Constructors
//***********************************************************************************

public BigCrawler(String name, ImageIcon image, String direction, int legs)
{       super(name, image, direction, 1);

    if (!(direction.equals("NorthEast") || direction.equals("SouthWest")))
        direction = "NorthEast";


    this.legs = legs;
}


//***********************************************************************************
//  Accessors
//***********************************************************************************

public int getLegs()
{ return legs;
}

//***********************************************************************************
//  Mutators
//***********************************************************************************

public void setLegs(int legs)
{ this.legs = legs;
}


//***********************************************************************************
//  Additional Methods
//***********************************************************************************

public String toString()
{
    return super.toString() + "and has " + legs + " creepy crawly legs.";
}

public boolean canMove(GridPanel bugGrid, int newRow, int newColumn)
    {   if (bugGrid.containsToken(newRow, newColumn))
            return false;
        else
            return true;
    }

 public void move(GridPanel bugGrid)
     {
    if (direction == "NorthEast" && column == bugGrid.getBoardColumns()-1)
        direction = "SouthWest";
    else if (direction == "SouthWest" && column == 0)
        direction = "NorthEast";

    if (direction == "SouthWest")
    {      column--;
            row++;
           if (canMove(bugGrid, row, column))
           {  bugGrid.addImage(null, row-1, column+1);
              bugGrid.addImage(image, row, column);
           }
           else
           { column++;
             row--;}
    }

    else
    {       column++;
            row--;
            if (canMove(bugGrid, row, column))
            {  bugGrid.addImage(null, row+1, column-1);
               bugGrid.addImage(image, row, column);
            }
            else
            { column--;
              row++;}
    }
   }


}

还有我的 GridPanel

        //********************************************************************
    //  GridPanel.java       Java Foundations
    //
    //  A grid panel to represent a  game board of buttons (8x8 default).
    //  The buttons use an ImageIcon to display the playing "pieces"
    //********************************************************************

    import java.awt.*;
    import javax.swing.*;

    public class GridPanel extends JPanel
    {
      //-----------------------------------------------------------------
      //  Sets up this panel with some buttons to show how grid
      //  layout affects their position, shape, and size.
      //-----------------------------------------------------------------

      //-----------------------------------------------------------------
      //  The only instance data item is an 8x8 array of buttons
      //-----------------------------------------------------------------
      private int rows, columns;
      private JButton[][] buttonArray;

      //-------------------------------------------------------------------------------------------
      //   The constructor for the panel.  This sets up the array using a GridLayout GUI component
      //   The text on each button is blank and no ImageIcons are loaded.
      //-------------------------------------------------------------------------------------------
      public GridPanel(int rows, int columns)
       {
          if (rows > 0 && columns > 0)
          { this.rows = rows;
            this. columns = columns;
            buttonArray = new JButton[rows][columns];
            setLayout(new GridLayout(rows,columns));    }
          else
          { rows = 8;
            columns = 8;
            buttonArray = new JButton[8][8];
            setLayout(new GridLayout(8,8));             }

          setBackground(Color.green);


          for (int i = 0; i < rows; i++)
            for (int j = 0; j < columns; j++)
                buttonArray[i][j] = new JButton(" ");

          for (int i = 0; i < rows; i++)
            for (int j = 0; j < columns; j++)
                add(buttonArray[i][j]);

       }

      //-------------------------------------------------------------------------------------------
      //   Accessors to return board size
      //-------------------------------------------------------------------------------------------
       public int getBoardRows()
       { return rows;   }


        public int getBoardColumns()
       { return columns;   }


    //-------------------------------------------------------------------------------------------
      //   A method to add an ImageIcon image to a specific position on the board
      //-------------------------------------------------------------------------------------------
       public void addImage(ImageIcon image, int row, int col)
       {
           if (row < rows && row >= 0 && col < columns && col >=0)
                buttonArray[row][col].setIcon(image);
        }
      //-------------------------------------------------------------------------------------------
      //   A method to check to see if an image (playing piece) contains exists on the board
      //-------------------------------------------------------------------------------------------
         public boolean containsToken(int row, int col)
           {
               if (buttonArray[row][col].getIcon() != null) return true; else return false;

        }

    }

和我的司机一起关闭一切

        //********************************************************************
        //  LayoutDemo.java       Java Foundations
        //
        //  Driver for the bugs game
        //********************************************************************

        import javax.swing.*;

        public class BugsDriver
        {
           //-----------------------------------------------------------------
           //  Sets up a frame containing a tabbed pane. The panel on each
           //  tab demonstrates a different layout manager.
           //-----------------------------------------------------------------
           public static void main(String[] args)
           {
              JFrame frame = new JFrame("Move the Bugs");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              BorderPanel bugsGame = new BorderPanel();


              frame.getContentPane().add(bugsGame);

              frame.pack();
              frame.setVisible(true);
           }
        }

【问题讨论】:

标签: java arrays nullpointerexception logical-operators


【解决方案1】:

== - 如果两个对象引用相同的引用,则返回 true .equals - 如果 String 对象表示与此对象相同的字符序列,则返回 true。

更改此部分:

if (direction == "NorthEast" && column == bugGrid.getBoardColumns()-1)
        direction = "SouthWest";
    else if (direction == "SouthWest" && column == 0)
        direction = "NorthEast";

    if (direction == "SouthWest")

【讨论】:

    【解决方案2】:

    我自己想出来的。它根本不需要处理 == 或 .equals。虽然我下次会接受这个建议。问题是我必须更新对象选择方向的参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多