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.


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

Results 1 to 3 of 3

Thread: Exception in thread ... Help

Hybrid View

  1. #1

    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);
    		// 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");
    		 infantry infant1 = new infantry(0,0,0);
    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

    Default Re: Exception in thread ... Help

    If this line is where the NullPointerException occurred:
    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 04:08 PM.

  3. #3

    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, 01: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, 09: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, 02:38 AM
  5. Replies: 1
    Last Post: October 25th, 2009, 12:54 PM