CDI Servlet java.lang.NullPointerException

时间:2014-02-21 10:24:10

标签: java servlets servlet-3.0

请帮助我!我开车真的很疯狂。我的代码在今天之前一直在运行,现在已经破了......

这是我的servlet:

@Stateless
class TempSession {
    void createFile( String fileName ) {
        try {
            FileWriter writer = new FileWriter( fileName );
            writer.close();
        }
        catch ( IOException e ) {
            e.printStackTrace();
        }
    }
}

@WebServlet( name = "CreteFileServlet", urlPatterns = "/createCSV" )
public class CreateCSVConfigurationServlet extends javax.servlet.http.HttpServlet {
    @Inject
    TempSession session;

    protected void doPost( javax.servlet.http.HttpServletRequest request,
            javax.servlet.http.HttpServletResponse response ) throws javax.servlet.ServletException, IOException {}

    protected void doGet( javax.servlet.http.HttpServletRequest request,
            javax.servlet.http.HttpServletResponse response ) throws javax.servlet.ServletException, IOException {
        session.createFile( "d:/session.txt" );
    }
}

这是web.xml:

<servlet>
    <servlet-name>CreateFileServlet</servlet-name>
    <servlet-class>com.bs.amg.test.servlets.CreateCSVConfigurationServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>CreateFileServlet</servlet-name>
    <url-pattern>/createCSV</url-pattern>
</servlet-mapping>

JSP片段:

<form action="createCSV" method="get">
    <input type="submit" value="Create configuration"/>
</form>

AnвJboss7。0。1日志服务器代码段:

14:10:52,471 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "ear-1.0.ear"
14:11:10,336 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/web].[CreteFileServlet]] (http--127.0.0.1-8080-2) Servlet.service() for servlet CreteFileServlet threw exception: java.lang.NullPointerException
    at com.bs.amg.test.servlets.CreateCSVConfigurationServlet.doGet(CreateCSVConfigurationServlet.java:136) [classes:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_25] 

拜托,我真的需要你的帮助!!!!!

2 个答案:

答案 0 :(得分:2)

您正在使用webservlet注释和web.xml映射声明您的servlet。您应该只使用注释中的一个声明,如this示例中那样。

答案 1 :(得分:1)

我认为由于未启用CDI,因此未进行注入。确保您有一个如CDI文档中所述的beans.xml:http://docs.oracle.com/javaee/6/tutorial/doc/gjbnz.html。以下是可用于启用CDI的示例beans.xml文件:

<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>