java数组java.lang.NullPointerException问题

yilliam 2007-10-15 04:38:13
我在编一个矩阵转置程序时,发生如题错误,查阅资料未能解决问题,初次学习,知识不全,特来求教高手。
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");
}
}

}
...全文
94 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yilliam 2007-10-15
  • 打赏
  • 举报
回复
多谢两位,问题解决。
「已注销」 2007-10-15
  • 打赏
  • 举报
回复
修改为
arr= new int[5][5];
「已注销」 2007-10-15
  • 打赏
  • 举报
回复
构造方法里
int arr[][] = new int[5][5];

把int去掉
luyang1016 2007-10-15
  • 打赏
  • 举报
回复
用eclipse,debug看看再说。

看起来比较累!

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧