0

I have an object (BigCrawler) that is going out of bounds and giving me a null pointer exception every time it hits the 0 and 7 in my 8x8 grid on the north and south end but not the east and west sides. My NPE is

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)

I'm positive it's something within my logic. I have 3 other objects, they instantiate and 2 move east and west while the other one moves north and south each time I press the move button. They each work just fine but for some reason whenever my BigCrawler object hits the north or south end it won't turn back and go the other way it'll just keep giving NPEs. BTW line 79 is the 3rd if statement posted. I can post any code that is required but I believe it's within the posted area.

My 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++;}
    }
   }


}

and my 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;

        }

    }

and my driver to set everything off

        //********************************************************************
        //  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);
           }
        }

2 Answers2

0

== - returns true if it two object is referring to same references .equals - returns true if the String object that represents the same sequence of characters as this object.

Change this portion:

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

    if (direction == "SouthWest")
mel3kings
  • 8,857
  • 3
  • 60
  • 68
0

Figured it out myself. It didn't have to deal with the == or .equals at all. Though I'll take that advice for next time. Problem was that I had to update the parameters in which the object would chose it's direction.