【问题标题】:HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerExceptionHTTP 状态 500 - 请求处理失败;嵌套异常是 java.lang.NullPointerException
【发布时间】:2014-02-04 19:41:38
【问题描述】:

我在尝试启动我的应用程序时遇到了困难,我找了几天我的错误,但我被困在代码中的某个地方并寻求您的帮助...谢谢

添加客户控制器:

 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";
    }



}

调度程序-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);

}

客户 JDBC 模板:

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)

【问题讨论】:

  • 请添加此行。 com.witlab.customer.CustomerJDBCTemplate.createCustomer(CustomerJDBCTemplate.java:37)
  • 你还没有发布任何有用的代码。您的例外中最相关的行是:java.lang.NullPointerException at com.witlab.customer.CustomerJDBCTemplate.createCustomer(CustomerJDBCTemplate.java:37) 所以请尝试发布。
  • 已发布 CustomerJDBCTemplate 类
  • 也发布CustomerServiceImpl 类。

标签: spring jsp


【解决方案1】:

你正在像这样定义你的CustomerJDBCTemplate bean

<bean id="CustomerDAO" class="com.witlab.customer.CustomerJDBCTemplate" />  

在这种情况下,没有理由调用setDataSource 方法。因此,jdbcTemplateObj 字段将保持为 null

有几种方法可以解决此问题。

选项 1:从 dataSource 字段中删除 @Autowired 注释,并像这样将 &lt;property&gt; 元素添加到 &lt;bean&gt; 定义中

<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(..) 方法中。

【讨论】:

  • 我正在从表单中获取数据,但我无法插入数据库并在 jdbcTemplateobj.update(SQL, new Object[]{customer.getName(), customer.getAddress( ),customer.getCity(), customer.getPhone(), customer.getMobile()});
  • @SridhatTurupati 与您的问题相关或无关的错误?你的评论是关于什么的?你做出改变了吗?
  • 我没有做任何更改,错误与问题有关,在调用 createCustomer 方法时和 jdbcTemplateobj.update 行引发了异常
  • @SridhatTurupati A NullPointerException 因为jdbcTemplateobnull。阅读我的回答,它提供了解决方案。
  • @SridhatTurupati 考虑支持有用的答案并接受您认为最能回答问题的答案。
【解决方案2】:

变量可以是null 但不应该是的任何地方,在使用它之前检查它。这意味着,特别是,在使用点运算符访问方法或成员变量之前检查它。您已经在检查变量 customer 但不是检查 modelcustomerservice

【讨论】:

  • 异常在customerService.createCustomer()内部抛出。
  • 同样的原则也适用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-06
  • 2013-11-15
相关资源
最近更新 更多