简体   繁体   中英

How to display a char array? Java

I'm trying to display a multi dimensional char array, but I'm getting an error when trying to display it. Is there a better way to display this? Did I do something wrong somewhere else? I can't figure out what is wrong with my code.

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
    }
}

Edit:

Sorry here's the error!

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)

try initializing this private char [][]binTree; rather than creating a new array inside the con structure ( char[][] binTree = new char[rows][columns]; ). So the private char [][]binTree; is null every time you try printing. And for printing you can use below:

   char[][] c = new char[2][10];
   // read the values
  //now display
    for(int i=0;i<c.length;i++){
        System.out.println(String.valueOf(c[i]));
    }
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
    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 static void main(String[] args) {
        int size = 16;
        recBinTree tree = new recBinTree(size);
        tree.display();  //error on this call
    }
}

You declare char [][]binTree; as instaces and also again declare at contructor. Remove declaration from constructor.

private char [][]binTree;

public recBinTree(int size){
     ......
   binTree = new char[rows][columns]; //Remove  char[][]  from here
    //populate entire array with hyphens
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++)
            binTree[i][j] = '-';
    }
}

Also you don't need two for loops to fill binTree array with - . Just simply use Arrays.fill operation.

 public recBinTree(int size){
     ......
   binTree = new char[rows][columns];
   Arrays.fill('-');       
}

Do like this

    char [][]binTree={{'A','B','C'},{'D','E','F'},{'G','H','I'}};
    System.out.println(Arrays.deepToString(binTree));

By doing char[][] binTree = new char[rows][columns]; you are creating a new variable within the scope of the function. You are getting a null pointer exception, because when the application later looks in the binTree-variable for the scope of the entire class, it will find out is empty.

Change it to:

binTree = new char[rows][columns];

This will initialise the binTree-variable for the scope of the entire class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM