I wrote sample code to test but it gives error. Following is the code that i wrote:
The program gives following error:
Exception in thread "main" java.lang.NullP ointerException
at ArrCreate.creat eArray(MultiArr ay.java:10…
at ArrCreate.displ ayArray(MultiAr ray.java:2…
at MultiArray.main (MultiArray.jav a:37)
Code:
class ArrCreate
{
public int[][] createArray()
{
int[][] s = new int[3][];
s[0] = new int[4];
s[1] = new int[2];
for(int i = 0; i < s.length; i++)
{
for(int j=0; j < s[i].length; j++)
{
s[i][j] = j + 1;
}
}
return s;
}
public void displayArray()
{
int[][] a;
a = createArray();
for(int i = 0; i < a.length; i++)
{
for(int j=0; j < a[i].length; j++)
{
System.out.println(a[i][j]);
}
System.out.println();
}
}
}
class MultiArray
{
public static void main(String args[])
{
ArrCreate Arr = new ArrCreate();
Arr.displayArray();
}
}
Exception in thread "main" java.lang.NullP ointerException
at ArrCreate.creat eArray(MultiArr ay.java:10…
at ArrCreate.displ ayArray(MultiAr ray.java:2…
at MultiArray.main (MultiArray.jav a:37)
Comment