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:
- Buildings superclass:
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;
}
}
- Specified building:
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++;
}
}
}
- Buildings manager:
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;
}
}
}
}
}
- GameTerrain (only update()):
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();
}
- Resources class:
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.