This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12个答案)
4年前关闭。
所以我没什么问题。我无法将信息添加到数据库,因为出现错误:
这是我的控制器:
我的SamochodDao课:
还有我的servlet.xml:
当我尝试向数据库添加新的“ Samochod”时出现错误:
也许有人知道我在做什么错?
@编辑
SamochodDao的第26行:
@ edit2
顺便说一句。当我运行此代码作为测试时,它可以正常工作。这是我从正常应用中得到的豆:
但是,当我将其更改为网络应用程序时,会出现错误。
(12个答案)
4年前关闭。
所以我没什么问题。我无法将信息添加到数据库,因为出现错误:
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
顺便说一句。当我运行此代码作为测试时,它可以正常工作。这是我从正常应用中得到的豆:
<?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>
但是,当我将其更改为网络应用程序时,会出现错误。
最佳答案
看起来jdbcTemplate是nullValue,因为它是在控制器中创建的。
尝试在上下文中创建一次Dao实例,然后自动连接到控制器中。
07-27 18:15