我有一个超出界限的对象(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。 BTW第79行是发布的第3个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);
}
}
答案 0 :(得分:0)
== - 如果两个对象引用相同的引用,则返回true .equals - 如果String对象表示与此对象相同的字符序列,则返回true。
更改此部分:
if (direction == "NorthEast" && column == bugGrid.getBoardColumns()-1)
direction = "SouthWest";
else if (direction == "SouthWest" && column == 0)
direction = "NorthEast";
if (direction == "SouthWest")
答案 1 :(得分:0)
自己想出来。它根本不需要处理==或.equals。虽然我下次会接受这个建议。问题是我必须更新对象选择方向的参数。