This site requires JavaScript, please enable it in your browser!
Greenfoot back
BrownDwarf
BrownDwarf wrote ...

2017/6/29

Removing objects from an array

BrownDwarf BrownDwarf

2017/6/29

#
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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);
        }
        
    }
}
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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;
    }
}
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
BrownDwarf BrownDwarf

2017/6/29

#
line 119 is where I add an object to the list
danpost danpost

2017/6/29

#
Before answering your question, here are a couple of helpful things:- (a) you never have to import java.lang.Object; (b) class names should (by convention) start with an uppercase letter ( 'button' should be 'Button' ); (c) it does not make sense to get a MouseInfo object before a button is created (remove assignment part of line 21 in button class); (d) similar to (c), getWorld must return null when 'world' at line 30 in the button class is assigned a value (remove assignment part); (e) as a consequence of (d), you cannot perform the assignment on line 31 either (remove assignment part); These are just a few individual things that can be done.. There is a real issue in general with the way you are going about everything; however, it is too large and would require a total re-vamping of the classes to fix. For now, as far as your error, half the fix would be performed by the above. The other half involves making those assignments at the proper time (that is, when the button is added into the world). Add the following method to the class and make those assignments removed from the above it this method:
1
2
3
4
protected void addedToWorld(World world)
{
    /* assign fields ( in (d) and (e) above) here */
}
Note that the button is being added into the world just before this method is executed and no mouse actions could yet be detected on this button even at this point.
You need to login to post a reply.