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 2 of 2

Thread: Requesting help.

  1. #1
    Junior Member narf0708's Avatar
    Join Date
    Jul 2012
    Posts
    1
    My Mood
    Lurking
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Requesting help.

    I made this code from the book "Head First Java 2nd Edition"(pages 95 and 96). It compiles fine, however, when one runs the program it gives the following error "exception in thread main java.lang.NullPointerException" This error occurs on line 21, most likely due to a variable that was set to Null. I cannot figure out what that variable is, and/or how to fix it. Any suggestions?

    class Dog{
    	int size;
    	String name;
    	void bark(int numOfBarks){
    		while(numOfBarks>0){	
    			if (size>60){
    				System.out.println("Woof! Woof!");
    			}else if(size>14){
    				System.out.println("Ruff! Ruff! Ruff!");
    			}else{
    				System.out.println("YIP!! YIP!! YIP!! YIP!!");
    			}//end if/else
    			numOfBarks=numOfBarks-1;
    		}//end loop
    	}//end bark
    }//end class Dog
     
    class DogTest{
    	public static void main(String[] args){
    		Dog[] Dog=new Dog[3];
    		Dog[0].size=63;  //error occurs here
    		Dog[1].size=8;
    		Dog[2].size=35;
     
    		System.out.println("Bark! Bark, all of ye! Bark, all my dogs!!");
    		Dog[0].bark(1);
    		Dog[1].bark(4);
    		Dog[2].bark(2);
    	}//end main
    }//end class DogTest


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Requesting help.

    Adding some println's in there to debug and backtrack can help you find the null variable.
    http://www.javaprogrammingforums.com...t-println.html
    If you do this, you will find that Dog[0] is null. Creating an object array is just that...you need to instantiate the objects within the array before you can use them.

Similar Threads

  1. Java uberNoob, requesting help with simple loop issue
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 09:13 PM
  2. Game requesting user input
    By Neo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 10:00 PM