2017-01-28 8 views
1

Ich habe Probleme mit meiner zweiten Klasse (MazeSolver) nicht die Werte von Reihen bekommt und cols von meiner ersten Klasse (MazeInput), wie es sein sollte. Das führt zu ArrayOutOfBoundsExceptions und NullPointerExceptions in meiner DrawMaze() -Methode in MazeSolver. Ich verstehe, warum diese Fehler passieren, ich bin nur ratlos, wie ich es angehen kann, weil ich nicht verstehe, wie man Felder an andere Klassen weitergibt. Könnten Sie bitte nette Leute darauf hinweisen, wo ich falsch liege?Getter & Setter und Verwirrung

public class LA2_MazeInput { 

private int rows; 
private int cols; 

public LA2_MazeInput() { 
    this.rows = getNumRows(); 
    this.cols = getNumCols(rows); 
} 

private int getNumRows() { 

    int rows = 0; 
    String input; 

    input = JOptionPane.showInputDialog(
      "Please enter the number of rows (5-10) for the maze."); 


    rows = Integer.parseInt(input); 




    return rows; 
} 

private int getNumCols(int numRows) { 

    int cols = 0; 
    String input; 



    input = JOptionPane.showInputDialog(
      "Please enter the number (5-10) of columns for the maze." 
      + "\n(Value cannot be the same as the number of rows.)"); 


    cols = Integer.parseInt(input); 




    return cols; 
} 

public void initializeMazeSolver(/*MazeSolver solver*/) { 

    LA2_MazeSolver ms = new LA2_MazeSolver(); 
    ms.setNumRows(this.rows); 
    ms.setNumCols(this.cols); 




    } 

} 



public class LA2_MazeSolver { 
    private int rows; 
    private int cols; 
    private String[][] maze; 

    public LA2_MazeSolver() { 

     this.maze = new String[rows][cols]; 
    } 

public void setNumRows(int numRows) { 

    this.rows = numRows; 

} 

public void setNumCols(int numCols) { 

    this.cols = numCols; 

} 

public int getNumRows() { 

    return this.rows; 

} 

public int getNumCols() { 

    return this.cols; 

} 

public void drawMaze() { 

    Random r = new Random(); 

    maze[0][0] = "S"; 
    maze[rows - 1][cols - 1] = "D"; 
    int limit = ((rows * cols)/3); 

    for (int i = r.nextInt(limit) + 1; i < limit; i++) { 

     maze[r.nextInt(rows - 1)][r.nextInt(cols - 1)] = "#"; 

    } 

    for (int i = 0; i < maze.length; i++) { 
     for (int c = 0; c < maze[0].length; c++) { 

      if (!(maze[i][c].matches("#")) && !(maze[i][c].matches("S")) && !(maze[i][c].matches("D"))) { 

       maze[i][c] = Integer.toString(r.nextInt(100) + 1); 

      } 

     } 
    } 
    for (int i = 0; i < maze.length; i++) { 
     for (int c = 0; c < maze[0].length; c++) { 

      System.out.print(maze[i][c] + " "); 

     } 
     System.out.println(); 
    } 


    } 
} 

Antwort

2

Ihr MazeSolver Konstruktor initialisiert Ihre zweidimensionale Matrix mit der Größe von [0][0]. Danach versuchst du die Zeilen und Spalten zu setzen, an denen es zu spät ist.

LA2_MazeSolver ms = new LA2_MazeSolver(); // constructor of LA2_MazeSolver is called 
ms.setNumRows(this.rows); // does nothing for the array 
ms.setNumCols(this.cols); // does nothing for the array 

Um dies zu beheben, könnten Sie einfach die Parameter zusammen mit dem Konstruktor übergeben.

public LA2_MazeSolver(int rows, int cols) { 
     this.maze = new String[rows][cols]; 
     this.rows = rows; // in case you want them to store 
     this.cols = cols; // in case you want them to store 
    } 

Die Antipattern:

public LA2_MazeSolver() { 
    } 

public void setNumRows(int numRows) { 
    this.rows = numRows; 
} 

public void setNumCols(int numCols) { 
    this.cols = numCols; 
} 

public void init(){ 
    this.maze = new String[rows][cols]; 
} 

Wäre es init wie diese

LA2_MazeSolver ms = new LA2_MazeSolver(); 
ms.setNumRows(this.rows); 
ms.setNumCols(this.cols); 
ms.init(); 
+0

ich darüber nachgedacht, aber an diesem Punkt würde ich sogar die Getter und Setter müssen? –

+0

@GregSmith So weit ich dir sagen kann, brauche ich sie nicht. –

+0

Das ist das halbe Problem ... Ich möchte es mit Konstrukteuren machen. Allerdings bin ich verpflichtet, die Getter und Setter zu haben und sie als meinen prof erforderlich zu verwenden, folgen wir seinem "grundlegenden Programmdesign". Gibt es eine Möglichkeit, dies mit Gettern und Setter zu tun? –