当我试图用Hibernate手动插入MySql表时,我会收到这个错误。我试图插入一些默认数据到我的应用程序,但当手动插入时,我得到空指针异常。我是java编程的新手,所以不能理解如何修复这个错误。如果有人能帮我做这件事,我真的很感激…
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;
}
}
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 causejava.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(本机方法) 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) 服务(httpservlet.java:641) org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:829) 服务(httpservlet.java:722)
DefaultDataController类
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类
我已经附上了你要求的课程。
我在Github上看到你的代码了。
显示该错误是因为InsertUserRole
中的UserRoleService
为空。因此不能对其调用任何方法。
为什么?
您可以使用DefaultDataInstall
中的New
关键字创建InsertUserRole
对象。@autowired
只有在defaultdatainstall
也被Spring实例化时才起作用。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
}
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
}
本文向大家介绍SQL Server 向临时表插入数据示例,包括了SQL Server 向临时表插入数据示例的使用技巧和注意事项,需要的朋友参考一下 这样就可以成功。 会提示error "There is already an object named '#DirtyOldWIPBOM' in the database" 为什么? [笔记]:千万不要用游标,用临时表和表变量代替游标会极大的提高性能,
在创建了临时表并声明了数据类型之后; 然后如何插入数据库中物理表中已保存的相关数据?
我有一个MySQL数据表,详细信息如下: 它正在工作,只是我正在尝试在插入数据后从数据库中获取id号。我该怎么做?
问题内容: 我一直在看这段代码已有一段时间了,但我看不出问题出在哪里。我已经阅读了整个StackOverflow,但仍然看不到我的错误在哪里。 错误在第13行中,即thats 。我尝试通过http://www.w3schools.com/php/php_mysql_insert.asp帮助自己,但对我没有太大帮助。 问题答案: 警告: 切勿将 w3schools 用于学习目的。他们的教程中有很多错
这是我的代码: 有人能知道这段代码中的错误是什么,请修复它。