Search code examples
javaarraysclone

Using Array From Another class Show Null pointer Exception


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();

Solution

  • 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