我想实现数组在函数之间传递,可以出现了以下问题,谁能帮我讲讲呀
代码如下:
public class NodeClass { int flag[][]={{0,0},{1,1},{0,1},{0,1}}; public NodeClass() { int flag[][]={{0,0},{1,1},{0,1},{0,1}}; } public void pain() { String s; for(int i=0;i<flag.length;i++) { s=""; for(int j=0;j<flag[i].length;j++) { if(flag[i][j]==1) s+="*"; else s+=" "; } System.out.print(s+"\n"); } } public static void main(String[] args) { NodeClass nodeClass=new NodeClass(); nodeClass.pain(); paint p=new paint(nodeClass.flag); p.pain(); } } class paint { int flag[][]; public paint(int flag[][]) { for(int i=0;i<flag.length;i++) { for(int j=0;i<flag[i].length;j++) this.flag[i][j]=flag[i][j]; } } public void pain() { String s; for(int i=0;i<this.flag.length;i++) { s=""; for(int j=0;j<this.flag[i].length;j++) { if(this.flag[i][j]==1) s+="*"; else s+=" "; } System.out.print(s+"\n"); } }
}
编译时没错误,运行时出现以下结果:
**
*
*
Exception in thread "main" java.langNullPointerException
at paint.<init>(NodeClass.java:40)
at NodeClass.main(NodeClass.java:28)
谁能帮我讲讲呀,谢了