Results 41 to 53 of 53
Thread: NullPointerException
- 03-20-2012, 05:47 PM #41
Member
- Join Date
- Mar 2012
- Posts
- 50
- Rep Power
- 0
Re: NullPointerException
This is what I have now:
Main:
Unit:Java Code:public class Main { public static void main (String[] args) { Unit unit = UnitList.getInstance().legionnaire; } }
UnitList:Java Code:public class Unit { String title; Tribe tribe; }
Legionnaire:Java Code:public final class UnitList { private static final UnitList instance = new UnitList(); public final Unit legionnaire; public final Unit phalanx; public final Unit[][] units; public final static int ROMAN = 0, GAUL = 1; private UnitList() { legionnaire = new Legionnaire(); phalanx = new Phalanx(); units = new Unit[2][1] units[ROMAN][0] = legionnaire; units[GAUL][1] = phalanx; } public static getInstance() { return instance; } }
Phalanx:Java Code:public final class Legionnaire extends Unit { public Legionnaire() { title = "Legionnaire"; tribe = TribeList.getInstance().roman; } }
Tribe:Java Code:public final class Phalanx extends Unit { public Phalanx() { title = "Phalanx"; tribe = TribeList.getInstance().gaul; } }
Roman:Java Code:public class Tribe { Unit[] tribeUnits = new Unit[1]; }
Gaul:Java Code:public class Roman extends Tribe { public Roman() { for (int i = 0; i < tribeUnits.length; i++) tribeUnits[i] = UnitList.getInstance().units[UnitList.ROMAN][i]; } }
TribeList:Java Code:public class Gaul extends Tribe { public Gaul() { title = "Gaul"; for (int i = 0; i < tribeUnits.length; i++) tribeUnits[i] = UnitList.getInstance().units[UnitList.GAUL][i]; } }
Building:Java Code:public class TribeList { private static final TribeList instance = new TribeList(); public final Tribe roman, gaul; private UnitList() { roman = new Roman(); gaul = new Gaul(); } public static getInstance() { return instance; } }
BuildingList:Java Code:public class Building { Unit[] trainedUnits = new Unit[1]; Tribe tribe; Building requiredBuilding; }
Academy:Java Code:public class BuildingList { private static final BuildingList instance = new BuildingList(); public final Building academy, mainBuilding; private BuildingList() { academy = new Academy(); mainBuilding = new MainBuilding(); } public static getInstance() { return instance; } }
MainBuilding:Java Code:public class Academy { public Academy() { trainedUnits[0] = UnitList.getInstance().legionnaire; tribe = TribeList.getInstance().roman; requiredBuilding = BuildingList.getInstance().mainBuilding; } }
Java Code:public class MainBuilding { public MainBuilding() { trainedUnits[0] = UnitList.getInstance().phalanx; tribe = TribeList.getInstance().gaul; } }
And I'm getting the same NPE that I was getting with static List classes:
Code:Exception in thread "main" java.lang.ExceptionInInitializerError at troopInfo.Legionnaire.<init>(Legionnaire.java:6) at troopInfo.UnitList.<init>(UnitList.java:14) at troopInfo.UnitList.<clinit>(UnitList.java:4) at troopInfo.UnitSelection.refreshChoices(UnitSelection.java:19) at troopInfo.UnitSelection.<init>(UnitSelection.java:11) at troopInfo.TroopPanel.<clinit>(TroopPanel.java:8) at main.MyTabbedPane.<init>(MyTabbedPane.java:8) at main.MainWindow.<init>(MainWindow.java:18) at main.MainWindow.main(MainWindow.java:25) Caused by: java.lang.NullPointerException at tribeInfo.Roman.<init>(Roman.java:7) at tribeInfo.TribeList.<init>(TribeList.java:9) at tribeInfo.TribeList.<clinit>(TribeList.java:4) ... 9 more
- 03-20-2012, 06:13 PM #42
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: NullPointerException
THere's no need to have the ROMAN units stored in the Roman instance.
Indeed, any of the tribe units.
You can get them anytime by doing:
And if you aren't storing them, then you won't hit the problem of trying to use them in the constructor before they are populated.Java Code:UnitList.getInstance().units[UnitList.ROMAN];
Essentially, you need to plan the model such that Something is the owner of a Thing.
Other classes will then request this data from the owner of the Thing as they need it, rather than storing their own copies of the Thing.Please do not ask for code as refusal often offends.
- 03-20-2012, 07:03 PM #43
Member
- Join Date
- Mar 2012
- Posts
- 50
- Rep Power
- 0
Re: NullPointerException
OK, I removed that now, but now I'm getting a NPE in the Academy class:
BuildingList's constructor calls Academy and Academy calls BuildingList.getInstance().mainBuilding, which I guess hasn't been initiated yet. But the problem persists even if I move the mainBuilding line above the academy line in the constructor (in which case it should be initiated before academy, no?):Code:Exception in thread "main" java.lang.ExceptionInInitializerError at tribeInfo.Roman.<init>(Roman.java:7) at tribeInfo.TribeList.<init>(TribeList.java:9) at tribeInfo.TribeList.<clinit>(TribeList.java:4) at troopInfo.Legionnaire.<init>(Legionnaire.java:6) at troopInfo.UnitList.<init>(UnitList.java:14) at troopInfo.UnitList.<clinit>(UnitList.java:4) at troopInfo.UnitSelection.refreshChoices(UnitSelection.java:19) at troopInfo.UnitSelection.<init>(UnitSelection.java:11) at troopInfo.TroopPanel.<clinit>(TroopPanel.java:8) at main.MyTabbedPane.<init>(MyTabbedPane.java:8) at main.MainWindow.<init>(MainWindow.java:18) at main.MainWindow.main(MainWindow.java:25) Caused by: java.lang.NullPointerException at buildingInfo.Academy.<init>(Academy.java:6) at buildingInfo.BuildingList.<init>(BuildingList.java:9) at buildingInfo.BuildingList.<clinit>(BuildingList.java:4) ... 12 more
Java Code:public class BuildingList { private static final BuildingList instance = new BuildingList(); public final Building academy, mainBuilding; private BuildingList() { mainBuilding = new MainBuilding(); academy = new Academy(); } public static getInstance() { return instance; } }
- 03-21-2012, 09:51 AM #44
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: NullPointerException
Why does Academy need access to the MainBuilding during contruction?
Please do not ask for code as refusal often offends.
- 03-21-2012, 10:37 AM #45
Member
- Join Date
- Mar 2012
- Posts
- 50
- Rep Power
- 0
Re: NullPointerException
Well, mainBuilding is the requirement for academy, so the requiredBuilding field is set to mainBuilding in Academy's constructor.
mainBuilding is in turn initiated as new MainBuilding() .
- 03-21-2012, 10:51 AM #46
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: NullPointerException
I think you might need to sit down and try and detach your dependencies somehow.
You may need a two-pass construction in this case.
First pass to actually make the objects, second to assign the requirements.
I've had to do similar stuff before when mucking about with these sorts of strategy game ideas.Please do not ask for code as refusal often offends.
- 03-21-2012, 11:16 AM #47
Member
- Join Date
- Mar 2012
- Posts
- 50
- Rep Power
- 0
Re: NullPointerException
Is it possible create a reference to an object without actually constructing that object? Cause that would solve my problems, I believe.
- 03-21-2012, 11:26 AM #48
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: NullPointerException
Nope.
I would suggest:
I've called it load, because I tend to load stuff from file, but the concept is the same.Java Code:public class Something { public Something() { // Basic constructor, possibly with some data set up here } public void loadData() { // Do the rest in here, that depends on other classes. } } public class GameLoader { public GameLoader() { // NOthing in here } pubic void loadGame() { // Create everything in here then call the load methods Something s = new Something(); ... etc... s.loadData(); } }
The advantage of having a GameLoader is you can then control the order of things, without relying purely on constructors.Please do not ask for code as refusal often offends.
- 03-21-2012, 01:17 PM #49
Member
- Join Date
- Mar 2012
- Posts
- 50
- Rep Power
- 0
Re: NullPointerException
Interesting.. I'll try something like this when I get home.
- 03-21-2012, 10:44 PM #50
Member
- Join Date
- Mar 2012
- Posts
- 50
- Rep Power
- 0
Re: NullPointerException
Wait, so in the GameLoader, the code can't be in the constructor? I was going to simply write the code that you put in loadGame into UnitList's constructor.
Something like this:
Entity:Java Code:public final class UnitList { private static final UnitList instance = new UnitList(); public final Unit legionnaire; public final Unit phalanx; public final Unit[][] units; public final static int ROMAN = 0, GAUL = 1; private UnitList() { legionnaire = new Legionnaire(); phalanx = new Phalanx(); units = new Unit[2][1]; units[ROMAN][0] = legionnaire; units[GAUL][0] = phalanx; for (int i = 0; i < units.length; i++) for (int j = 0; j < units[i].length; j++) units[i][j].loadData(); } public static getInstance() { return instance; } }
Unit:Java Code:public abstract class Entity { protected String title; public String toString() { return title; } public void loadData(); }
Java Code:public abstract class Unit extends Entity { protected Tribe tribe; }
Legionnaire:
Java Code:public final class Legionnaire extends Unit { public Legionnaire() { title = "Legionnaire"; } public void loadData() { tribe = TribeList.getInstance().roman; } }
Phalanx:
Java Code:public final class Phalanx extends Unit { public Phalanx() { title = "Phalanx"; } public void loadData() { tribe = TribeList.getInstance().gaul; } }
And something similar for buildings and tribes.Last edited by Mate de Vita; 03-21-2012 at 11:55 PM.
- 03-22-2012, 09:59 AM #51
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: NullPointerException
So now the UnitList creates a skeleton set of Units, which are then populated, except you might still clash since the TribeList class also needs to be constructed.
It looks like you should be OK, though, since none of the Units use TribeList until the loadData is called, and by that point UnitList has a complete set of (incomplete, but existing) Units.Please do not ask for code as refusal often offends.
- 03-22-2012, 11:03 AM #52
Member
- Join Date
- Mar 2012
- Posts
- 50
- Rep Power
- 0
Re: NullPointerException
All right, I managed to successfully run the program now without any NPEs or similar problems. I have a few problems with my Swing GUI, but I'll open a new thread for those, since it's a completely different subject.
Thanks for all the help, really appreciate it.
- 03-22-2012, 11:27 AM #53
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
NullPointerException
By Diz in forum New To JavaReplies: 10Last Post: 05-13-2011, 02:58 AM -
NullPointerException
By GPB in forum New To JavaReplies: 8Last Post: 02-21-2010, 03:05 PM -
NullPointerException
By Juuno in forum New To JavaReplies: 1Last Post: 02-11-2010, 05:43 PM -
NullPointerException help?
By fab5freddy in forum New To JavaReplies: 2Last Post: 02-04-2010, 08:26 PM -
NullPointerException help me!
By phancuong87 in forum New To JavaReplies: 4Last Post: 01-19-2010, 04:01 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks