I have mad a list of objects, in an array, and am attempting to remove some objects at specific points in the array. The problem is I need to remove the visual representation of the object on the screen, and am getting a nullpointer exception. I don't see how it could be reading a null...
This is for a minesweeper game, in which when the mouse is clicked, I check to see if there are mines surrounding the clicked part, if not then I want to remove all surrounding buttons, as it happens in the actual game.
there is a lot of commented code I sorry, I was just testing stuff and left it there. This is the code for the buttons, the error is thrown when I remove the list (the list will have all the elements when complete, but when testing it didn't work so I didn't continue adding), at the last line.
This is where I make the actual array (the world class), again sorry about the commented code, ill get rid of it later.
So how do I remove these objects upon clicking the button???
Thanks
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
import java.lang.Object ;
import java.util.Arrays ;
/**
* Write a description of class button here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class button extends Actor
{
/**
* Act - do whatever the button wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public boolean kill = false;
public int mousex;
public int mousey;
MouseInfo mouse = Greenfoot.getMouseInfo();
public int next = 0;
public int next1 = 0;
public int row;
public int column;
public int arrayNum = 0;
public int check = 0;
one one = new one();
MyWorld world = new MyWorld();
button[] firstarray = world.getButtonArray();
List<button> butlist = new ArrayList<button>();
public void act()
{
gameColumn();
gameRow();
death();
checkRight();
checkLeft();
checkUp();
checkDown();
finals();
}
public button(boolean kill)
{
this.kill = kill;
}
public void gameColumn()
{
if (Greenfoot.mouseClicked(this))
{
MouseInfo mouse = Greenfoot.getMouseInfo();
mousex = mouse.getX();
//System.out.println("X: " + mousex);
//System.out.println("Y: " + mousey);
//getWorld().removeObject(firstarray[12]);
System.out.println("X: " + mousex);
next = mousex;
next = next - 344;
column = next / 16;
column = 16 - column;
//column = (37 % next) + 1;
System.out.println ( "Column: " + column);
}
}
public void gameRow()
{
if (Greenfoot.mouseClicked(this))
{
MouseInfo mouse = Greenfoot.getMouseInfo();
mousey = mouse.getY();
System.out.println("Y: " + mousey);
next1 = mousey;
next1 = next1 - 128;
row = (next1 / 16) ;
row = (16 - row) ;
System.out.println ( "Row: " + row);
arrayNum = 16 * (row - 1);
arrayNum = (arrayNum + column) - 1;
System.out.println( "Array: " + arrayNum);
}
}
public void death()
{
if (Greenfoot.mouseClicked(this) && firstarray[arrayNum].kill != true)
{
getWorld().removeObject(firstarray[arrayNum]);
System.out.println(firstarray[arrayNum].kill);
}
}
public void checkRight()
{
//the moment there is a mine in the radius all around, then stop removing. so
//if no mines, remove all, if one then dont remove just put number, if no number can go then
// remove, or just put numbers before hand, and where there is no number then remove
//EZ?
if (Greenfoot.mouseClicked(this) && firstarray[arrayNum - 1].kill != true && firstarray[arrayNum].kill != true)
{
//System.out.println ("REAL: " + arrayNum );
check++;
//getWorld().removeObject(firstarray[arrayNum]);
butlist.add(firstarray[arrayNum - 1]);
}
}
public void checkLeft()
{
if (Greenfoot.mouseClicked(this) && firstarray[arrayNum + 1].kill != true && firstarray[arrayNum].kill != true)
{
//System.out.println ("REAL: " + arrayNum );
check++;
//getWorld().removeObject(firstarray[arrayNum]);
}
}
public void checkUp()
{
if (Greenfoot.mouseClicked(this) && firstarray[arrayNum + 16].kill != true && firstarray[arrayNum].kill != true && firstarray[arrayNum + 15].kill != true && firstarray[arrayNum + 17].kill != true)
{
//System.out.println ("REAL: " + arrayNum );
check+= 3;
//getWorld().removeObject(firstarray[arrayNum]);
}
}
public void checkDown()
{
if (Greenfoot.mouseClicked(this) && firstarray[arrayNum - 16].kill != true && firstarray[arrayNum].kill != true && firstarray[arrayNum - 17].kill != true && firstarray[arrayNum - 15].kill != true)
{
//System.out.println ("REAL: " + arrayNum );
check+= 3;
//System.out.println ("IM HERE!" + " " + check);
//getWorld().removeObject(firstarray[arrayNum]);
}
}
public void finals()
{
if (check == 8)
{
int arrayNum2 = arrayNum - 16;
System.out.println ("IM HERE!" + arrayNum );
int newx = firstarray[arrayNum + 16].getX();
int newy = firstarray[arrayNum + 16].getY();
//one = getWorld().getObjectsAt(newx, newy, button.class);
//GET OBJECTS AT LOCATION, THEN ROMOVE IT! USE ARRAY X AND Y +- 16
//fill( firstarray[], arrayNum-1, arrayNum-2, one);
getWorld().removeObjects(butlist);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld extends World
{
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 384, 1);
}
public int backAmount = 0;
public int maxBack = 256;
public int num = 0;
public int newn = 0;
public int yBack = 392;
Background[] b = new Background[256];
static button[] cover = new button[256];
public int xBack = 592;
public int xMine;
public int yMine;
public int mineLocation;
public int trueAmount = 0;
public void act()
{
//xBack < 20
//System.out.println("Inside act method");
set();
butt();
//for (int j = 0; j <= 40; j++)
//{
mine();
//}
}
public void set()
{
for (backAmount = 0; backAmount <= maxBack; backAmount++)
{
if (backAmount % 16 == 0)
{
xBack = 592;
yBack = yBack - 16;
// backAmount = 1;
}
//System.out.println("num =" + num);
if (num <= 255)
{
b[num] = new Background();
addObject(b[num], xBack, yBack);
num++;
}
else if (num >= 256)
{
break;
}
xBack -= 16;
}
}
public void butt()
{
//if (getObjects(Mine.class).size() >= 40)
if (getObjects(Background.class).size() >= 255)
{
xBack = 592;
yBack = 392;
for (backAmount = 0; backAmount <= maxBack; backAmount++)
{
if (backAmount % 16 == 0)
{
xBack = 592;
yBack = yBack - 16;
// backAmount = 1;
}
//System.out.println("num =" + num);
if (newn <= 255)
{
cover[newn] = new button(false);
addObject(cover[newn], xBack, yBack);
// if (getObjectsAt(xBack, yBack, Mine.class) == null)
// {
//System.out.println("here");
// cover[newn] = new button(false);
// addObject(cover[newn], xBack, yBack);
//}
// else if (getObjectsAt(xBack, yBack, Mine.class) != null)
//{
// cover[newn] = new button(true);
// addObject(cover[newn], xBack, yBack);
// }
newn++;
}
else if (newn >= 256)
{
break;
}
xBack -= 16;
}
}
}
public void mine()
{
//System.out.println("Inside mine method");
//if (getObjects(Mine.class).size() <= 40)
//for (int d = 0; d <= 1; d++)
//{
if (trueAmount <= 40)
{
mineLocation = Greenfoot.getRandomNumber(255);
if (cover[mineLocation].kill != true)
{
cover[mineLocation].kill = true;
Mine mine = new Mine();
addObject (mine, cover[mineLocation].getX(), cover[mineLocation].getY());
}
else if (cover[mineLocation].kill == true)
{
}
trueAmount++;
//removeObject(cover[mineLocation]);
//xMine = b[mineLocation].getX();
//yMine = b[mineLocation].getY();
//Mine mine = new Mine();
//addObject(mine, xMine, yMine);
//button ba = new button(true);
//addObject(ba, xMine, yMine);
//}
}
}
public button[] getButtonArray()
{
return cover;
}
}
