Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Malformed expression: "(ERROR)" - get value from object in list  (Read 1340 times)
0 Members and 1 Guest are viewing this topic.
Offline Mac70
« Posted 2013-01-15 13:58:35 »

Like in topic. I have problem with getting primitive value from ArrayList. Everything should work, IDE show no errors, all values are initialized and I can see them in debugger (in ArrayList object list), but when I am trying to do anything with this object I am getting NullPointerException. Also, when I am hovering primitive while debuging I see Malformed expression: "(ERROR)" instead of value.

Classes that could potentially be causing the problem:

1. Buildings superclass:

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  
package Objects.Buildings;

import Lib.Graphics.multiTex;
import Objects.GameTerrain;
import Objects.Tiles.Background.BlankTile;
import Objects.Tiles.BuildingTile;

public abstract class Buildings {
   
    public int startWidth;
    public int endWidth;
    public int startHeight;
    public int endHeight;
    public int ID;
   
    public int population=1;
   
    private GameTerrain father;
    private multiTex tex;
   
    private boolean deleting = false;
   
    public Buildings(int id, int startWidth, int startHeight, int endWidth, int endHeight, multiTex tex, GameTerrain father) {
        this.ID = id;
        this.startWidth = startWidth;
        this.startHeight = startHeight;
        this.endWidth = endWidth;
        this.endHeight = endHeight;
        this.tex = tex;
        this.father = father;
        build();
    }
   
    private void build() {
        int texx=0;
        int texy=(endHeight-1)-startHeight;
        for (int i=startWidth; i<endWidth; i++) {
            for (int i2=startHeight; i2<endHeight; i2++) {
                father.tiles[i][i2] = new BuildingTile(i, i2, ID, tex.getTex(texx, texy), father);
                texy--;
            }
            texy=(endHeight-1)-startHeight;
            texx++;
        }
    }
   
    public void update() {
        delete();
    }
   
    private void delete() {
        if (deleting) {
            for (int i=startWidth; i<=endWidth; i++) {
                for (int i2=startHeight; i2<=endHeight; i2++) {
                    father.tiles[i][i2] = new BlankTile(i, i2, father);
                }
            }
        }
        father.buildings.set(ID, null);
    }
   
    public void deleteInit() {
        deleting = true;
    }
   
}


2. Specified building:

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  
package Objects.Buildings.Houses;

import Lib.Graphics.multiTex;
import Objects.Buildings.Buildings;
import Objects.GameTerrain;

public class SmallHouse extends Buildings {
   
    int maxPop;
    int happiness;
    int level;
   
    public SmallHouse(int id, int startWidth, int startHeight, int endWidth, int endHeight, multiTex tex, GameTerrain father) {
        super(id, startWidth, startHeight, endWidth, endHeight, tex, father);
        maxPop = 20;
        happiness = 50;
        level = 1;
    }
   
    @Override public void update() {
        popManager();
        super.update();
    }
   
    private void popManager() {
        if (population<maxPop && Math.random()>=0.95) {
            population++;
        }
    }
   
}


3. Buildings manager:

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  
package Objects;

import Core.Camera.Camera;
import Core.Graphics.LT;
import Core.Logic.Logic;
import Core.Switch;
import Lib.Graphics.Texture;
import Lib.Graphics.multiTex;
import Objects.Buildings.Buildings;
import Objects.Buildings.Houses.SmallHouse;
import Objects.Tiles.Background.BlankTile;

public class BuildingsManager {
   
    GameTerrain father;
    String buildingName;
    private int tilex;
    private int tiley;
    multiTex toDraw;
    boolean allowBuild = true;
   
    BuildingsManager(GameTerrain father) {
        this.father = father;
    }
   
    public void update() {
       checkHouses();
       if (toDraw!=null) {
           checker();
       }
    }
   
    public Buildings endAction() {
        if (toDraw!=null && allowBuild) {
            endBuildingSwitches();
            return buildingNameChecker();
        }
        else {
            return null;
        }
    }
   
    private void endBuildingSwitches() {
        Switch.groupIcon.house.small = false;
        Switch.groupIcon.house.medium = false;
        Switch.groupIcon.house.high = false;
        Switch.groupIcon.trade.small = false;
        Switch.groupIcon.trade.small = false;
        Switch.groupIcon.trade.small = false;
    }
   
    private Buildings buildingNameChecker() {
        switch (buildingName) {
            case "small house":
                return new SmallHouse(father.buildings.size(), tilex, tiley, tilex+toDraw.width, tiley+toDraw.height, toDraw, father);
            default:
                return null;
        }
    }
   
    private void checkHouses() {
        if (Switch.groupIcon.house.small) {
            buildingName = "small house";
            toDraw = LT.building.smallHouse.upgrade1;
        }
    }
   
    public void draw() {
        if (toDraw!=null) {
            for (int i=0; i<toDraw.width; i++) {
                for (int i2=0; i2<toDraw.height; i2++) {
                    if (allowBuild) {
                        Texture.draw(toDraw.getTex(i, i2), (tilex+i)*20, (tiley+toDraw.height-i2-1)*20, 0, 0.5f, 1, 0.5f, 0.5f);
                    }
                    else {
                        Texture.draw(toDraw.getTex(i, i2), (tilex+i)*20, (tiley+toDraw.height-i2-1)*20, 0, 1, 0.5f, 0.5f, 0.5f);
                    }
                }
            }
            allowBuild = true;
            toDraw=null;
        }
    }
   
    private void checker() {
        tilex = GameTerrain.currTile.x;
        tiley = GameTerrain.currTile.y;
         
        if (toDraw.width%2==0) {
            if ((Logic.mouse.x+Camera.cameraX)%20>=10) {
                tilex--;
            }
        }
        else {
            tilex-=toDraw.width/2;
        }
        if (toDraw.height%2==0) {
            if ((Logic.mouse.y+Camera.cameraY)%20>=10) {
                tiley--;
            }
        }
        else {
            tiley-=toDraw.height/2;
        }
       
        for (int i=0; i<toDraw.width; i++) {
            for (int i2=0; i2<toDraw.height; i2++) {
                if (!(father.tiles[tilex+i][tiley+toDraw.height-i2-1] instanceof BlankTile)) {
                    allowBuild=false;
                }
            }
        }
    }
   
}


4. GameTerrain (only update()):

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  
    public void update() {
        me.update();
        currTile.x = (int) Math.floor((Logic.mouse.x+Camera.cameraX+10)/20);
        currTile.y = (int) Math.floor((Logic.mouse.y+Camera.cameraY+10)/20);
       
        for (int i=0; i<buildings.size(); i++) {
            buildings.get(i).update();
        }
       
        if (!cancelActions) {
            manager.update();
            if (me.mousePressed && !me.GUIHovered) {
                ignoreGUI = true;
                iGUI = true;
            }
            if (me.mousePressed && ignoreGUI) {
                roadMaker.update();
                deleter.update();
            }
            if (me.mouseClicked) {
                if (manager.endAction()!=null) {
                    buildings.add(manager.endAction());
                }
                roadMaker.endAction();
                deleter.endAction();
            }
            else if (!me.mousePressed) {
                roadMaker.clear();
                deleter.clear();
                ignoreGUI=false;
                iGUI = false;
            }
        }
        else {
            roadMaker.clear();
            ignoreGUI=false;
            iGUI = false;
        }
       
        if (me.mousePressed && me.cancelClicked) {
            cancelActions=true;
        }
        else if (!me.mousePressed) {cancelActions=false;}
       
        resources.update();
       
    }


5. Resources class:

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  
package Objects;

public class Resources {
   
    public static long population = 0;
    public static long money = 0;
    public static long oil = 0;
    public static long steelOre = 0;
    public static long steel = 0;
    public static long fastanicOre = 0;
    public static long fastanic = 0;
   
    private GameTerrain father;
   
    public Resources(GameTerrain father) {
        this.father = father;
    }
   
    public void update() {
        population = 0;
        for (int i=0; i<father.buildings.size(); i++) {
            population += father.buildings.get(i).population;
        }
    }
   
    public void draw() {
       
    }
   
}


Game crashes at father.buildings.get(i).population; in Resources class and buildings.get(i).update(); in GameTerrain class.

Check out my Devblog! Smiley
Offline actual

JGO Coder


Medals: 24



« Reply #1 - Posted 2013-01-15 17:56:23 »

In Buildings.delete() method you are setting an index to null instead of using remove(). If you aren't closing the gaps, you will have null values that will give you an error when you try to iterate over everything.




Offline Mac70
« Reply #2 - Posted 2013-01-15 18:04:36 »

Very big thanks! I was looking for a bug in a completely different place, didn't expected that it could be there. I will have to be more cautious next time. Wink

Check out my Devblog! Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (21 views)
2014-11-26 15:22:04

toopeicgaming1999 (20 views)
2014-11-26 15:20:36

toopeicgaming1999 (7 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (24 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!