Page 1 of 3 123 LastLast
Results 1 to 20 of 53
Like Tree1Likes

Thread: NullPointerException

  1. #1
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    60
    Rep Power
    0

    Default NullPointerException

    Using the Eclipse Run button, I tried to run my program and came up with this error:
    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
    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.


    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.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default 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:

    Java Code:
    something.getSomethingElse().andAnotherThing().doSomething();
    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:
    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));
    Tolls likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    60
    Rep Power
    0

    Default 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.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default 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!

  5. #5
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    60
    Rep Power
    0

    Default 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:
    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;
    	}
    }
    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.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: NullPointerException

    Without an SSCCE, I'm only guessing. But you might also split that for loop up into multiple lines, something like this:

    Java Code:
    for(whatever){
       //do something
    }
    When do you initialize your array? How many indexes does the array you're accessing have? Which index are you trying to access?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    60
    Rep Power
    0

    Default 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).

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default 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!

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,800
    Rep Power
    19

    Default 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.

    ** This space for rent **

  10. #10
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    60
    Rep Power
    0

    Default Re: NullPointerException

    Quote Originally Posted by KevinWorkman View Post
    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.
    Quote Originally Posted by Tolls View Post
    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.
    I inserted the following code before the for loop in question:
    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");
    }
    And it still says the exception is at line 7, which is for (int i = 0; i < troopInfo.UnitList.units.length; i++).
    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?

    Quote Originally Posted by Tolls View Post
    By the way, according to the above code and stack trace this is the problem line (line 8):
    Java Code:
    tribeBuilding = buildingInfo.BuildingList.horseDrinkingTrough;
    Yeah, I edited the line after I had already copied the error message.

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: NullPointerException

    Quote Originally Posted by Mate de Vita View Post
    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?
    No, it's supposed to be a reference to the actual array. When do you declare that array? Are you sure? Like I said, we're just guessing 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!

  12. #12
    filestream is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: NullPointerException

    Uh oh, I smell a "pass by value vs. pass by reference vs. pass by pointer to a pointer" debate coming up...

  13. #13
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    60
    Rep Power
    0

    Default Re: NullPointerException

    Quote Originally Posted by KevinWorkman View Post
    No, it's supposed to be a reference to the actual array. When do you declare that array? Are you sure? Like I said, we're just guessing without an SSCCE.
    OK, I'll try to make an SSCCE.
    Last edited by Mate de Vita; 03-13-2012 at 08:02 PM.

  14. #14
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    60
    Rep Power
    0

    Default 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

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default 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!

  16. #16
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    60
    Rep Power
    0

    Default Re: NullPointerException

    Like this?

    Main:
    Java Code:
    public class Main {    
        public static void main (String[] args) {
            Unit unit = UnitList.legionnaire;
        }
    }
    Unit:
    Java Code:
    public class Unit {
    }
    UnitList:
    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;
    }
    Legionnaire:
    Java Code:
    public final class Legionnaire extends Unit {
        public Legionnaire() {
        }
    }
    Phalanx:
    Java Code:
    public final class Phalanx extends Unit {
        public Phalanx() {
        }
    }
    Tribe:
    Java Code:
    public class Tribe {
        protected Unit[] tribeUnits = new Unit[1];
    }
    Roman:
    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];
        }
    }

  17. #17
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: NullPointerException

    Quote Originally Posted by Mate de Vita
    Java Code:
    public final static Unit legionnaire = new Legionnaire(), ...
    Since you initialised these objects in a static context, they won't be initialised by what is written in the constructor.

    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; }
    
    }
    Java Code:
    static Legionnaire leg = new Legionnaire();
    int hp = leg.getHP(); //null pointer - field hp was not initialised!
    But with the static block:

    Java Code:
    class Legionnaire {
    
      //all same as above
      ...
    
      static {
          hp=50;
          maxhp=50;
      }
    }
    Then it should work.

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,800
    Rep Power
    19

    Default Re: NullPointerException

    Quote Originally Posted by Mate de Vita View Post
    Like this?
    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.

    Quote Originally Posted by ozzyman View Post
    Since you initialised these objects in a static context, they won't be initialised by what is written in the constructor.
    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.

    ** This space for rent **

  19. #19
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    60
    Rep Power
    0

    Default Re: NullPointerException

    This is what I have now:

    Main:
    Java Code:
    public class Main {    
        public static void main (String[] args) {
            Unit unit = UnitList.legionnaire;
        }
    }
    Unit:
    Java Code:
    public class Unit {
        protected static String title;
        
        public String toString() {
            return (title);
        }
    }
    UnitList:
    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;
    }
    Legionnaire:
    Java Code:
    public final class Legionnaire extends Unit {    static {
            title = "Legionnaire";
        }
    }
    Phalanx:
    Java Code:
    public final class Phalanx extends Unit {
        static {
            title = "Phalanx";
        }
    }
    Tribe:
    Java Code:
    public class Tribe {
        protected static String title;
        
        protected static troopInfo.Unit[] tribeUnits = new troopInfo.Unit[10];
        
        public String toString() {
            return (title);
        }
    }
    Roman:
    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];
        }
    }
    But it still says that units is null.


    Quote Originally Posted by Tolls View Post
    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.
    Hmm.. I'll reinstall java and try running it.
    Last edited by Mate de Vita; 03-14-2012 at 11:00 AM.

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,800
    Rep Power
    19

    Default Re: NullPointerException

    Don't reinstall Java.
    It won't be that.
    It is more likely that you need to ensure a clean build.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 1 of 3 123 LastLast

Similar Threads

  1. NullPointerException
    By Diz in forum New To Java
    Replies: 10
    Last Post: 05-13-2011, 02:58 AM
  2. NullPointerException
    By GPB in forum New To Java
    Replies: 8
    Last Post: 02-21-2010, 03:05 PM
  3. NullPointerException
    By Juuno in forum New To Java
    Replies: 1
    Last Post: 02-11-2010, 05:43 PM
  4. NullPointerException help?
    By fab5freddy in forum New To Java
    Replies: 2
    Last Post: 02-04-2010, 08:26 PM
  5. NullPointerException help me!
    By phancuong87 in forum New To Java
    Replies: 4
    Last Post: 01-19-2010, 04:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •