我正在尝试使用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;
提前感谢
答案 0 :(得分:1)
您需要以下内容:
private static Random random = new Random();
或者你也可以最终决定:
private static final Random random = new Random();
Java正在将random初始化为null,因此:
random.nextInt()
生成NPE,因为您尝试在null上调用方法。