HTTP 状态 500 - 请求处理失败。嵌套异常是 java.lang.NullPointerException [重复]

Posted

技术标签:

【中文标题】HTTP 状态 500 - 请求处理失败。嵌套异常是 java.lang.NullPointerException [重复]【英文标题】:HTTP Status 500 - Request processing failed. Nested exception is java.lang.NullPointerException [duplicate] 【发布时间】:2015-12-07 23:13:21 【问题描述】:

所以我有一点问题。由于出现错误,我无法将信息添加到数据库:

HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException

这是我的控制器:

package pl.edu.controller;

@Controller
public class StudentAdmissionController 

    @RequestMapping(value = "/admissionForm.html", method = RequestMethod.GET)
    public ModelAndView getAdmissionForm() 

        ModelAndView model = new ModelAndView("AdmissionForm");

        return model;
    

    @RequestMapping(value = "/submitAdmissionForm.html", method = RequestMethod.POST)
    public ModelAndView submitAdmissionForm(@RequestParam("pojemnosc") float pojemnosc,
            @RequestParam("modelSamochodu") String modelSamochodu, @RequestParam("liczbaPasazerow") int liczbaPasazerow,
            @RequestParam("liczbaDrzwi") int liczbaDrzwi) 

        ModelAndView model = new ModelAndView("AdmissionSuccess");

        Silnik silnik = new Silnik();
        silnik.setPojemnosc(pojemnosc);
        silnik.setId(1);

        Samochod samochod = new Samochod();
        samochod.setSilnik(silnik);
        samochod.setLiczbaDrzwi(liczbaDrzwi);
        samochod.setLiczbaPasazerow(liczbaPasazerow);
        samochod.setModel(modelSamochodu);
        samochod.setLiczbaPoduszek(liczbaPasazerow * 2);
        samochod.setId(1);

        model.addObject("msg",
                "Details submited by you: Pojemnosc: " + silnik.getPojemnosc() + " Model: " + samochod.getModel()
                        + ", Liczba drzwi: " + samochod.getLiczbaDrzwi() + ", Liczba pasazerow: "
                        + samochod.getLiczbaPasazerow() + ", Liczba poduszek: " + samochod.getLiczbaPoduszek());

        SamochodDao samochodDao = new SamochodDao();
        samochodDao.dodaj(samochod);

        return model;
    

我的 SamochodDao 课程:

public class SamochodDao 
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setDataSource(DataSource dataSource) 
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    

    @Transactional
    public void dodaj(final Samochod samochod) 
        final String engineQuery = "insert into silnik (pojemnosc) values (?)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() 
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException 
                PreparedStatement ps = connection.prepareStatement(engineQuery, new String[]  "id" );
                ps.setFloat(1, samochod.getSilnik().getPojemnosc());
                return ps;
            
        , keyHolder);

        String carQuery = "insert into samochod (id, silnik,model,liczbaDrzwi,liczbaPasazerow,liczbaPoduszek) values (?,?,?,?,?,?)";
        Object[] inputs = new Object[]  keyHolder.getKey(), keyHolder.getKey(), samochod.getModel(),
                samochod.getLiczbaDrzwi(), samochod.getLiczbaPasazerow(), samochod.getLiczbaPoduszek() ;
        jdbcTemplate.update(carQuery, inputs);
    

    public void usun(int id) 
        String query = "DELETE FROM samochod WHERE id = ?";
        Object[] inputs = new Object[]  id ;
        jdbcTemplate.update(query, inputs);
        query = "DELETE FROM silnik WHERE id = ?";
        jdbcTemplate.update(query, inputs);
    

    @SuppressWarnings( "rawtypes", "unchecked" )
    public List<Samochod> listuj() 
        String sql = "SELECT * FROM Silnik";
        List<Silnik> listaSilnikow = jdbcTemplate.query(sql, new BeanPropertyRowMapper(Silnik.class));
        List<Samochod> listaSamochodow = new ArrayList();

        for (Silnik silnik : listaSilnikow) 
            sql = "SELECT * FROM samochod WHERE id = ?";
            Samochod samochod = (Samochod) jdbcTemplate.queryForObject(sql, new Object[]  silnik.getId() ,
                    new SamochodRowMapper());
            samochod.setSilnik(silnik);
            listaSamochodow.add(samochod);
        
        return listaSamochodow;
    

还有我的 servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="pl.edu" />
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean> 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/spring" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean> 

</beans>

当我尝试将新的“Samochod”添加到数据库时出现错误:

HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException

type Exception report

message Request processing failed; nested exception is java.lang.NullPointerException

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

java.lang.NullPointerException
    pl.edu.dao.SamochodDao.dodaj(SamochodDao.java:26)
    pl.edu.controller.StudentAdmissionController.submitAdmissionForm(StudentAdmissionController.java:48)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:497)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

也许有人知道我做错了什么?

@编辑

来自 SamochodDao 的第 26 行:

jdbcTemplate.update(new PreparedStatementCreator() 

@edit2 顺便提一句。当我将此代码作为测试运行时,它可以正常工作。这是我的普通应用程序中的 bean:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">

    <aop:aspectj-autoproxy />

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="Silnik" class="pl.edu.lab1.Silnik">
        <property name="pojemnosc" value="1.4"></property>
        <property name="id" value="1"></property>
    </bean>
    <bean id="Samochod" class="pl.edu.lab1.Samochod">
        <property name="silnik" ref="Silnik"></property>
        <property name="model" value="Audi"></property>
        <property name="liczbaPasazerow" value="2"></property>
        <property name="liczbaDrzwi" value="4"></property>
        <property name="liczbaPoduszek" value="2"></property>
        <property name="id" value="1"></property>
    </bean>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/spring" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>
    <bean id="SamochodDao" class="pl.edu.dao.SamochodDao">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

但是当我将其更改为 web-app 时出现错误。

【问题讨论】:

我编辑了我的帖子并添加了 Line26。 这么多代码,你确定不能减少你的例子吗? 【参考方案1】:

看起来 jdbcTemplate 是 nullValue 因为它是在控制器中创建的。 尝试在上下文中创建一次 Dao 实例并自动装配到控制器中。

【讨论】:

有效!非常感谢!

以上是关于HTTP 状态 500 - 请求处理失败。嵌套异常是 java.lang.NullPointerException [重复]的主要内容,如果未能解决你的问题,请参考以下文章

HTTP 状态 500 - 处理程序处理失败;嵌套异常是 java.lang.UnsatisfiedLinkError: 找不到指定的模块

使用dubbo引用和发布服务时出现的异常:HTTP状态500 - 请求处理失败; 嵌套异常是com.alibaba.dubbo.rpc.RpcException:无法在服务cn.e3mall.serv

HTTP 状态 500 - 意外令牌:,靠近第 1 行,第 129 列

HTTP 状态 500 - 请求处理失败

反序列化问题

Axios POST 请求失败,错误状态码 500: Internal Server error