我有一個對象(BigCrawler)超出界限,並且每次在我的8x8網格中點擊0和7時,在北和南端但不是東西兩側都會出現空指針異常。我的NPE是爲什麼我的數組對象在我的8x8網格內移動出界?
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行是發佈的第三條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);
}
}
在字符串上使用'.equals'而不是'=='。 – Maroun
Java是否改變了'=='運算符對字符串的作用? [否則你不應該使用它](http://stackoverflow.com/a/513839/440558)。 –
可能的重複[如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) –