Results 1 to 20 of 53
Thread: NullPointerException
- 03-13-2012, 02:46 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 50
- Rep Power
- 0
NullPointerException
Using the Eclipse Run button, I tried to run my program and came up with this error:
After a quick google search I came up with this. However I can't find a reason why any of the five things listed as the most likely reasons for a NullPointerException would be happening in my particular case.Code:Exception in thread "main" java.lang.ExceptionInInitializerError at troopInfo.Legionnaire.<init>(Legionnaire.java:6) 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:8) at tribeInfo.TribeList.<clinit>(TribeList.java:4) ... 8 more
The line that appears to be causing the problem in my program is in the class Roman (in the package tribeInfo):
Java Code:for (int i = 0; i < tribeUnits.length; i++) tribeUnits[i] = troopInfo.UnitList.units[troopInfo.UnitList.ROMAN][i];
In the class Tribe (a superclass of the class Roman), I have the following line to define and initiate the array tribeUnits:
Java Code:protected troopInfo.Unit[] tribeUnits = new troopInfo.Unit[10];
This is the class troopInfo.UnitList:
Java Code:package troopInfo; public final class UnitList { public final static Unit legionnaire = new Legionnaire(), praetorian = new Praetorian(), imperian = new Imperian(), equitesLegati = new EquitesLegati(), equitesImperatoris = new EquitesImperatoris(), equitesCaesaris = new EquitesCaesaris(), batteringRam = new BatteringRam(), fireCatapult = new FireCatapult(), senator = new Senator(), romanSettler = new RSettler(); public final static Unit phalanx = new Phalanx(), swordsman = new Swordsman(), pathfinder = new Pathfinder(), theutatesThunder = new TheutatesThunder(), druidrider = new Druidrider(), haeduan = new Haeduan(), gaulRam = new GRam(), trebuchet = new Trebuchet(), chieftain = new Chieftain(), gaulSettler = new GSettler(); public final static Unit clubswinger = new Clubswinger(), spearman = new Spearman(), axeman = new Axeman(), scout = new Scout(), paladin = new Paladin(), teutonicKnight = new TeutonicKnight(), teutonRam = new TRam(), catapult = new Catapult(), chief = new Chief(), teutonSettler = new TSettler(); public final static Unit[][] units = { {legionnaire, praetorian, imperian, equitesLegati, equitesImperatoris, equitesCaesaris, batteringRam, fireCatapult, senator, romanSettler}, {phalanx, swordsman, pathfinder, theutatesThunder, druidrider, haeduan, gaulRam, trebuchet, chieftain, gaulSettler}, {clubswinger, spearman, axeman, scout, paladin, teutonicKnight, teutonRam, catapult, chief, teutonSettler} }; public final static int ROMAN = 0, GAUL = 1, TEUTON = 2; }
What is the problem?Last edited by Mate de Vita; 03-13-2012 at 02:48 PM.
- 03-13-2012, 03:00 PM #2
Re: NullPointerException
I suggest you step through this with a debugger, or even just add some print statements, to figure out which reference is null.
For example, say I'm getting a NPE on this line:
The best way to figure out which part of that is null is to step through it with a debugger. Or I could add some print statements to test:Java Code:something.getSomethingElse().andAnotherThing().doSomething();
Java Code:System.out.println("Is something null? " + (something == null)); System.out.println("Is something.somethingElse() null? " + (something.somethingElse() == null)); System.out.println("Is something.somethingElse().andAnotherThing() null? " + (something.somethingElse()andAnotherThing() == null));How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-13-2012, 04:14 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 50
- Rep Power
- 0
Re: NullPointerException
I ran the debugger, but I can't find a way to check the values of objects with the final modifier (in my case the Unit objects in UnitList and Tribe objects in TribeList). They don't show up under variables.
- 03-13-2012, 04:16 PM #4
Re: NullPointerException
I'm not sure why you need to. Run the debugger until just before the line that throws the Exception is executed. What values are you about to reference? Which one of them is null?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-13-2012, 04:24 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 50
- Rep Power
- 0
Re: NullPointerException
Because the way this looks, those final objects are for some reason the ones that are null.
The code of the Roman class is:
At the last step before the exception is thrown, the title in Roman is set to "Roman", but all the tribeUnits fields are null, and i is 0. All the fields of troopInfo.UnitList.units are supposed to be final objects as seen in the OP. troopInfo.UnitList.ROMAN can't be null because it's an integer, and I don't think troopInfo and troopInfo.UnitList can be null, since they're package and class respectively, not objects.Java Code:package tribeInfo; public class Roman extends Tribe { public Roman() { title = "Roman"; for (int i = 0; i < tribeUnits.length; i++) tribeUnits[i] = troopInfo.UnitList.units[troopInfo.UnitList.ROMAN][i]; tribeBuilding = buildingInfo.BuildingList.horseDrinkingTrough; } }
- 03-13-2012, 04:34 PM #6
Re: NullPointerException
Without an SSCCE, I'm only guessing. But you might also split that for loop up into multiple lines, something like this:
When do you initialize your array? How many indexes does the array you're accessing have? Which index are you trying to access?Java Code:for(whatever){ //do something }How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-13-2012, 04:51 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 50
- Rep Power
- 0
Re: NullPointerException
The array is initialized in the UnitList class, as seen in the last piece of code in my first post (though a bit of it got cut off). AFAIK it should be initialized at compile time, no?
It's basically a [3][10] array of final Unit objects, each row representing the 10 Units of the respective tribe (row 0 the Roman units, row 1 the Gaul units, and row 2 the Teuton units).
What I'm trying to do is copy the Units from the row 0 (the Roman units) into the array tribeUnits in class Roman.
troopInfo.UnitList.ROMAN is simply the constant 0, which I used to clarify that I'm trying to access the row 0 (which contains Roman units).
- 03-13-2012, 05:07 PM #8
Re: NullPointerException
Right, and what happens when you check everything for null? Something on that line is null, but which thing is it? We can't proceed until we find that out, and I can't do anything on my end without an SSCCE.
Nothing is initialized at compile time. Initialization is a run-time action.
Sorry, but that's about all I can do without an SSCCE.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-13-2012, 05:09 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: NullPointerException
Do as Kevin suggest and do a load of printlns just prior to that loop.
Don't assume everything must be OK, because clearly it isn't.
Until you find out what it is that is null then you'll be flailing.
By the way, according to the above code and stack trace this is the problem line (line 8):
Java Code:tribeBuilding = buildingInfo.BuildingList.horseDrinkingTrough;
Please do not ask for code as refusal often offends.
- 03-13-2012, 06:41 PM #10
Member
- Join Date
- Mar 2012
- Posts
- 50
- Rep Power
- 0
Re: NullPointerException
I inserted the following code before the for loop in question:
And it still says the exception is at line 7, which is for (int i = 0; i < troopInfo.UnitList.units.length; i++).Java Code:for (int i = 0; i < troopInfo.UnitList.units.length; i++) { if (troopInfo.UnitList.units[i] == null) System.out.println (i+" is null"); else System.out.println (i+"isn't null"); }
But I can't do if (troopInfo == null) or if (troopInfo.UnitList == null), as those are illegal expressions. Nor can I do it with troopInfo.UnitList.units.length or i. So the only thing that remains is troopInfo.UnitList.units, which I now checked, and it is null, but I'm not sure what that's supposed to be if not null... Is it supposed to be a pointer to the beginning of the array as it would be in C?
Yeah, I edited the line after I had already copied the error message.
- 03-13-2012, 07:01 PM #11
Re: NullPointerException
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-13-2012, 07:02 PM #12
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Re: NullPointerException
Uh oh, I smell a "pass by value vs. pass by reference vs. pass by pointer to a pointer" debate coming up...
- 03-13-2012, 07:59 PM #13
Member
- Join Date
- Mar 2012
- Posts
- 50
- Rep Power
- 0
- 03-13-2012, 09:16 PM #14
Member
- Join Date
- Mar 2012
- Posts
- 50
- Rep Power
- 0
Re: NullPointerException
OK, I think this'll do, but I can't test it because I'm getting the following error: java.lang.UnsupportedClassVersionError: project1/MainProject : Unsupported major.minor version 51.0
Google says I have to reinstall java, but I don't have time for that right now. Will do it tomorrow though if it's necessary.
Not quite sure how else to send this:
http://speedy.sh/ucBY5/NPE-SSCCE.rar
- 03-13-2012, 11:50 PM #15
Re: NullPointerException
You shouldn't have to send anything- an SSCCE should be easy to copy and paste from the forum into an editor. You should post it exactly like you posted the code above, except it should contain everything we need to understand what's actually going on.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-14-2012, 12:55 AM #16
Member
- Join Date
- Mar 2012
- Posts
- 50
- Rep Power
- 0
Re: NullPointerException
Like this?
Main:
Unit:Java Code:public class Main { public static void main (String[] args) { Unit unit = UnitList.legionnaire; } }
UnitList:Java Code:public class Unit { }
Legionnaire:Java Code:public final class UnitList { public final static Unit legionnaire = new Legionnaire(); public final static Unit phalanx = new Phalanx(); public final static Unit[][] units = { {legionnaire}, {phalanx} }; public final static int ROMAN = 0, GAUL = 1; }
Phalanx:Java Code:public final class Legionnaire extends Unit { public Legionnaire() { } }
Tribe:Java Code:public final class Phalanx extends Unit { public Phalanx() { } }
Roman:Java Code:public class Tribe { protected Unit[] tribeUnits = new Unit[1]; }
Java Code:public class Roman extends Tribe { public Roman() { if (UnitList.units == null) System.out.println ("units is null"); for (int i = 0; i < tribeUnits.length; i++) tribeUnits[i] = UnitList.units[UnitList.ROMAN][i]; } }
-
Re: NullPointerException
Since you initialised these objects in a static context, they won't be initialised by what is written in the constructor.
Originally Posted by Mate de Vita
You can initialise static objects by adding a static block to the class to initialise any variables in a static context, for example:
Java Code:class Legionnaire { int hp; int maxhp; public Legionnaire() { hp=50; maxhp=50; } public int getHP() { return hp; } public int getMaxHP() { return maxhp; } }But with the static block:Java Code:static Legionnaire leg = new Legionnaire(); int hp = leg.getHP(); //null pointer - field hp was not initialised!
Then it should work.Java Code:class Legionnaire { //all same as above ... static { hp=50; maxhp=50; } }
- 03-14-2012, 10:17 AM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: NullPointerException
Does it cause the same problem?
Because I just ran that code (adding 'Roman r = new Roman()' to main()) and it runs with no errors.
What does that even mean?
I have no idea what you are trying to say here.
The OP has simply declaring a load of simple classes to illustrate their problem...which has nothing to do with their initialisation.
The above code works.Please do not ask for code as refusal often offends.
- 03-14-2012, 10:56 AM #19
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.legionnaire; } }
UnitList:Java Code:public class Unit { protected static String title; public String toString() { return (title); } }
Legionnaire:Java Code:public final class UnitList { public final static Unit legionnaire = new Legionnaire(); public final static Unit phalanx = new Phalanx(); public final static Unit[][] units = { {legionnaire}, {phalanx} }; public final static int ROMAN = 0, GAUL = 1, TEUTON = 2; }
Phalanx:Java Code:public final class Legionnaire extends Unit { static { title = "Legionnaire"; } }
Tribe:Java Code:public final class Phalanx extends Unit { static { title = "Phalanx"; } }
Roman:Java Code:public class Tribe { protected static String title; protected static troopInfo.Unit[] tribeUnits = new troopInfo.Unit[10]; public String toString() { return (title); } }
But it still says that units is null.Java Code:public class Roman extends Tribe { static { title = "Roman"; if (troopInfo.UnitList.units == null) System.out.println ("units is null"); for (int i = 0; i < tribeUnits.length; i++) tribeUnits[i] = troopInfo.UnitList.units[troopInfo.UnitList.ROMAN][i]; } }
Hmm.. I'll reinstall java and try running it.Last edited by Mate de Vita; 03-14-2012 at 11:00 AM.
- 03-14-2012, 11:03 AM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- 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