Re: [EqualsVerifier] EqualsVerifyer throws NullPointerException

129 views
Skip to first unread message
Message has been deleted

Jan Ouwens

unread,
May 15, 2013, 4:45:22 AM5/15/13
to equalsv...@googlegroups.com
Hi Matthias,

Thanks for your question. Unfortunately, I don't have enough information to give you a good answer. Can you please post a self-contained class that reproduces this problem, and also run EV with .debug(), like this:

EqualsVerifier.forClass(BSValidationDataTypesObjectImpl.class).suppress(Warning.NULL_FIELDS).debug().verify();

And then post the stacktrace that is printed to the console?


Thanks!
Jan




On 15 May 2013 09:19, Matthias Dillier <matthias...@css.ch> wrote:
Hi
 
I just started to use EqualsVerifyer to test some of our classes with overwritten equals and hashCode methods. I use the following testcode:

 

@Test

public void equalsContractBSValidationDataTypesObjectImpl() {

EqualsVerifier.forClass(BSValidationDataTypesObjectImpl.

class).suppress(Warning.NULL_FIELDS).verify();

}

 
With this test, I get a NullPointerException:
 
java.lang.AssertionError: java.lang.NullPointerException:
For more information, go to: http://code.google.com/p/equalsverifier/wiki/ErrorMessages
 at nl.jqno.equalsverifier.util.Assert.fail(Assert.java:80)
 at nl.jqno.equalsverifier.EqualsVerifier.handleError(EqualsVerifier.java:355)
 at nl.jqno.equalsverifier.EqualsVerifier.verify(EqualsVerifier.java:343)
 at ch.css.sample.java.dto.impl.EqualsVerify.equalsContractBSPartnerQueryReadNatPersonOutImpl(EqualsVerify.java:28)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
 at java.lang.reflect.Method.invoke(Method.java:611)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
 
Do you have any tip to solve this?
 
Thank you and kind regards
Matthias
 

--
You received this message because you are subscribed to the Google Groups "equalsverifier" group.
To unsubscribe from this group and stop receiving emails from it, send an email to equalsverifie...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Matthias Dillier

unread,
May 16, 2013, 12:56:02 PM5/16/13
to equalsv...@googlegroups.com
Hi Jan
 
Here is an example-class:
 

package

ch.css.sample.java.dto.impl;

import

java.io.Serializable;

import

java.util.HashSet;

import

java.util.Set;

public

class TttImpl implements Serializable {

private static final String modelVersion = "10.0";

private static final String interfaceVersion = "10.0";

private static final long serialVersionUID = 10000L;

/**

* a Set of names of member variables to be excluded for comparison.

*

<p>

* the excluded members are ignored by the equals and hashCode() methods.

*/

private static Set excludedMembersForComparison = new HashSet();

/**

* used in comparison methods to avoid cycles

*/

private static ThreadLocal<Boolean> alreadyCompared = new ThreadLocal<Boolean>() {

@Override

protected Boolean initialValue() {

return Boolean.FALSE;

}

};

/**

*

@param aSet

* the Set with member names to be excluded for comparison

*

@see excludedMembersForComparison

*/

public static void setExcludedMemberForComparison(Set aSet) {

if (aSet != null) {

excludedMembersForComparison = aSet;

}

else {

excludedMembersForComparison = new HashSet();

}

}

/**

* adds another member to the exclude Set

*

*

@param memberName

* the name of the member to be excluded for comparison

*

@see excludedMembersForComparison

*/

public static void addExcludedMemberForComparison(String memberName) {

excludedMembersForComparison.add(memberName);

}

/**

* Default Ctor

*/

public TttImpl() {

super();

}

/**

* Factory method, only called by the factory implementation in the same package

*/

static TttImpl createTttImpl() {

return new TttImpl();

}

/**

*

@see java.lang.Object#equals(java.lang.Object)

*/

@Override

public boolean equals(Object obj) {

if (alreadyCompared.get().booleanValue()) {

return true;

}

try {

alreadyCompared.set(Boolean.TRUE);

if (obj == null || !(obj instanceof TttImpl)) {

return false;

}

TttImpl thisObj = (TttImpl) obj;

}

finally {

alreadyCompared.set(Boolean.FALSE);

}

return true;

}

/**

*

@see java.lang.Object#hashCode()

*/

@Override

public int hashCode() {

int hashCode = 3;

if (alreadyCompared.get().booleanValue()) {

return hashCode;

}

try {

alreadyCompared.set(Boolean.TRUE);

}

finally {

alreadyCompared.set(Boolean.FALSE);

}

return hashCode;

}

}

 

StackTrace:

 

java.lang.AssertionError: java.lang.NullPointerException:
For more information, go to: http://code.google.com/p/equalsverifier/wiki/ErrorMessages
 at nl.jqno.equalsverifier.util.Assert.fail(Assert.java:80)
 at nl.jqno.equalsverifier.EqualsVerifier.handleError(EqualsVerifier.java:355)
 at nl.jqno.equalsverifier.EqualsVerifier.verify(EqualsVerifier.java:343)

 at ch.css.sample.java.dto.impl.EqualsVerify.equalsContractTttImpl(EqualsVerify.java:52)


 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
 at java.lang.reflect.Method.invoke(Method.java:611)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

 

Debug output:

java.lang.NullPointerException
 at ch.css.sample.java.dto.impl.TttImpl.equals(TttImpl.java:72)
 at nl.jqno.equalsverifier.util.Assert.assertEquals(Assert.java:40)
 at nl.jqno.equalsverifier.ExamplesChecker.checkReflexivity(ExamplesChecker.java:108)
 at nl.jqno.equalsverifier.ExamplesChecker.checkSingle(ExamplesChecker.java:88)
 at nl.jqno.equalsverifier.ExamplesChecker.check(ExamplesChecker.java:67)
 at nl.jqno.equalsverifier.EqualsVerifier.verifyWithExamples(EqualsVerifier.java:393)
 at nl.jqno.equalsverifier.EqualsVerifier.performVerification(EqualsVerifier.java:364)
 at nl.jqno.equalsverifier.EqualsVerifier.verify(EqualsVerifier.java:334)
 at ch.css.sample.java.dto.impl.EqualsVerify.equalsContractTttImpl(EqualsVerify.java:52)


 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
 at java.lang.reflect.Method.invoke(Method.java:611)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Hope, this helps

Regards

Matthias

 

 

Jan Ouwens

unread,
May 20, 2013, 6:39:56 AM5/20/13
to equalsv...@googlegroups.com
Hi Matthias,

I've taken a look at your issue. You can fix the NullPointerException by adding a .withPrefabValues for the ThreadLocal, sort of like this:

    class BooleanThreadLocal extends ThreadLocal<Boolean> {
        private final boolean bool;
       
        public BooleanThreadLocal(boolean bool) {
            this.bool = bool;
        }
       
        @Override
        protected Boolean initialValue() {
            return bool;
        }
    }
   
    @Test
    public void testTtt() {
        EqualsVerifier.forClass(TttImpl.class)
                .suppress(Warning.NULL_FIELDS)
                .withPrefabValues(ThreadLocal.class, new BooleanThreadLocal(true), new BooleanThreadLocal(false))
                .verify();
    }

I was unable to make your class pass the test as a whole, though, since it doesn't seem to actually compare (or even have) any fields. Also, I really don't understand what you're trying to achieve with the threadlocal in the first place, so I'm not sure what other advice I can give you to make it work.


Good luck!
Jan


Matthias Dillier

unread,
May 31, 2013, 3:47:12 AM5/31/13
to equalsv...@googlegroups.com
Hi Jan
 
Thank you for the answer. This seems to work. Now I can see some problems with the equals method itself.
 
Regards
Matthias
Reply all
Reply to author
Forward
0 new messages