1

So I'm trying to create a simple program that an object that has an Integer Array as it's member and a method toString() that prints out all the elements in the Array. The code look like this:

public final class IntegerArray {

      private int a[];

      IntegerArray(int a[])
      {
          for(int i=0; i<a.length; i++) this.a[i]=a[i];
      }

      public String toString()
      {
          String fin=new String();
          fin+='[';
          for(int i=0; i<a.length-1; i++)
          {
              fin+=a[i]+", ";
          }
          fin+=a[a.length-1];
          fin+=']';
          return fin;
      }
}

public class IntegerArrayTester {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan=new Scanner(System.in);
        Random random=new Random();

        int[] a={1, 5, 3, 7};
        IntegerArray A=new IntegerArray(a);
        System.out.println(A.toString());

    }

}

However when I run the program it gives me 2 errors. One in the constructor and another when I initialize an the object from the class:

Exception in thread "main" java.lang.NullPointerException

    at main.IntegerArray.<init>(IntegerArray.java:9)

    at main.IntegerArrayTester.main(IntegerArrayTester.java:14)

Can somebody please tell what's the problem and how can I fix it?

Thank you in advance!

jrtapsell
  • 6,719
  • 1
  • 26
  • 49
David Mathers
  • 163
  • 2
  • 7

4 Answers4

0

You array isn't initialitazed, so it gives a NullpointerException, you should add this in the constructor:

        this.a=new int[a.length];

So it ends as:

    IntegerArray(int a[]) {
                this.a=new int[a.length];
                for (int i = 0; i < a.length; i++)
                    this.a[i] = a[i];
            }
Cfuentes
  • 149
  • 1
  • 12
0

You never initialize a[] in IntegerArray

public final class IntegerArray {

    private int a[]; // here it's null
    ...
}

You should initialize the class variable creating a new array about the size of the one you are receiving as parameter:

 public final class IntegerArray {

     private int a[]; // here it's null

     IntegerArray(int a[]){
         this.a = new int[a.length];
         ...
     }
}

And by the way you don't have to loop over the array. You can just clone it:

 public final class IntegerArray {

     private int a[];

     IntegerArray(int a[]){
        this.a = a.clone();
   }
}
Guillaume Barré
  • 4,168
  • 2
  • 27
  • 50
0

You should initialize array in IntegerArray constructor like this:

IntegerArray(int a[])
{
  this.a = new int[a.length];
  for(int i=0; i<a.length; i++) this.a[i]=a[i];
}

Also, you can assign this.a = a, but if you do it, any operation with this.a array in your class, such as remove, add or edit element, array a will be change too, because operation this.a = a you just make a "link" to a and work with original a array.

0

What is the issue

You do not initialise a, so it is equal to null

Why is this an issue

You cannot call methods on null, as that causes NullPointerExceptions

How to fix

You need to initialise the array before you use it, an example of how would be:

this.a=new int[a.length];
jrtapsell
  • 6,719
  • 1
  • 26
  • 49