【发布时间】:2013-11-29 14:40:27
【问题描述】:
我正在尝试显示一个多维 char 数组,但在尝试显示它时出现错误。有没有更好的方法来显示这个?我在其他地方做错了吗?我无法弄清楚我的代码有什么问题。
public class recBinTree {
private int left, right, columns, rows;
private char [][]binTree;
public recBinTree(int size){
left = 0;
right = size-1;
columns = size;
double n = Math.log(size)/Math.log(2); //convert to base 2
rows = (int)n + 1; // # of rows = n
char[][] binTree = new char[rows][columns];
//populate entire array with hyphens
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++)
binTree[i][j] = '-';
}//end outer for
}//end constructor
public void display(){
System.out.print("Columns: " + columns + " ");
System.out.print("Rows: " + rows);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++)
System.out.print(binTree[i][j] + " "); //error on this line
System.out.print("\n");
}//end outer for
}//end display()
public class Driver {
public static void main(String[] args) {
int size = 16;
recBinTree tree = new recBinTree(size);
tree.display(); //error on this call
}
}
编辑:
对不起,这是错误!
Exception in thread "main" java.lang.NullPointerException
at ProjectThreeC.recBinTree.display(recBinTree.java:38)
at ProjectThreeC.Driver.main(Driver.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
【问题讨论】:
-
如果您告诉我们错误是什么,您认为这对我们没有帮助吗?
-
您遇到的错误是什么?
标签: java arrays multidimensional-array char