gpt4 book ai didi

java - DisjSet Maze Builder 中出现空指针异常,无法找出原因

转载 作者:行者123 更新时间:2023-12-01 10:49:18 26 4
gpt4 key购买 nike

我正在尝试使用 DisjSet 算法构建一个迷宫。我觉得我已经把它记下来了,但由于某种原因,我无法弄清楚为什么我在我正在使用的类之一中不断收到此空指针异常。任何帮助将不胜感激

这是迷宫类

package project3;

public class Maze2 {



private void nextRoom(int[] room, int wall, int n)
{

int row = wall / (2 * n - 1);
int column = wall % (2 * n - 1);


if (column < n - 1)
{

room[0] = n * row + column;
room[1] = n * row + column + 1;
}
else
{

column += 1 - n;

room[0] = n * row + column;
room[1] = n * (row + 1) + column;
}
}

private void mazeBuild(int m, int n)
{


DisjSets rooms = new DisjSets(m * n);

int wallNumber = 2 * m * n - m - n;
boolean[] is_wall = new boolean[wallNumber];
Permutation wallUntested = new Permutation(wallNumber);

for (int i = 0; i < wallNumber; ++i)
{
is_wall[i] = true;
}




while (rooms.s.length>1 )
{
int wall = wallUntested.next();
int[] room = new int[2];
nextRoom(room, wall, n);


if (rooms.find(room[0]) != rooms.find(room[1]))
{
is_wall[wall] = false;
rooms.union(room[0], room[1]);
}
}



System.out.print("+ +");

for (int i = 0; i < n - 1; ++i)
{
System.out.print("--+");
}

System.out.print("\n");

int wall_counter = 0;

for (int i = 0; i < m - 1; ++i)
{


System.out.print("|");

for (int j = 0; j < n - 1; ++j)
{
if (is_wall[wall_counter])
{
System.out.print(" |");
}
else
{
System.out.print(" ");
}

++wall_counter;
}

System.out.print(" |");
System.out.print("\n");



System.out.print("+");

for (int j = 0; j < n; ++j)
{
if (is_wall[wall_counter])
{
System.out.print("--+");
}
else
{
System.out.print(" +");
}

++wall_counter;
}

System.out.print("\n");
}


System.out.print("|");

for (int j = 0; j < n - 1; ++j)
{
if (is_wall[wall_counter])
{
System.out.print(" |");
}
else
{
System.out.print(" ");
}

++wall_counter;
}

System.out.print(" |");
System.out.print("\n");



for (int i = 0; i < n - 1; ++i)
{
System.out.print("+--");
}

System.out.print("+ +");
System.out.print("\n");
}


public static void main( String [] args){


Maze2 maze = new Maze2();


//input parameters here

maze.mazeBuild(3,3);



}
}

这是 DisjSet 类

package project3;

public class DisjSets
{

// DisjSets class
//
// CONSTRUCTION: with int representing initial number of sets
//
// ******************PUBLIC OPERATIONS*********************
// void union( root1, root2 ) --> Merge two sets
// int find( x ) --> Return set containing x
// ******************ERRORS********************************
// No error checking is performed

/**
* Disjoint set class, using union by rank and path compression.
* Elements in the set are numbered starting at 0.
* @author Mark Allen Weiss
*/

/**
* Construct the disjoint sets object.
* @param numElements the initial number of disjoint sets.
*/
public DisjSets( int numElements )
{
s = new int [ numElements ];
for( int i = 0; i < s.length; i++ )
s[ i ] = -1;
}

/**
* Union two disjoint sets using the height heuristic.
* For simplicity, we assume root1 and root2 are distinct
* and represent set names.
* @param root1 the root of set 1.
* @param root2 the root of set 2.
*/
public void union( int root1, int root2 )
{
if( s[ root2 ] < s[ root1 ] ) // root2 is deeper
s[ root1 ] = root2; // Make root2 new root
else
{
if( s[ root1 ] == s[ root2 ] )
s[ root1 ]--; // Update height if same
s[ root2 ] = root1; // Make root1 new root
}
}

/**
* Perform a find with path compression.
* Error checks omitted again for simplicity.
* @param x the element being searched for.
* @return the set containing x.
*/
public int find( int x )
{
if( s[ x ] < 0 )
return x;
else
return s[ x ] = find( s[ x ] );
}

public int [ ] s;



}

这是排列类

package project3;

import java.util.*;

public class Permutation{

private static Random random;

int capacity;
int bound;
int[] array;

public Permutation(int n)
{
this.capacity = n;
this.bound = (int) capacity;
this.array = new int[capacity];
for (int i = 0; i < capacity; ++i)
{
array[i] = i;
}
}



public final int next()
{
if (bound == 0)
{
return 0;
}

int index = random.nextInt() % bound;
int result = array[index];

--bound;
array[index] = array[bound];

return result;
}


public final void reset()
{
for (int i = 0; i < capacity; ++i)
{
array[i] = i;
}

bound = capacity;
}

}

错误是

Exception in thread "main" java.lang.NullPointerException
at project3.Permutation.next(Permutation.java:33)
at project3.Maze2.mazeBuild(Maze2.java:50)
at project3.Maze2.main(Maze2.java:159)

这一切都指向排列类中的这行代码

        int index = random.nextInt() % bound;

提前谢谢您

最佳答案

你需要这样的东西:

private static Random random = new Random();

或者你也可以将其定为最终版本:

private static final Random random = new Random();

Java 正在将随机值初始化为 null,因此:

random.nextInt()

生成一个 NPE,因为您尝试在 null 上调用方法。

关于java - DisjSet Maze Builder 中出现空指针异常,无法找出原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34013029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com