Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 53
Like Tree1Likes

Thread: NullPointerException

  1. #21
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: NullPointerException

    Oh, and ignore ozzyman until he explains what he's trying to say.

    Those static blocks are wrong wrong wrong.
    You have given yourself a single 'title' field in Unit shared by all subclasses.
    Which means that 'title' will be "Phalanx" for everything.
    Use a constructor and remove the static from 'title'.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  2. #22
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: NullPointerException

    Same with the Tribe stuff.
    Remove the static data and code blocks and put proper constructors in there, as you had.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: NullPointerException

    OK, but the reason I said I'd reinstall java is that when I compile&run this with the command prompt, it compiles fine but when I do java Main, it gives me an error message:
    Exception in thread "main" java.lang.UnsupportedClassVersionError: Main: Unsupported major.minor version 51.0

    Google says I need to reinstall java because my jdk version is off or something.

  4. #24
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    63
    Rep Power
    0

    Default Re: NullPointerException

    One thing I did notice is that javac Main.java doesn't compile the classes Legionnaire and Phalanx, so even if I successfully ran Main, it couldn't properly initiate legionnaire and phalanx in the class UnitList. Does Eclipse automatically compile all the files in the project or could this be the problem?
    Last edited by Mate de Vita; 03-14-2012 at 11:22 AM.

  5. #25
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: NullPointerException

    That error is because you are compiling using one version of java and running using another.
    From the command prompt do:
    javac -version
    then
    java -version

    What command are you using to compile all your code?
    It should be (for something like the sample code given):
    javac *.java
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #26
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    63
    Rep Power
    0

    Default Re: NullPointerException

    Quote Originally Posted by Tolls View Post
    That error is because you are compiling using one version of java and running using another.
    From the command prompt do:
    javac -version
    then
    java -version
    javac is version 1.7.0_01
    java is version 1.6.0_31

    I'm guessing I have 64-bit java set as the default, since I have both 32-bit and 64-bit installed. Where can I change that?

    Quote Originally Posted by Tolls View Post
    What command are you using to compile all your code?
    It should be (for something like the sample code given):
    javac *.java
    Yeah, I used javac Main.java

  7. #27
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: NullPointerException

    That's got to be your PATH somewhere.
    The 1.6 JRE one is probably earlier in the PATH than the 1.7 JDK.

    As for compiling, that's why you're seeing wierd things, you;re only compiling Main, and never recompiling anything else. So it's running old versions.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #28
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    63
    Rep Power
    0

    Default Re: NullPointerException

    Hmm... the only java-related thing in the Path environmental variable is C:\Program Files\Java\jdk1.7.0_01\bin

    javac Main.java does compile the classes Tribe, Unit, UnitList and Roman as well. Not Phalanx and Legionnaire though.

  9. #29
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: NullPointerException

    Take my word for it, if the above code is giving you an NPE then you are not running the above code but something else, likely because there are old class files somewhere.

    As for the Java 1.6 runtime, it must be somewhere. The Command Line has to have it in its path to see it.
    Try C:\Program Files\Java\jdk1.7.0_01\bin\java -version from the command.
    If that comes up 1.7 then there is a path somewhere causing you trouble.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: NullPointerException

    I don't get this, this is the content of my Path environmental variable (My Computer Properties -> Advanced System Settings -> Environmental variables):
    C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;%SystemRoot%\system32;%Sy stemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\ System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\;C:\Program Files (x86)\Common Files\Roxio Shared\OEM\DLLShared\;C:\Program Files (x86)\Common Files\Roxio Shared\OEM\DLLShared\;C:\Program Files (x86)\Common Files\Roxio Shared\OEM\12.0\DLLShared\;C:\Program Files (x86)\Roxio\OEM\AudioCore\;C:\Program Files\Java\jdk1.7.0_01\bin
    Where else can that path be defined?
    "C:\Program Files\Java\jdk1.7.0_01\bin\java" -version came up 1.7.0_01

    I haven't run the code the I posted here yet, this was supposed to be an SSCCE (though if it really doesn't give a NPE, it's apparently not a very successfully made one).
    I get the NPE when I run the entire program from Eclipse with the built-in Run button.

  11. #31
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: NullPointerException

    Well, the example you supplied never creates a Roman, so it never runs that line, but even adding in (as I did) a 'new Roman()' call it still didn't give the NPE.

    As for why your command line is giving you 1.6 for java, what is the output of PATH from the cmd (not that it should be any different).

    Anyway, back at the problem, does that example you gave us give you an NPE when run through Eclipse?

    ETA: Oh, and what happens with your original project if you right click on the Main class and select RunAs...Java
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: NullPointerException

    Quote Originally Posted by Tolls View Post
    As for why your command line is giving you 1.6 for java, what is the output of PATH from the cmd (not that it should be any different).
    Same.

    Quote Originally Posted by Tolls View Post
    Anyway, back at the problem, does that example you gave us give you an NPE when run through Eclipse?
    No, that works.

    Quote Originally Posted by Tolls View Post
    ETA: Oh, and what happens with your original project if you right click on the Main class and select RunAs...Java
    I tried Open With.. Java Platform SE Binary, and the command prompt shows up for a split second, then exits. Nothing else happens. Managed to print screen it after a few tries, and the error message is
    Error: Could not find or load main class C:\users\blabla\workspace\blabla\main\MainWindow.c lass



    My SSCCE was bad, I forgot to include the thing that I'm now convinced is causing the problem. In each of the Unit subclasses the unit's tribe is defined, which is causing the problem, since it puts the program into a magic circle, I guess. Try running this, it gives me a NPE in Eclipse:

    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() {
            title = "Legionnaire";
            tribe = TribeList.roman;
        }
    }
    Phalanx:
    Java Code:
    public final class Phalanx extends Unit {    public Phalanx() {
            title = "Phalanx";
            tribe = TribeList.roman;
        }
    }
    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];
        }
    }
    Gaul:
    Java Code:
    public class Gaul extends Tribe {	public Gaul() {
            title = "Gaul";
            for (int i = 0; i < tribeUnits.length; i++) tribeUnits[i] = UnitList.units[UnitList.GAUL][i];
        }
    }
    Tribelist:
    Java Code:
    public class TribeList {
    	public final static Tribe roman = new Roman(), gaul = new Gaul();
    }

  13. #33
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: NullPointerException

    Whoops, sorry, I meant within Eclipse...:)
    Run As -> Java App

    I rarely trust the Run button to run what I actually want it to run.

    Anyway...you have found your problem.
    The order of your constructors is:
    Legionnaire (called from the static legionnaire in UnitList)
    Roman (called from within TribeList when TribeList.roman is referenced in Legionnaire)
    ...BANG...

    because UnitList.units hasn't been initialised.

    I would suggest making UnitList and TribeList singletons, and removing the 'static' data, making it attributes of the single UnitList and TribeList objects.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: NullPointerException

    You mean like this?

    Main:
    Java Code:
    public class Main {    
        public static void main (String[] args) {
            Unit unit = UnitList.legionnaire;
        }
    }
    Unit:
    Java Code:
    public class Unit {
        String title;
        Tribe tribe;
        final TribeList tribeList = new TribeList();
    }
    UnitList:
    Java Code:
    public final class UnitList {
        public final Unit legionnaire = new Legionnaire();
        public final Unit phalanx = new Phalanx();
     
     
        public final Unit[][] units = { {legionnaire}, {phalanx} };
        public final static int ROMAN = 0, GAUL = 1;
    }
    Legionnaire:
    Java Code:
    public final class Legionnaire extends Unit {
            public Legionnaire() {
            title = "Legionnaire";
            tribe = tribeList.roman;
        }
    }
    Phalanx:
    Java Code:
    public final class Phalanx extends Unit {
        public Phalanx() {
            title = "Phalanx";
            tribe = tribeList.roman;
        }
    }
    Tribe:
    Java Code:
    public class Tribe {
        Unit[] tribeUnits = new Unit[1];
        final UnitList unitList = new UnitList();
    }
    Roman:
    Java Code:
    public class Roman extends Tribe {
        public Roman() {
            for (int i = 0; i < tribeUnits.length; i++) tribeUnits[i] = unitList.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.units[UnitList.GAUL][i];
        }
    }
    Tribelist:
    Java Code:
    public class TribeList {
        public final Tribe roman = new Roman(), gaul = new Gaul();
    }


    The problem is that I also have a similar package with building information, and when I did that same thing with the BuildingList class, this code caused a stack overflow error (because it's constantly calling itself over and over again - Building calls BuildingList, which calls Academy, which again calls Building - at least that's my guess):

    Building:
    Java Code:
    public class Building {
        Unit[] trainedUnits = new Unit[1];
        Tribe tribe;
        Building requiredBuilding;
    
        final UnitList unitList = new UnitList();
        final TribeList tribeList = new TribeList();
        final BuildingList buildingList = new BuildingList();
    }
    BuildingList:
    Java Code:
    public class BuildingList {
        public final Building academy = new Academy(), mainBuilding = new MainBuilding();
    }
    Academy:
    Java Code:
    public class Academy {
        public Academy() {
            trainedUnits[0] = unitList.legionnaire;
            tribe = tribeList.roman;
            requiredBuilding = buildingList.mainBuilding;
        }
    }
    MainBuilding:
    Java Code:
    public class MainBuilding {
        public MainBuilding() {
            trainedUnits[0] = unitList.phalanx;
            tribe = tribeList.gaul;
        }
    }


    Here's the error code:
    Exception in thread "main" java.lang.StackOverflowError at buildingInfo.Academy.<init>(Academy.java:4)
    at buildingInfo.BuildingList.<init>(BuildingList.java :4)
    at buildingInfo.Building.<init>(Building.java:16)
    at buildingInfo.Academy.<init>(Academy.java:4)
    at buildingInfo.BuildingList.<init>(BuildingList.java :4)
    at buildingInfo.Building.<init>(Building.java:16)
    This is repeated several more times (note that the line numbers aren't correct, as the versions of the classes that I posted are abridged).
    Last edited by Mate de Vita; 03-18-2012 at 10:30 PM.

  15. #35
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: NullPointerException

    Where else can that path be defined?
    "C:\Program Files\Java\jdk1.7.0_01\bin\java" -version came up 1.7.0_01
    Check whether C:\Windows\System32 (rather, its x64 equivalent) still has an old version.

    I think it should be safe to overwrite the file in that location with the Java 7 executable, but I wouldn't swear to that in a court of law ;)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #36
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: NullPointerException

    You have a major recursion problem resulting in your overflow.
    You have to figure out a way to break that dependence.
    Your Lists are the cause.

    Take the troops lists.
    There is no need for the Roman class to have its own list of troops, since it can access them directly from the UnitList class.
    Either that, or remove the UnitList class entirely and simply have each subclass of Tribe have its own units.

    Same sort of thing for Buildings.
    Why does each Building need its own BuildingList?
    It doesn't.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #37
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    63
    Rep Power
    0

    Default Re: NullPointerException

    Quote Originally Posted by Tolls View Post
    Take the troops lists.
    There is no need for the Roman class to have its own list of troops, since it can access them directly from the UnitList class.
    How do you mean? They're not static..

  18. #38
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: NullPointerException

    Oops.
    Sorry, they were in the original...that's my excuse anway.
    :)

    Do you know what a singleton is?
    You only really want one UnitList, since there will only ever be one list of these things.
    So either create that one then pass it in to the other constructors as a parameter, or make it a singleton.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: NullPointerException

    Quote Originally Posted by Tolls View Post
    Do you know what a singleton is?
    You mean this?
    Introducing the singleton - "The Single Java Object"

  20. #40
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: NullPointerException

    Yes, but the preferred structure is:
    Java Code:
    public class MySingleton {
        private static final MySingletong instance = new MySingleton();
    
        private MySingleton() {
            // Whatever setup stuff you want in here
        }
    
        public static MySingleton getInstance() {
            return instance;
        }
        ... instance methods etc.
    }
    That's shorter, definitely thread safe, and all the rest.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 2 of 3 FirstFirst 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
  •