DisjSet Maze Builder 中的空指针异常,无法弄清楚原因
Null Pointer Excpetion In DisjSet Maze Builder and can't figure out why
我正在尝试使用 DisjSet 算法构建迷宫。我觉得我已经搞定了,但出于某种原因,我无法弄清楚为什么我在使用的 classes 之一中不断收到此空指针异常。任何帮助将不胜感激
这是迷宫class
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 class
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;
}
这是排列class
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)
并且都指向排列中的这一行代码 class
int index = random.nextInt() % bound;
提前致谢
你需要这样的东西:
private static Random random = new Random();
或者您也可以将其设为最终版本:
private static final Random random = new Random();
Java 正在将随机初始化为 null,因此:
random.nextInt()
生成一个 NPE,因为您正试图在 null 上调用方法。
我正在尝试使用 DisjSet 算法构建迷宫。我觉得我已经搞定了,但出于某种原因,我无法弄清楚为什么我在使用的 classes 之一中不断收到此空指针异常。任何帮助将不胜感激
这是迷宫class
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 class
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;
}
这是排列class
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)
并且都指向排列中的这一行代码 class
int index = random.nextInt() % bound;
提前致谢
你需要这样的东西:
private static Random random = new Random();
或者您也可以将其设为最终版本:
private static final Random random = new Random();
Java 正在将随机初始化为 null,因此:
random.nextInt()
生成一个 NPE,因为您正试图在 null 上调用方法。