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

Thread: NullPointerException help with array

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NullPointerException help with array

    Hi i'm new to Java, when running the following test class on deb1 i get a NullPointerException. Can someone explain how to fix my array?

    Deb1:

    public class Deb1 
    {
        private int[] array; // create integer array
     
        public int[] allocate() 
        {
    	array[0] = 0;
    	array[1] = 1;
    	array[2] = 2;
    	return array;
        }
     
        /**
         * Gets a value from the array of integers.
         */
        public int get(int index) 
        {
    	return array[index];
        }
     
        /**
         * Sets a value in the array of integers; returns true if succesful,
         * false otherwise.
         */
        public boolean set(int index, int value) 
        {
    	if (index < array.length && index >= 0) {
    	    array[index] = value;
    	    return true;
    	}
    	else {
    	    return false;
    	}
        }
    }

    TestDeb1:
    public class TestDeb1 {
        public static void main(String[] args) {
    	Deb1 testObject;
    	testObject = new Deb1();
    	testObject.allocate();
    	if (testObject.get(1) != 1) {
    	    System.out.println("Error in get");
    	}
    	if (!testObject.set(2, 3)) {
    	    System.out.println("Error in set");
    	}
    	if (testObject.get(2) != 2) {
    	    System.out.println("Error in set 2");
    	}
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: NullPointerException help with array

    i get a NullPointerException.
    please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException help with array

    Exception in thread "main" java.lang.NullPointerException
    at Deb1.allocate(Deb1.java:20)
    at TestDeb1.main(TestDeb1.java:12)

    This may not be accurate as i removed some of the comments when posting the code. Just asked a friend on fb and he said i haven't instantiated it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: NullPointerException help with array

    xception in thread "main" java.lang.NullPointerException
    at Deb1.allocate(Deb1.java:20)
    at TestDeb1.main(TestDeb1.java:12)
    There is a variable with a null value on line 20. Look at line 20 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 20 and print out the values of all the variables on that line.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException help with array

    Fixed the original error by adding = new int[20] to the end of private int[] array. This solved the error because i hadn't created the array itself or set a value for the size,

Similar Threads

  1. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  2. NullPointerException Error in Code that Prints Array Elements
    By Atypically Engineered in forum Collections and Generics
    Replies: 8
    Last Post: April 6th, 2012, 10:21 PM
  3. NullPointerException
    By deathmatex in forum Exceptions
    Replies: 4
    Last Post: March 27th, 2012, 03:54 AM
  4. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM
  5. NullPointerException when trying to read Strings into an array of myObject
    By sdivinyi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 24th, 2010, 09:45 AM