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 when creating an array

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception when creating an array

    I'm losing the will to live now.

    I have created a class that simply initializes a number an then has a function that returns it. If I run the class normally it will return the number fine but whenever I want to create an array, it returns an exception.

    This initializes the array and prints the number to the the console:

    	public Game()
    	{
    		player[] p;
    		p = new player[1];
    		System.out.print(p[0].getX());
    	}

    And the class:

    public class player
    {
    	int x, y;
    	public player()
    	{
    		x=100;
    		y=100;
    	}
    	public int getX()
    	{
    		return x;
    	}
    }
    Last edited by Majora94; February 23rd, 2012 at 05:01 PM.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Exception when creating an array

    it returns an exception
    which is ... ?

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Exception when creating an array

    you are making a new array object. this array object holds player objects
    when you call your array and ask it for the first player object it sees that it has no players inside it, thus it can not run your get method.

    effectively:

    System.out.print(p[0].getX());

    is saying:

    System.out.print(null.getX());

    TADA!
    Jonathan

Similar Threads

  1. Creating an array in constructor ... defaults all values?
    By mwebb in forum Object Oriented Programming
    Replies: 2
    Last Post: February 19th, 2012, 04:06 PM
  2. Array exception out of bounds
    By gabberlt in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 17th, 2011, 08:05 AM
  3. [SOLVED] Error: Null Exception on Array of classes
    By g000we in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2010, 09:14 AM
  4. Creating array from generic type objects?
    By AndrewMiller in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2010, 09:22 PM
  5. creating an array of strings?
    By Jason in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 19th, 2010, 08:28 AM