Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Labyrinthgenerator NullPointerExeption

Empfohlene Antworten

Veröffentlicht

Hallo, ich versuche derzeit einen Labyrinth-Algorithmus zu schreiben, und es funktioniert zum Teil auch, allerdings kommt nach kurzer zeit eine NullPointerExeption.

Zur Rrklärung mal der wichtigste Teil des Sourcecodes:

private boolean[][] generatenewmaze_Prims_algorithm(Selection s) {

        boolean[][] maze = new boolean[s.getWidth()][s.getLength()];

        ArrayList<Point> neighbours = new ArrayList<Point>();

        //wir setzen alle blöcke als wand

        for (int x = 0; x < maze.length; x++) {

            for (int y = 0; y < maze[x].length; y++) {

                maze[x][y] = true;

            }

        }

        //erstes element als weg markieren

        maze[1][1] = false;

        //nachbarn in die liste adden

        this.addaround(1, 1, maze, neighbours);

        while (!neighbours.isEmpty()) {

            int curpos = this.rand.nextInt(neighbours.size());

            Point cur = neighbours.get(curpos);

            if (maze[cur.x - 1][cur.y] == true) {

                maze[cur.x - 1][cur.y] = false;

                this.addaround(cur.x - 1, cur.y, maze, neighbours);

            } else if (maze[cur.x + 1][cur.y] == true) {

                maze[cur.x + 1][cur.y] = false;

                this.addaround(cur.x + 1, cur.y, maze, neighbours);

            } else if (maze[cur.x][cur.y - 1] == true) {

                maze[cur.x][cur.y - 1] = false;

                this.addaround(cur.x, cur.y - 1, maze, neighbours);

            } else if (maze[cur.x][cur.y + 1] == true) {

                maze[cur.x][cur.y + 1] = false;

                this.addaround(cur.x, cur.y + 1, maze, neighbours);

            }

            neighbours.remove(curpos);

        }


        return maze;


    }


    private void addaround(int x, int y, boolean[][] maze, ArrayList<Point> liste) {

        if (!this.isoutofrange(x, y + 1, maze)) {

            liste.add(new Point(x, y + 1));

        }

        if (!this.isoutofrange(x, y - 1, maze)) {

            liste.add(new Point(x, y - 1));

        }

        if (!this.isoutofrange(x + 1, y, maze)) {

            liste.add(new Point(x + 1, y));

        }

        if (!this.isoutofrange(x - 1, y, maze)) {

            liste.add(new Point(x - 1, y));

        }




    }


    private boolean isoutofrange(int x, int y, boolean[][] maze) {

        return x == 0 || y == 0 || x == maze.length - 1 || y == maze[x].length;

    }

wird nun allerdings eine 0 mit random generiert, kommt eine NullPointerExeption, und der algorithmus stürzt ab.

Nach einigen versuchen habe ich festgestellt, dass der fehler in der methode addaround passiert, allerdings habe ich keine idee, WARUM.

die idee zum algorithmus habe ich von Wikipedia (Maze generation algorithm - Wikipedia, the free encyclopedia)

Ich hoffe ihr könnt mir helfen den Fehler zu finden, ich habe ehrlich gesagt keine ahnung wo der fehler sein könnte.

Danke im Vorraus, Laubi

Können die Attribute x und y von Point auch 0 sein? Wenn ja, dann tritt auch eine java.lang.ArrayIndexOutOfBoundsException in isoutofrange() auf.

Für deine NullPointerException solltest du mal den kompletten Exception-Trace mit posten.

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.