This question already has answers here:
What is a NullPointerException, and how do I fix it?

(12个答案)


4年前关闭。




我正在尝试使用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正在将random初始化为null,因此:

random.nextInt()


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

07-28 05:34