Mac70
|
 |
«
Posted
2013-01-15 14: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.
|