Java JUnit Test - Java.Lang.NullPointerException

时间:2017-03-15 12:36:45

标签: java testing junit null

我是Java和JUnit测试的新手,我对我遇到的错误非常困惑。错误,Null指针异常作为我猜的下面的代码是因为某些东西等于null但我不确定为什么。

java.lang.NullPointerException
at com.nsa.y1.trafficlights.FourWayJunctionTest.PhaseOneInitiation(FourWayJunctionTest.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:114)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:377)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745) com.nsa.y1.trafficlights.FourWayJunctionTest > PhaseOneInitiation FAILED
java.lang.NullPointerException at FourWayJunctionTest.java:47

这是测试文件:

package com.nsa.y1.trafficlights;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * Created by c167 on 12/03/2017.
 */
public class FourWayJunctionTest {

    private Light greenLight, amberLight, redLight, greenRightArrow, greenLeftArrow;

    private FourLightTrafficLight turnRightTrafficLight;

    boolean lightStateRed;
    boolean lightStateAmber;
    boolean lightStateGreen;

    private TrafficLight northLeftStraight;
    private FourLightTrafficLight northLeftArrow;
    private FourLightTrafficLight northRightArrow;
    private TrafficLight eastLeftStraight;
    private TrafficLight westStraightRight;
    private FourWayJunction junction = new FourWayJunction();



    @Before
    public void createLights() throws Exception {
        greenLight = (new Light(Shape.CIRCLE, Colour.GREEN));
        amberLight = (new Light(Shape.CIRCLE, Colour.AMBER));
        redLight = (new Light(Shape.CIRCLE, Colour.RED));
        northRightArrow = new FourLightTrafficLight();
        northLeftArrow = new FourLightTrafficLight();
        northLeftStraight = new TrafficLight();
        eastLeftStraight = new TrafficLight();
        westStraightRight = new TrafficLight();
    }

    @Test
    public void PhaseOneInitiation() throws Exception {
        createLights();
        //Greenleftarrow should be on, northleft on, and eat left on. All others off.
        junction.initiatePhaseOne();
        assertEquals(greenLeftArrow.isOn(), true);

    }

}

这是包含方法的代码:

package com.nsa.y1.trafficlights;

/**
 * Created by on 13/03/2017.
 */
public class FourWayJunction extends FourLightTrafficLight{

    // Evans junction recreation in cardiff

    private Light greenLight, amberLight, redLight, greenRightArrow, greenLeftArrow;

    private TrafficLight oppositeTrafficLight;
    private FourLightTrafficLight turnRightTrafficLight;

    boolean lightStateRed;
    boolean lightStateAmber;
    boolean lightStateGreen;

    private TrafficLight northLeftStraight;
    private FourLightTrafficLight northLeftArrow;
    private FourLightTrafficLight northRightArrow;
    private TrafficLight eastLeftStraight;
    private TrafficLight westStraightRight;

    public FourWayJunction() {
        greenLight = (new Light(Shape.CIRCLE, Colour.GREEN));
        amberLight = (new Light(Shape.CIRCLE, Colour.AMBER));
        redLight = (new Light(Shape.CIRCLE, Colour.RED));
        northRightArrow = new FourLightTrafficLight();
        northLeftArrow = new FourLightTrafficLight();
        northLeftStraight = new TrafficLight();
        eastLeftStraight = new TrafficLight();
        westStraightRight = new TrafficLight();
    }

    public void initiatePhaseOne() {
        // Left arrow for buses and taxis on, north green light for left on but no right arrow.
        // Also green light on for the East Traffic light.
        // All others off.
        westStraightRight.getRedLight().turnOn();
        northRightArrow.getGreenLight().turnOff();
        if (westStraightRight.getRedLight().isOn() && !northRightArrow.getGreenLight().isOn()){

            northLeftArrow.getGreenLight().turnOn();
            northLeftStraight.setTrafficLightOn(northLeftStraight);
            eastLeftStraight.setTrafficLightOn(eastLeftStraight);
        }

        else {
            System.out.println("Problems, traffic wil collide");
            westStraightRight.setTrafficLightOff(westStraightRight);
            northRightArrow.getGreenLight().turnOff();
        }

    }

    public void initiatePhaseTwo() {
        // North left straight, left arrow, and right arrow are on.
        // West straight right light off.
        // East Left Straight light is off.
        if (!eastLeftStraight.getRedLight().isOn()) {
            eastLeftStraight.setTrafficLightOff(eastLeftStraight);
            northRightArrow.getGreenLight().turnOn();
        }

        else {
            northRightArrow.getGreenLight().turnOn();
        }
    }

    public void initiatePhaseThree() {
        // All lights are off except for the EastStraightRight light.
        if (northRightArrow.getGreenLight().isOn() && !northLeftStraight.getRedLight().isOn() &&
                northLeftArrow.getGreenLight().isOn()) {

            northRightArrow.getGreenLight().turnOff();
            northLeftArrow.getGreenLight().turnOff();
            northLeftStraight.setTrafficLightOff(northLeftStraight);
        }

        else {
            eastLeftStraight.setTrafficLightOn(eastLeftStraight);
        }
    }

    public FourLightTrafficLight getTrafficLight(FourLightTrafficLight light) {
        return light;
    }

}

        public void setTrafficLightOn(TrafficLight trafficLight) {
        trafficLight.getRedLight().turnOff();
        LightPause();
        trafficLight.getAmberLight().turnOn();
        LightPause();
        trafficLight.getGreenLight().turnOn();
    }

    public void setTrafficLightOff(TrafficLight trafficLight) {
        trafficLight.getGreenLight().turnOff();
        LightPause();
        trafficLight.getGreenLight().turnOff();
        trafficLight.getAmberLight().turnOn();
        LightPause();
        trafficLight.getRedLight().turnOn();

    }

感谢您的帮助:)

2 个答案:

答案 0 :(得分:1)

greenLeftArrow未初始化为某个值(它会自动初始化为null),因此在PhaseOneInitialization方法中调用greenLeftArrow.isOn()将抛出NullPointerException。

答案 1 :(得分:1)

您应该首先初始化greenLeftArrow对象,例如greenLight。您无法在未初始化的对象上调用方法。

您还可以使用assertTrueassertFalse来简化代码。