-1

I'm trying yo use an array that is created in one class and using Clone() To clone it into another class by triggering a method but its throwing a null pointer exception

int[] mat[];
int N; 
int SRN;
int K;
int send[][];
Generate()
{ 

}
Generate(int N, int K) 
{ 
    this.N = N; 
    this.K = K; 

    // Compute square root of N 
    Double SRNd = Math.sqrt(N); 
    SRN = SRNd.intValue();

    mat = new int[N][N];
}

public int[][] SendAry()
{
    return send.clone();
}

and call this method from another class and string it in an array

    Generate ARY=new Generate();
    int Values[][]=ARY.SendAry();
Hamza
  • 23
  • 5

1 Answers1

1

The array send is not initialized so it is null. You can initialize it using many ways. The simplest way is int send[][] = new int[<size>][<size>]; the <size> is the size you want the array to have. You can see more about initializing arrays from here

TehMattGR
  • 1,710
  • 2
  • 7
  • 19
  • Thanks for the replay but the nullpointexception is solved but the array is still empty. not null but zeros even if i point it directly with an object – Hamza May 10 '19 at 13:21
  • what do you want to have? – TehMattGR May 10 '19 at 13:23
  • This is a program to create a sudoku puzzle the array is in generate class and its working fine if i print it directly from that class it works fine but im trying to use that array and print it using an interface using panels and buttons and all that but as soon as i clone it or even directly point to the array in the class using an object its empty now like zeros all the way – Hamza May 10 '19 at 13:28
  • the whole array is full of zeros by default. You can change them using a loop. – TehMattGR May 10 '19 at 13:32
  • yeah the method runs...Changes the values....when i print it in the same class it works. but if print it in another class its zeros – Hamza May 10 '19 at 13:47
  • can I see what code you used? – TehMattGR May 10 '19 at 13:52
  • I figured it out. i was asking for the array before a method worked on it so thats why it was showing up as Zero. thanks for the help about initializing array that actually really helped – Hamza May 10 '19 at 14:01