Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Exception in thread ... Help

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Location
    Scotland
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Exception in thread ... Help

    Hello everyone at JPF!

    I am completey new to Java, in fact I am completely new to lower level languages full stop. I have a reasonable amount of experience with high level languages namely MatLab so I understand programming concepts but not the software engineering of it all. I tell you this so you go in knowing it's most probably a stupid question...

    Project Outline:
    I am building a turn based game for which I am laying down some foundations to get some understanding

    components of the project that are causing me problems:

    class unit
    subclass infantry extends unit

    class army

    public class unit {
    //	vars
    public int posX,posY,posZ;
    public String type;
     
     
    //	constructor
    public unit(int x, int y, int z, String t) {
    	this.posX = x;
    	this.posY = y;
    	this.posZ = z;
    	this.type = t;
    	}
    	//	methods
     
    	// movement
    	public void move(int moveX, int moveY, int moveZ)
    	{
    		this.posX = this.posX + moveX;
    		this.posY = this.posY + moveY;
    		this.posZ = this.posZ + moveZ;
    	}
        public void printStates()
        {
            System.out.println("unit type:"+this.type+"posX"+this.posX+" posY:"+this.posY+" posZ:"+this.posZ);
        }
     
     
    }
    public class infantry extends unit{
     
    	static String infantryType = "Infantry";
     
    //	stance 0=standing,1=crouch,2=prone
    	public int stance,accuracy;
     
    //	accuracy base describes the accuracy of a unit given a stance
    	public int accuracyBase[] = new int[3];
     
     
     
    	public infantry(int x, int y, int z) {
    		super(x, y, z, infantryType);
     
    		this.accuracyBase[0]=1;
    		this.accuracyBase[1]=2;
    		this.accuracyBase[2]=3;
     
     
     
    		// TODO Auto-generated constructor stub
    	}
     
    	public void changeStance(int newStance)
    	{
    		this.stance = newStance;
     
    	}
     
    	public void update()
    	{
    		this.accuracy = accuracyBase[this.stance];
    	}
     
     
    }

    public class army
    {
    	public infantry[] armyInf;
    	public infantry[] armyUni;
     
    	public army(int ninf,int nveh)
    	{
    		infantry[] 	armyInf = new infantry[ninf];
    		unit[] 		armyUni = new unit[nveh];
     
    		for (int i = 0;i<ninf;i++)
    		{
    			armyInf[i] = new infantry(0,0,1);
    		}
     
    		for (int j = 0;j<nveh;j++)
    		{
    			armyUni[j] = new unit(0,0,0,"vehicle");
    		}
    	}
     
    }

    so My main function is below. I will mention now that there are no compilation errors and the calls to unit classes and infantry classes work and the printstates works also.

    public class emwar {
     
    	public static void main(String[] args) {
     
    		 army army1 = new army(3,1);
     
    		 unit unit1 = new unit(10,10,10,"foot");
    		 unit unit2 = new unit(0,0,0,"foot");
     
    		 unit1.move(1,1,1);
    		 unit1.printStates();
     
    		 unit2.move(20,20,0);
    		 unit2.printStates();
     
    		 infantry infant1 = new infantry(0,0,0);
    		 infant1.printStates();
     
    		 army1.armyInf[0].printStates();
     
    	}

    The error when this is run is:

    Exception in thread "main" java.lang.NullPointerException at cis.emwar.main(emwar.java:30)

    Note: line 30 coincides with the army1.armyInf[0].printStates(); call

    I have seen the thread here and have made the modification in class army to match this, which [clearly] has not worked for me.

    All help greatly appreciated!

    Keep up the Fun!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exception in thread ... Help

    If this line is where the NullPointerException occurred:
    army1.armyInf[0].printStates();
    You need to determine Which variables in that line are null?
    If you can't tell, print them out separately to see which one is null.
    Then back track in your code to see why that variable has a null value.
    Then change the code to give it a value.

    public infantry[] armyInf;
    	public infantry[] armyUni;
     
    	public army(int ninf,int nveh)
    	{
    		infantry[] 	armyInf = new infantry[ninf];
    		unit[] 	armyUni = new unit[nveh];

    You have defined the same variable at two levels of scope. Then inner one hides the outer one.

    Java naming conventions suggest that class names start with a Capital letter. When I see unit I think it is a variable. If I see Unit I think it is a class name.
    Last edited by Norm; June 26th, 2011 at 03:08 PM.

  3. The Following User Says Thank You to Norm For This Useful Post:

    .:Pi_man:. (June 26th, 2011)

  4. #3
    Junior Member
    Join Date
    Jun 2011
    Location
    Scotland
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread ... Help

    Thanks for the quick reply Norm!

    After replacing all the class names with Capitals and adding in some println's in to look for Nulls as you suggested it has seemed to correct itself! I'm putting it down to some of the auto correction that eclipse executed while I was changing the class names.

    Thanks again!

Similar Threads

  1. Exception in thread "main" java.lang.NullPointerException
    By manzili in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 9th, 2011, 12:02 PM
  2. Exception in thread "main" java.lang.NullPointerException
    By Tsark in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 11th, 2011, 08:39 AM
  3. exception in thread main java.lang.Nullpointerexception
    By westandeast in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 6th, 2011, 09:08 AM
  4. Replies: 2
    Last Post: March 23rd, 2010, 01:38 AM
  5. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM