Page 3 of 3 FirstFirst 123
Results 41 to 53 of 53
Like Tree1Likes

Thread: NullPointerException

  1. #41
    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.getInstance().legionnaire;
        }
    }
    Unit:
    Java Code:
    public class Unit {
        String title;
        Tribe tribe;
    }
    UnitList:
    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;
        }
    }
    Legionnaire:
    Java Code:
    public final class Legionnaire extends Unit {
            public Legionnaire() {
            title = "Legionnaire";
            tribe = TribeList.getInstance().roman;
        }
    }
    Phalanx:
    Java Code:
    public final class Phalanx extends Unit {
        public Phalanx() {
            title = "Phalanx";
            tribe = TribeList.getInstance().gaul;
        }
    }
    Tribe:
    Java Code:
    public class Tribe {
        Unit[] tribeUnits = new Unit[1];
    }
    Roman:
    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];
        }
    }
    Gaul:
    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];
        }
    }
    TribeList:
    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;
        }
    }
    Building:
    Java Code:
    public class Building {
        Unit[] trainedUnits = new Unit[1];
        Tribe tribe;
        Building requiredBuilding;
    }
    BuildingList:
    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;
        }
    }
    Academy:
    Java Code:
    public class Academy {
        public Academy() {
            trainedUnits[0] = UnitList.getInstance().legionnaire;
            tribe = TribeList.getInstance().roman;
            requiredBuilding = BuildingList.getInstance().mainBuilding;
        }
    }
    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

  2. #42
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default 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:
    Java Code:
    UnitList.getInstance().units[UnitList.ROMAN];
    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.

    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.

    ** This space for rent **

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

    Default Re: NullPointerException

    OK, I removed that now, but now I'm getting a NPE in the Academy class:
    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
    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?):
    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;
        }
    }

  4. #44
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: NullPointerException

    Why does Academy need access to the MainBuilding during contruction?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

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

  6. #46
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

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

    ** This space for rent **

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

    Default Re: NullPointerException

    Is it possible create a reference to an object without actually constructing that object? Cause that would solve my problems, I believe.

  8. #48
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: NullPointerException

    Nope.
    I would suggest:
    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();
        }
    }
    I've called it load, because I tend to load stuff from file, but the concept is the same.
    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.

    ** This space for rent **

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

    Default Re: NullPointerException

    Interesting.. I'll try something like this when I get home.

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

    Default 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:
    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;
        }
    }
    Entity:
    Java Code:
    public abstract class Entity {
        protected String title;
        
        public String toString() { return title; }
        public void loadData();
    }
    Unit:
    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.

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

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

    ** This space for rent **

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

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

  13. #53
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: NullPointerException

    No problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 3 of 3 FirstFirst 123

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
  •