HTTP状态500 - 嵌套异常是java.lang.NullPointerException

时间:2014-06-28 06:11:30

标签: hibernate spring-mvc

我是春天和冬眠的新手。我得到了这个例外

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:659)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


root cause 

java.lang.NullPointerException
    com.mayur.dao.ProductDAOImpl.updateProduct(ProductDAOImpl.java:36)
    com.mayur.service.ProductServiceImpl.updateProduct(ProductServiceImpl.java:34)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:108)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    com.sun.proxy.$Proxy19.updateProduct(Unknown Source)
    com.mayur.Controller.ProductController.editProduct(ProductController.java:75)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

以下是其他文件

Product.java

@Entity
@Table(name="PRODUCTS")
public class Product {
@Id
@Column(name="ID")
@GeneratedValue
private Integer id;

@Column(name="PRODUCT_NAME")
private String productName;

@Column(name="PRICE")
private int price;

@Column(name="QUANTITY")
private int quantity;

public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
public String getProductName() {
    return productName;
}
public void setProductName(String productName) {
    this.productName = productName;
}
public int getPrice() {
    return price;
}
public void setPrice(int price) {
    this.price = price;
}
public int getQuantity() {
    return quantity;
}
public void setQuantity(int quantity) {
    this.quantity = quantity;
}



}

** 的 ProductDAOImpl.java **

@Repository
public class ProductDAOImpl implements ProductDAO {

@Autowired
private SessionFactory sessionFactory;


public void addProduct(Product product) {
    sessionFactory.getCurrentSession().save(product);
}

public void removeProduct(int id) {
    Product product = getProductById(id);
    if(null != product) {
        sessionFactory.getCurrentSession().delete(product);
    }
  }

  @SuppressWarnings("unchecked")
 public List<Product> listProducts() {
    return sessionFactory.getCurrentSession().createQuery("from Product").list();
  }

  public void updateProduct(Product product) {
    Product productToUpdate = getProductById(product.getId());
    productToUpdate.setProductName(product.getProductName());
    productToUpdate.setPrice(product.getPrice());
    productToUpdate.setQuantity(product.getQuantity());
    sessionFactory.getCurrentSession().update(productToUpdate);
   }

    public Product getProductById(int id) {
    Product product =(Product)sessionFactory.getCurrentSession().get(Product.class,id);
    return product;


   }
 } 

ProductServiceImpl.java

@Service
public class ProductServiceImpl implements ProductService {

@Autowired
private ProductDAO productDAO;

@Transactional
public void addProduct(Product product) {
    productDAO.addProduct(product);
}

@Transactional
public void removeProduct(int id) {
    productDAO.removeProduct(id);
}

@Transactional
public List<Product> listProducts() {
    return productDAO.listProducts();
}

@Transactional
public void updateProduct(Product product) {
    productDAO.updateProduct(product);
}

@Transactional
public Product getProductById(int id) {
    return productDAO.getProductById(id);
}
}

ProductController.java

@Controller
@SessionAttributes

  public class ProductController {

@Autowired
 private ProductService productService;

@RequestMapping("addproduct.html")
 public String viewaddProduct(Map model) {
    Product product = new Product();    
    model.put("product", product);
    return "addproduct";
}

@RequestMapping(value ="addproduct.html", method = RequestMethod.POST)
public String addProduct(@ModelAttribute("product") Product product, 
        BindingResult bindingresult, Map model) {

        if (bindingresult.hasErrors()) {
        return "addproduct";

    }
        productService.addProduct(product); 
        model.put("productList", productService.listProducts());

        return "listproduct";
  }
@RequestMapping("/delete/{id}")
 public String removeProduct(@PathVariable("id") Integer id, 
        @ModelAttribute("product") Product product, Map model)
 {
        productService.removeProduct(id);
        model.put("productList", productService.listProducts());

    return "redirect:/listproduct.html";
 }

@RequestMapping("listproduct.html")
 public String listProducts(Map model) {
    model.put("productList", productService.listProducts());
    return "listproduct";
}

@RequestMapping(value="/edit/{id}", method=RequestMethod.GET)
 public String vieweditProduct(@PathVariable("id")Integer id,
                 Map model)
 {  
    model.put("product", productService.getProductById(id));

    return "editproduct";
 }

@RequestMapping(value="/edit/{id}", method=RequestMethod.POST)
public String editProduct(@PathVariable("id")Integer id,
        @ModelAttribute("product") Product product, Map model){

    productService.updateProduct(product);
    model.put("productList", productService.listProducts());

    return "redirect:/listproduct.html";
}
  }

弹簧servlet.xml中

    <?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<mvc:annotation-driven />

<context:component-scan  base-package="com.mayur.Controller,
       com.mayur.dao,com.mayur.service" />

<bean id="tilesViewResolver"
     class="org.springframework.web.servlet.view.tiles2.TilesViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>

<bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles.xml</value>
        </list>
    </property>
</bean>

<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages" />
</bean>

<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
        p:password="${jdbc.password}" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>resources/hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>    

    <tx:annotation-driven />

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>    
</beans>

它是一个基本的crud应用程序。添加和删​​除作品。当我提交编辑时,我得到这个例外。 请帮帮我

错误指向ProductDAOImpl.java中的这一行 - &gt;

产品productToUpdate = getProductById(product.getId());      并在ProductServiceImpl.java中 - &gt;

productDAO.updateProduct(产品);

请某人帮帮我

ANSWER 通过添加此行product.setId(id);它现在正在工作。

0 个答案:

没有答案