日期:2014-05-20  浏览次数:20696 次

java数组java.lang.NullPointerException问题
我在编一个矩阵转置程序时,发生如题错误,查阅资料未能解决问题,初次学习,知识不全,特来求教高手。
1。问题出在什么地方?
2。为什么会产生此问题?
3。请帮我指出程序上该改进的地方。
先谢谢大家,希望不吝赐教。

源程序如下:
import java.io.*;

 public class C_Matrix{
public static void main(String args[]) throws IOException{
  Matrix A = new Matrix();
  A.ZhuanZ();
  Matrix B = new Matrix(3,4);
  B.ZhuanZ();
}
}



class Matrix{
private static int arr[][];
private static int col;
private static int row;

public Matrix(){
col = 5;
row = 5;
int arr[][] = new int[5][5];
for(int i = 0;i < 5;System.out.println(),i++)
for(int j = 0;j < 5;j++){
arr[i][j] = 10*i+j;
System.out.print(arr[i][j]+"\t");}
}

public Matrix(int a,int b){
col = a;
row = b;
int arr[][] = new int[col][row];
System.out.println("Init the array");
for(int i = 0;i < a;i++)
for(int j = 0;j < b;j++)
arr[i][j] = IntOut();

}

public static int IntOut() {
BufferedReader buf;
String str;
int tmp = 0;
try{
buf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Input an integer:");
str = buf.readLine();
tmp = Integer.parseInt(str);
}
catch(IOException e){
e.toString();}
finally{
return tmp;
}
}

public void ZhuanZ(){
int tmp;
int arr2[][] = new int[row][col];
for(int i = 0;i < col;i++)
for(int j = 0;j < row;j++){
arr2[j][i] = arr[i][j];//!!!!
}

for(int i = 0;i < row;System.out.println(),i++)
for(int j = 0;j < col;j++){
System.out.print(arr2[i][j]+"\t");
}
}

}

------解决方案--------------------
用eclipse,debug看看再说。

看起来比较累!
------解决方案--------------------
修改为
arr= new int[5][5];