-1

I don't know what's wrong with this code:

class A{
    private int i,j;

    public void get(int i, int j)
    {
        this.i=i;
        this.j=j;
    }

    public void show()
    {
        System.out.println(i + " " + j);
    }
}

public class App {

    public static void main(String[] args) {
        A[] c = new A[11];
        for(int i=0; i<10; i++)
        {   
            c[i].get(i, i);
        }
        for(int j=0; j<10; j++)
        {
            c[j].show();
        }
    }

}

can anyone please tell me the right way to call the get() method?? thanks in advance...

p32929
  • 99
  • 2
  • 11
  • 2
    You should populate your array with instances of `A` first. – Arnaud Oct 27 '16 at 14:10
  • 2
    You're creating an array of type `A[]` but never populating it - every element will be null. – Jon Skeet Oct 27 '16 at 14:10
  • Can you please give an example code? I'm actually a new java learner... – p32929 Oct 27 '16 at 14:12
  • You should give your stacktrace. Even though it"s obviously a NullPointerException – OneCricketeer Oct 27 '16 at 14:14
  • Possible duplicate of [NullPointerException when Creating an Array of objects](http://stackoverflow.com/questions/1922677/nullpointerexception-when-creating-an-array-of-objects) – OneCricketeer Oct 27 '16 at 14:16
  • `get` is also a poor name for a method that actually sets values. You should use a constructor. https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html – OneCricketeer Oct 27 '16 at 14:18

1 Answers1

1
for(int i=0; i<10; i++)
{   
    c[i] = new A();
    c[i].get(i, i);
}

You had an empty array. You need to fill that array with elements

Jude Niroshan
  • 4,280
  • 8
  • 40
  • 62