2014-07-22 45 views
0

時出現NullPointerException我收到此錯誤,當我嘗試手動插入到MySql表與休眠。 我想插入一些默認數據到我的應用程序,但手動插入時,我得到空指針異常。我是新的Java編程,所以無法理解如何解決這個錯誤。如果任何人都可以幫我這個會我真的很感激..SpringMvc + Hibernate:在手動插入數據到表

USER_ROLE類:

package com.sanjay31321.sys.model; 


import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity @Table(name="user_role") 
public class User_Role { 

    @Id @Column @GeneratedValue(strategy=GenerationType.AUTO) 
    private int id; 

    @Column(name="role_name") 
    private String name; 

    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 

} 

InsertUserRole

import org.springframework.beans.factory.annotation.Autowired; 

import com.sanjay31321.sys.model.User_Role; 
import com.sanjay31321.sys.service.UserRoleService; 

public class InsertUserRole { 
    private static final Logger logger = LoggerFactory.getLogger(InsertUserRole.class); 

    @Autowired 
    private UserRoleService userRoleService; 

    public void insert() { 
     User_Role role = new User_Role(); 

     role.setId(1); 
     role.setName("ROLE_ADMIN"); 
     logger.info("id : "+ role.getId() + " | Role : " + role.getName()); 
     userRoleService.addUserRole(role); 
    } 
} 

錯誤:

 
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException 
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965) 
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641) 
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
root cause

java.lang.NullPointerException com.sanjay31321.sys.preset.data.InsertUserRole.insert(InsertUserRole.java:22) com.sanjay31321.sys.preset.DefaultDataInstall.install(DefaultDataInstall.java:23) com.sanjay31321.sys.controller.DefaultDataController.postinstall(DefaultDataController.java:31) 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:601) org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855) javax.servlet.http.HttpServlet.service(HttpServlet.java:641) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

DefaultDataController class

package com.sanjay31321.sys.controller; 

import java.util.Locale; 

import javax.validation.Valid; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.stereotype.Controller; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import com.sanjay31321.sys.preset.DefaultDataInstall; 
import com.sanjay31321.sys.preset.model.Install; 

@Controller 
public class DefaultDataController { 
    private static final Logger logger = LoggerFactory.getLogger(DefaultDataController.class); 

    @RequestMapping(value = "/install", method = RequestMethod.GET) 
    public String getinstall(Locale locale, Install install) { 
     logger.info("Welcome to Install Default Settings page ! GET METHOD : The client locale is {}.", locale); 
     return "install"; 
    } 

    @RequestMapping(value="/install", method=RequestMethod.POST) 
    public String postinstall(@Valid Install install, BindingResult result, Locale locale){ 
     logger.info("Welcome to Install Default Settings page ! POST METHOD : The client locale is {}.", locale); 
     DefaultDataInstall settings = new DefaultDataInstall(); 
     settings.install(); 
     return "install"; 
    } 
} 

DefaultDataInstall類

package com.sanjay31321.sys.preset; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.sanjay31321.sys.preset.data.InsertCourse; import com.sanjay31321.sys.preset.data.InsertFeedback; import com.sanjay31321.sys.preset.data.InsertQuestion; import com.sanjay31321.sys.preset.data.InsertQuestionSet; import com.sanjay31321.sys.preset.data.InsertStudent; import com.sanjay31321.sys.preset.data.InsertSubject; import com.sanjay31321.sys.preset.data.InsertTeacher; import com.sanjay31321.sys.preset.data.InsertUser; import com.sanjay31321.sys.preset.data.InsertUserRole; public class DefaultDataInstall { private static final Logger logger = LoggerFactory.getLogger(DefaultDataInstall.class); public void install() { InsertUserRole role = new InsertUserRole(); role.insert(); logger.info("User Role data is installed"); } }

我重視你問的類。

+0

我們展示DefaultDataController.java的代碼。它如何得到它的InsertUserRole的實例? –

回答

0

我在github上看到了你的代碼。

它顯示錯誤,因爲userRoleServiceInsertUserRole爲空。所以你不能調用任何方法。

爲什麼?

您在DefaultDataInstall中使用new關鍵字創建InsertUserRole對象。 @Autowired在你這樣做的時候不起作用,它只在DefaultDataInstall也在春天實例化時才起作用。只有在創建對象時,Spring才能注入對象。

如何解決它。

實例化preset包內的所有對象,即將它們標記爲@Component

其他方法是,您將ApplicationContext注入控制器並將其傳遞給您正在創建的對象並從AppContext獲取服務。

@Controller 
public class DefaultDataController { 
    private static final Logger logger = LoggerFactory.getLogger(DefaultDataController.class); 

    @Autowired private ApplicationContext applicationContext; 

    @RequestMapping(value="/install", method=RequestMethod.POST) 
    public String postinstall(@Valid Install install, BindingResult result, Locale locale){ 
     logger.info("Welcome to Install Default Settings page ! POST METHOD : The client locale is {}.", locale);  
     DefaultDataInstall settings = new DefaultDataInstall(applicationContext);//create a constructor which acceps ApplicationContext object 
     settings.install(); 
     return "install"; 
    } 

    //other code 
} 

傳遞ApplicationContext對象要使用的Spring bean的所有訂單的對象。

public class DefaultDataInstall { 

    private static final Logger logger = LoggerFactory.getLogger(DefaultDataInstall.class); 
    private ApplicationContext applicationContext; 

    public DefaultDataInstall(ApplicationContext applicationContext){ 
     this.applicationContext = applicationContext; 
    } 

    public void install() { 

     InsertUserRole role = new InsertUserRole(); 
     role.setUserRoleService(applicationContext.getBean("userRoleServiceImpl")); 
     role.insert(); 
     logger.info("User Role data is installed"); 

     //other stuff 
    } 
} 

現在你可以使用通過服務來調用它們的方法

public class InsertUserRole { 
    private static final Logger logger = LoggerFactory.getLogger(InsertUserRole.class); 

    private UserRoleService userRoleService; 

    public void setUserRoleService(UserRoleService){ 
     this.userRoleService = userRoleService; 
    } 

    //other code 
} 
+0

非常感謝您的幫助。我會嘗試這個解決方案。 –

+0

謝謝阿米特。我試過這個解決方案,但無法正確理解,所以我去了@component。現在工作正常。我想感謝你指點我正確的方向。 –

+0

如果解決了問題,請接受答案。 – bitkot