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"); } } }