我在所有JUnit上都得到空指针异常。在调试中 - 找不到源

时间:2014-11-21 14:12:28

标签: java junit nullpointerexception

由于NullPointerException,此JUnit测试(和其他)总是失败。当我调试时,它返回错误" Source not found"在线sq.setPlayers(球员); 下面是使用的类方法及其依赖项的列表。我无法想象为什么它不起作用。

public class SingleEliminationTest {

    private Queue queue;
    private Match currentMatch;
    private SingleElimination sq;


    public void setUp() {
        queue = new Queue(4);
        sq = new SingleElimination();
    }

    @Test
    public void setPlayers()
    {
        ArrayList<String> players = new ArrayList<String>();
        players.add("Max Atkins");
        players.add("Hannah Marlow");
        players.add("Liam Ross");
        players.add("Chandlar Bruce");
        sq.setPlayers(players);

        assertEquals("Not enough players", queue.length(), 4);
    }

public class SingleElimination implements IManager
{
    private Queue queue;
    private Match current;

    public SingleElimination()
    {
        queue = new Queue(5);
    }
    /**
     * Set the players or teams to use in the competition
     * @param players the players or teams
     */
    public void setPlayers(ArrayList<String> players)
    {
        for(String player : players)
        {
            queue.enQ(player);
        }
    }

public class Queue 
{
    Object[] queue;
    int head;
    int tail;
    int length;

    public Queue(int startSize)
    {
        queue = new Object[startSize];
        head = tail = length = 0;
    }

    // Adds an Object to the back of the queue. 
     public void enQ(Object o)
     {
         if(length == queue.length)
         {
             //queue = new Object[queue.length * 2];
         }
         queue[tail++] = o;
         length++;
         if(tail == queue.length)
         {
             tail = 0;
         }
     }

3 个答案:

答案 0 :(得分:3)

您需要注释setup方法,否则无法运行。我对JUnit不是很熟悉,但我怀疑@BeforeClass@Before会做你期望的事。

答案 1 :(得分:1)

如果您希望自己的设置方法在每次测试之前运行 (任何使用@Before注释的方法),请使用@Test注释,使用@BeforeClass如果您希望您的设置方法只运行一次,则注释。

答案 2 :(得分:0)

您需要使用setUp@Before方法添加注释。它没有被运行。