This is my program to reverse all the elements of a matrix individually. (ie 23 goes to 32, 1427 goes to 7241, etc). It is giving 'java.lang.NullPointerException' in the fillArray() method, right after displaying the print statement. I tried using InputStreamReader throughout the code and it still did not work. Please help. Thanks!
import java.io.*;
import java.util.*;
public class MatRev
{int m,n,arr[][];
MatRev(int mm, int nn)
{m=mm;
n=nn;
int arr[][]=new int[m][n];
}
public void fillArray()
{Scanner sc=new Scanner(System.in);
System.out.println("Please enter elements in array");
for(int i=0;i<m;i++)
{for(int j=0;j<n;j++)
{arr[i][j]=sc.nextInt();
}
}
}
public int reverse(int x)
{int r=0,g=0;
do
{g=x%10;
x=x/10;
r=r*10+g;
}while(x!=0);
return r;
}
void revMat(MatRev p)
{for(int i=0;i<m;i++)
{for(int j=0;j<n;j++)
{this.arr[i][j]=reverse(p.arr[i][j]);
}
}
}
void show()
{System.out.println("Display in matrix format");
for(int i=0;i<m;i++)
{for(int j=0;j<n;j++)
{System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
public void main()throws IOException
{ InputStreamReader read=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(read);
System.out.println("Enter no of rows followed by no of columns");
int a=Integer.parseInt(buf.readLine());
int b=Integer.parseInt(buf.readLine());
MatRev ob1=new MatRev(a,b);
ob1.fillArray();
System.out.println("Original matrix");
ob1.show();
MatRev ob2=new MatRev(a,b);
ob2.revMat(ob1);
System.out.println("Reverse matrix");
ob2.show();
}}