我在尝试启动我的应用程序时遇到了困难,我在几天内找到了我的错误,但我被困在代码中的某处并请求您的帮助...谢谢
添加客户COntrller:
package com.witlab.controller;
import com.witlab.services.CustomerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.witlab.services.CustomerService;
import com.witlab.customer.Customer;
import org.springframework.beans.factory.annotation.Autowired;
@Controller
public class AddCustomerController {
@Autowired
CustomerService customerservice;
@RequestMapping(value="/AddCustomer", method = RequestMethod.GET)
public String addcustomer(@ModelAttribute Customer customer) {
return "AddCustomer";
}
@RequestMapping(value ="InsertCustomer", method = RequestMethod.POST)
public String insertCustomer(@ModelAttribute ("customer") Customer customer, ModelMap model)
{ System.out.println(customer.getAddress());
if (customer!=null)
{
System.out.println(customer.getName());
System.out.println(customer.getCity());
System.out.println(customer.getPhone());
System.out.println(model.addAttribute("Name", customer.getName()));
model.addAttribute("Name", customer.getName());
model.addAttribute("Address", customer.getAddress());
model.addAttribute("city", customer.getCity());
model.addAttribute("phone", customer.getPhone());
model.addAttribute("mobile", customer.getMobile());
customerservice.createCustomer(customer);
}
else
{
System.out.print("Error");
}
return "AddCustomer";
}
}
dispatcher-servlet:
<context:annotation-config />
<context:component-scan base-package="com.witlab.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean id="CustomerDAO" class="com.witlab.customer.CustomerJDBCTemplate" />
<bean id="customerService" class="com.witlab.services.CustomerServiceImpl" />
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />
</beans>
客户服务:
package com.witlab.services;
import com.witlab.customer.Customer;
import java.util.List;
public interface CustomerService {
public void createCustomer(Customer customer);
public Customer getCustomer(double Id);
public List<Customer> getCustomers();
public void deleteCustomer(double Id);
public void updateCustomer(double Id, String Name, String Address, String city, String phone, String mobile);
}
客户JDBCTemplate:
package com.witlab.customer;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.instrument.classloading.jboss.JBossLoadTimeWeaver;
import org.springframework.jdbc.core.JdbcTemplate;
public class CustomerJDBCTemplate implements CustomerDAO{
@Autowired
DataSource dataSource;
private JdbcTemplate jdbcTemplateobj;
public void setDataSource(DataSource dataSource)
{
this.dataSource = dataSource;
this.jdbcTemplateobj = new JdbcTemplate(dataSource);
}
public void createCustomer(Customer customer) {
System.out.print("In Template"+customer.getName());
String SQL="insert into customer (Name, Address,city, phone, mobile) values (?, ?, ?, ?, ?)";
jdbcTemplateobj.update(SQL, new Object[]{customer.getName(), customer.getAddress(),customer.getCity(), customer.getPhone(), customer.getMobile()});
return;
}
@Override
public Customer getCustomer(double Id) {
String SQL="select * from customer where Id=?";
Customer customer=jdbcTemplateobj.queryForObject(SQL, new Object[]{Id},new CustomerMapper());
return customer;
}
public List<Customer> getCustomers() {
String SQL="select * from customer";
List <Customer> customers=jdbcTemplateobj.query(SQL, new CustomerMapper());
return customers;
}
@Override
public void deleteCustomer(double Id) {
String SQL="delete from customer where Id=?";
jdbcTemplateobj.update(SQL, Id);
return;
}
@Override
public void updateCustomer(double Id, String Name, String Address, String city, String phone, String mobile) {
String SQL = "update customer set String = ?, String = ?, String = ?, String = ?, mobile = ? where Id = ?";
jdbcTemplateobj.update(SQL, Name, Address, city, phone, mobile, Id);
return;
}
}
错误:
Feb 05, 2014 12:49:55 AM org.apache.catalina.core.ApplicationContext log
INFO: Destroying Spring FrameworkServlet 'dispatcher'
Feb 05, 2014 12:49:55 AM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
Feb 05, 2014 12:50:36 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Feb 05, 2014 12:50:36 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Feb 05, 2014 12:50:37 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Feb 05, 2014 12:50:55 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/WhizzyBilling] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.witlab.customer.CustomerJDBCTemplate.createCustomer(CustomerJDBCTemplate.java:37)
at com.witlab.services.CustomerServiceImpl.createCustomer(CustomerServiceImpl.java:26)
at com.witlab.controller.AddCustomerController.insertCustomer(AddCustomerController.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
答案 0 :(得分:3)
你正在定义你的CustomerJDBCTemplate
bean
<bean id="CustomerDAO" class="com.witlab.customer.CustomerJDBCTemplate" />
在这种情况下,没有理由调用setDataSource
方法。因此,jdbcTemplateObj
字段将保留null
。
有几种方法可以解决这个问题。
选项1:从@Autowired
字段中删除dataSource
注释,并将<property>
元素添加到<bean>
定义中,如此
<bean id="CustomerDAO" class="com.witlab.customer.CustomerJDBCTemplate" >
<property name="dataSource" ref="dataSource" />
</bean>
Spring会调用你的setDataSource(..)
方法,传入你定义的dataSource
bean。这将初始化jdbcTemplateobj
字段。
选项2:添加setDataSource()
方法
@PostConstruct
方法
@PostConstruct
public void init() {
this.jdbcTemplateobj = new JdbcTemplate(dataSource);
}
当Spring完成初始化bean并注入任何字段时,它将调用此方法,初始化jdbcTemplateobj
字段。
选项3:从字段中删除@Autowired
注释,并将其添加到setDataSource(..)
方法。
答案 1 :(得分:0)
变量可以是null
但不应该是的任何地方,请在使用之前进行检查。这意味着,尤其是在使用点运算符访问方法或成员变量之前检查它。您已针对变量customer
检查此内容,但未针对model
或customerservice
检查此内容。