Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException during equals #201

Closed
2kewl4u opened this issue Jul 16, 2018 · 2 comments
Closed

NullPointerException during equals #201

2kewl4u opened this issue Jul 16, 2018 · 2 comments
Labels

Comments

@2kewl4u
Copy link

2kewl4u commented Jul 16, 2018

What steps will reproduce the problem?

Using a maven project with the following configuration, the test will throw a NullPointerException.
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T20:33:14+02:00)
Java version: 9.0.4, vendor: Oracle Corporation
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>testbench</groupId>
	<artifactId>testbench</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<maven.compiler.source>9</maven.compiler.source>
		<maven.compiler.target>9</maven.compiler.target>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>nl.jqno.equalsverifier</groupId>
			<artifactId>equalsverifier</artifactId>
			<version>2.4.8</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

</project>

What is the code that triggers this problem?

testbench/Bakery.java

package testbench;

public abstract class Bakery {

    protected Object flour;

    private Object flour() {
        if (flour == null) {
            synchronized (this) {
                if (flour == null) {
                    flour = createFlour();
                }
            }
        }
        return flour;
    }

    protected abstract Object createFlour();

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (!(obj instanceof Bakery)) return false;
        return flour().equals(((Bakery) obj).flour());
    }

    @Override
    public int hashCode() {
        return flour().hashCode();
    }

}

testbench/PizzaBakery.java

package testbench;

import static java.util.Objects.requireNonNull;

import java.util.function.Supplier;


public class PizzaBakery extends Bakery {

    private final Supplier<Object> flourFactory;

    protected PizzaBakery(final Supplier<Object> flourFactory) {
        this.flourFactory = requireNonNull(flourFactory);
    }

    @Override
    protected Object createFlour() {
        return flourFactory.get();
    }

    public static Bakery newBakery() {
        return new PizzaBakery(Object::new);
    }
    
}

testbench/BakeryTest.java

package testbench;

import org.junit.Test;

import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;


public class BakeryTest {

    @Test
    public void testEquals() throws Exception {
        EqualsVerifier.forClass(PizzaBakery.class)
                .suppress(Warning.NULL_FIELDS, Warning.STRICT_INHERITANCE, Warning.NONFINAL_FIELDS).verify();
    }
}

What error message or stack trace does EqualsVerifier give?

java.lang.AssertionError: NullPointerException: 
For more information, go to: http://www.jqno.nl/equalsverifier/errormessages
	at nl.jqno.equalsverifier.EqualsVerifier.handleError(EqualsVerifier.java:375)
	at nl.jqno.equalsverifier.EqualsVerifier.verify(EqualsVerifier.java:364)
	at testbench.BakeryTest.testEquals(BakeryTest.java:14)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.NullPointerException
	at testbench.PizzaBakery.createFlour(PizzaBakery.java:18)
	at testbench.Bakery.flour(Bakery.java:11)
	at testbench.Bakery.equals(Bakery.java:25)
	at nl.jqno.equalsverifier.internal.checkers.HierarchyChecker.checkSuperProperties(HierarchyChecker.java:96)
	at nl.jqno.equalsverifier.internal.checkers.HierarchyChecker.safelyCheckSuperProperties(HierarchyChecker.java:86)
	at nl.jqno.equalsverifier.internal.checkers.HierarchyChecker.checkSuperclass(HierarchyChecker.java:74)
	at nl.jqno.equalsverifier.internal.checkers.HierarchyChecker.check(HierarchyChecker.java:45)
	at nl.jqno.equalsverifier.EqualsVerifier.verifyWithExamples(EqualsVerifier.java:425)
	at nl.jqno.equalsverifier.EqualsVerifier.performVerification(EqualsVerifier.java:387)
	at nl.jqno.equalsverifier.EqualsVerifier.verify(EqualsVerifier.java:358)
	... 24 more

What did you expect?

A clear error message or a passed test. I can only fix the verification if I exclude the EqualsVerifier from my test.

Which version of EqualsVerifier are you using?

2.4.8

Please provide any additional information below.

The flourFactory used in PizzaBakery.createFlour() can only be null if the object was created without calling the constructor. Interestingly enough when using the following configuration with java 8, the test passes.

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>testbench</groupId>
	<artifactId>testbench</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>nl.jqno.equalsverifier</groupId>
			<artifactId>equalsverifier</artifactId>
			<version>1.7.2</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

</project>
@jqno
Copy link
Owner

jqno commented Jul 26, 2018

Sorry for the late reply, I was on vacation. I've reproduced the issue and I'll fix it in the next release.

@jqno
Copy link
Owner

jqno commented Jul 30, 2018

I just released version 2.5, which should fix this issue.

@jqno jqno closed this as completed Jul 30, 2018
akhalikov pushed a commit to akhalikov/equalsverifier that referenced this issue Nov 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants