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

NPE in StdDateFormat hashCode and equals #1130

Closed
kazuki43zoo opened this issue Feb 16, 2016 · 4 comments
Closed

NPE in StdDateFormat hashCode and equals #1130

kazuki43zoo opened this issue Feb 16, 2016 · 4 comments
Milestone

Comments

@kazuki43zoo
Copy link

Occur NullPointerException in hashCode() and equals(Object) method.
Please implements these methods correctly.

Thanks.

Test Cases

@Test
public void testHashCode() {
    StdDateFormat.instance.hashCode();
}

@Test
public void testEquals() {
    StdDateFormat.instance.equals(new StdDateFormat());
}

Stack Traces

java.lang.NullPointerException
    at java.text.DateFormat.hashCode(DateFormat.java:739)
        ...
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
java.lang.NullPointerException
    at java.text.DateFormat.equals(DateFormat.java:751)
        ...
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
@cowtowncoder
Copy link
Member

I would like to see more of stack trace to know why these methods are invoked.

@cowtowncoder cowtowncoder changed the title NPE in hashCode and equals NPE in StdDateFormat hashCode and equals Feb 16, 2016
@kazuki43zoo
Copy link
Author

It is a small sample for repro. For example, NPE is occurred when add the StdDateFormat instance into HashSet.

public class FooTest {
    @Test
    public void testHashCode() {
        Set<DateFormat> set = new HashSet<>();
        set.add(StdDateFormat.instance);
    }
}
  • Full stak trace
java.lang.NullPointerException
    at java.text.DateFormat.hashCode(DateFormat.java:739)
    at java.util.HashMap.hash(HashMap.java:338)
    at java.util.HashMap.put(HashMap.java:611)
    at java.util.HashSet.add(HashSet.java:219)
    at com.github.kazuki43zoo.sample.FooTest.testHashCode(FooTest.java:20)
    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:497)
    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.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    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:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

I tried same operation using the SimpleDateFormat.

@Test
public void testHashCode2() {
    Set<DateFormat> set = new HashSet<>();
    set.add(new SimpleDateFormat("yyyyMMdd"));
}

This code is no problem.

@cowtowncoder
Copy link
Member

Ok, went ahead and added minimal code to avoid NPE.

@kazuki43zoo
Copy link
Author

Thanks for fix !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants