0

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

Here is the test file:

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

    }

}

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 :)

eis
  • 51,991
  • 13
  • 150
  • 199
  • `assertEquals(greenLeftArrow.isOn(), true);` where do you assing an instance to this variable? – Timothy Truckle Mar 15 '17 at 12:42
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Timothy Truckle Mar 15 '17 at 12:43
  • Please [edit] your code to reduce it to a [mcve] of your problem. Your current code includes much that is peripheral to your problem - a minimal sample shouldn't need anything like as much code. – Toby Speight Mar 15 '17 at 12:48

2 Answers2

2

greenLeftArrow is not initialized to a value (it's automatically initialized to null) so calling greenLeftArrow.isOn() in the PhaseOneInitialization method will throw a NullPointerException.

Joe
  • 320
  • 1
  • 7
1

You should initialize greenLeftArrow object first like you did for example greenLight. You cannot call methods on not initialized objects.

You can also use assertTrue or assertFalse to simplify your code.