I am running into a blocking issue with my install (JDK 1.7). Basically I have the following NPE:
10:19:17.548 [main] ERROR o.s.t.w.s.TestDispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1007) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:289) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.createDefaultStrategy(DispatcherServlet.java:810) ~[spring-webmvc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.getDefaultStrategies(DispatcherServlet.java:779) ~[spring-webmvc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.initHandlerAdapters(DispatcherServlet.java:587) ~[spring-webmvc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.initStrategies(DispatcherServlet.java:444) ~[spring-webmvc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.onRefresh(DispatcherServlet.java:432) ~[spring-webmvc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:525) ~[spring-webmvc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459) ~[spring-webmvc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) [spring-webmvc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at javax.servlet.GenericServlet.init(GenericServlet.java:160) [tomcat-servlet-api-7.0.37.jar:3.0.FR]
at org.springframework.test.web.servlet.MockMvcBuilderSupport.createMockMvc(MockMvcBuilderSupport.java:53) [spring-test-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder.build(DefaultMockMvcBuilder.java:207) [spring-test-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at com.mycompany.ui.controller.ConnectionInfosControllerTest.setup(ConnectionInfosControllerTest.java:41) [test-classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_21]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_21]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_21]
at java.lang.reflect.Method.invoke(Method.java:601) ~[na:1.7.0_21]
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) [junit-4.11.jar:na]
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.11.jar:na]
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) [junit-4.11.jar:na]
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) [junit-4.11.jar:na]
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) [spring-test-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) [spring-test-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) [spring-test-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) [spring-test-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) [spring-test-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) [junit-4.11.jar:na]
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) [spring-test-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.junit.runners.ParentRunner.run(ParentRunner.java:309) [junit-4.11.jar:na]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) [spring-test-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) [.cp/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1000) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
... 44 common frames omitted
Caused by: java.lang.NullPointerException: null
at java.nio.charset.Charset.put(Charset.java:550) ~[na:1.7.0_21]
at java.nio.charset.Charset.access$300(Charset.java:276) ~[na:1.7.0_21]
at java.nio.charset.Charset$4.run(Charset.java:593) ~[na:1.7.0_21]
at java.nio.charset.Charset$4.run(Charset.java:585) ~[na:1.7.0_21]
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.7.0_21]
at java.nio.charset.Charset.availableCharsets(Charset.java:584) ~[na:1.7.0_21]
at org.springframework.http.converter.StringHttpMessageConverter.<init>(StringHttpMessageConverter.java:66) ~[spring-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.http.converter.StringHttpMessageConverter.<init>(StringHttpMessageConverter.java:56) ~[spring-web-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.<init>(AnnotationMethodHandlerAdapter.java:201) ~[spring-webmvc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_21]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_21]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_21]
at java.lang.reflect.Constructor.newInstance(Constructor.java:525) ~[na:1.7.0_21]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148) ~[spring-beans-3.2.3.RELEASE.jar:3.2.3.RELEASE]
... 46 common frames omitted
I strongly suspect it is related to the following bug: see report here
I am desperately looking for a workaround. Note that I haven't touched the jdk installation. I haven't added or removed any charset...
Can anyone help?
edit 1: See other bug report here and notice the "for some reason"...
edit 2: I have further info regarding the above issue: I have added the following test case in a separate project:
package pack;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Set;
public class TestCase {
public static void main(String[] args) {
Set set = Charset.availableCharsets().keySet();
for (Iterator iterator = set.iterator(); iterator.hasNext();) {
String s = (String) iterator.next();
System.out.println(s);
}
}
}
I noticed that the test case runs without any error!
Now when I add the very same test case to our project (built with maven), I run into the same NPE:
Exception in thread "main" java.lang.NullPointerException
at java.nio.charset.Charset.put(Charset.java:550)
at java.nio.charset.Charset.access$300(Charset.java:276)
at java.nio.charset.Charset$4.run(Charset.java:593)
at java.nio.charset.Charset$4.run(Charset.java:585)
at java.security.AccessController.doPrivileged(Native Method)
at java.nio.charset.Charset.availableCharsets(Charset.java:584)
at pack.TestCase.main(TestCase.java:9)
I therefore suspect it might be related to our config of maven. Any one has any clue of what could cause this Charset issue in a multimodule maven config?
edit 3: The effective POM is too long. Here is the pastebin for it: pastebin here
edit 4: I managed to pinpoint the problematic dependency. Here it is:
<dependency>
<groupId>com.sybase</groupId>
<artifactId>jconn4</artifactId>
<version>7.07.GA</version>
<scope>runtime</scope>
</dependency>
By removing this dependency, the TestCase executes properly and prints out all available charsets. However, we do need a database driver for our app. Any one has got any clue as to what is going wrong with sybase jdbc driver?
It turns out to be a bug in the Sybase JDBC driver as one can see here. To quote the disgruntled user of the Sybase JDBC driver from the above link:
Nothing should suprise me though, as the current Sybase driver is utterly broken, and has been for over a year! It contains a CharsetProvider, that returns a null iterator causing Charset.availableCharsets() to throw a nullpointer exception. A lot of frameworks such as Spring calls Charset.availableCharsets().
I am not sure how to report this bug to Sybase/SAP though...
Not sure if anyone still looking for the answer or not...But yes, this is a bug in Sybase driver jar. There is a discussion and solution within this link Discussion of NPE with jconn4.jar in SAP. I just faced the same issue and got resolved by replacing with a newer jar(it turned out we were using the very old one.)
来源:https://stackoverflow.com/questions/16979586/nullpointerexception-thrown-by-charset-availablecharsets-due-to-our-sybase-jdbc