mercredi 15 mars 2017

Java JUnit Test - Java.Lang.NullPointerException

I am quite new to Java and JUnit testing and am very confused with an error I am getting. The error, Null Pointer exception as the code below I am guessing is because something is equal to null but i am unsure why.

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

This is the code containing the methods:

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();

}

Thanks for your help :)

Aucun commentaire:

Enregistrer un commentaire