gpt4 book ai didi

java - Vaadin EntityManager NullReferenceException

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:44:39 25 4
gpt4 key购买 nike

我试图让程序在 vaadin 和 spring-boot 中运行,但无法让 EntityManager 运行。获取 NullReferenceException

对于初学者来说,这是我第一次尝试使用 Spring-boot 和 Vaadin 制作一些东西。我正在尝试使 EntityManager 正常工作,但出现 NullReferenceException。似乎我的 EntityManager 抛出了异常,但我找不到任何有关如何正确使用它的示例。我考虑过使用 EntityManagerFactory,但同样,任何地方都没有示例。findAll() 方法有效,所以我不确定该怎么做。

我有一个 Repository_Room 接口(interface)

public interface Repository_Room
{
void save(Room room);

void delete(Room room);

void update(Room room);

List<Room> findAll();

List<Course> getCourses(Room room);
}

在 Repository_Room_Impl 类中实现

@Transactional
@Repository
public class Repository_Room_Impl implements Repository_Room{

@PersistenceContext
private EntityManager em;

public Repository_Room_Impl()
{

}

@Override
public List<Room> findAll() {
return em.createQuery("select n from " + Room.class.getSimpleName() + " n").getResultList();
}

@Override
public void save(Room room) {
em.persist(room);
}

public void delete(Room room) {
em.remove(em.find(Room.class, room.getId()));
}

public void update(Room room) {
em.merge(em);
}

public List<Course> getCourses(Room room)
{
List<Course> courses = em.createQuery("select n from " + Course.class.getSimpleName() + " n WHERE n.Room = :roomID", Course.class)
.setParameter("roomID", room.getId())
.getResultList();
return courses;
}
}

尝试在我的表单中使用存储库来添加新元素

@SpringComponent
@UIScope
public class Form_Room extends VerticalLayout {

private Room room;

private Reloader reloader;

private TextField val_id;
private TextField val_name;
private TextField val_level;

private Binder<Room> binder;

@Autowired
private Repository_Room repository;

public Form_Room()
{
binder = new Binder<>(Room.class);

val_id = new TextField("ID:");
val_name = new TextField("Name:");
val_level = new TextField(("Level:"));

binder.forField(val_id)
.withConverter(
new StringToIntegerConverter("Must enter a number"))
.bind(Room::getId, Room::setId);

binder.forField(val_name).bind(Room::getName, Room::setName);

binder.forField(val_level)
.withConverter(
new StringToIntegerConverter("Must enter a number"))
.bind(Room::getLevel, Room::setLevel);

add(val_id);
add(val_name);
add(val_level);
}

public Room getRoom() {
return room;
}

public void setRoom(Room room) {
if (room == null) {
this.room = null;
}
else {
this.room = room;
binder.readBean(this.room);
}
}

public Reloader getReloader() {
return reloader;
}

public void setReloader(Reloader reloader) {
this.reloader = reloader;
}

public void updateRoom()
{
try
{
if(room == null)
{
repository.save(this.room);
System.out.println("Created");
}
else
{
binder.writeBean(this.room);
repository.update(this.room);
System.out.println("Updated");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

这个表单是在我的 View 中创建的

@Route(value = "rooms")
public class View_CRUD_Room extends VerticalLayout implements Reloader{

@Autowired
private Repository_Room repository;

@Autowired
private Form_Room form;

private Grid<Room> grid;
private Button button_view;
private Button button_remove;
private Button button_execute;
private HorizontalLayout buttonLayout;

private Room selected;

@PostConstruct
private void init(){
form = new Form_Room();
grid = new Grid<>();
button_view = new Button();
button_remove = new Button();
buttonLayout = new HorizontalLayout();
selected = null;

button_execute = new Button();
button_execute.setText("Végrehajt");

button_view.setText("Részletek");
button_remove.setText("Töröl");
buttonLayout.add(button_view);
buttonLayout.add(button_remove);

form.setVisible(true);

button_execute.addClickListener(event -> {
form.updateRoom();
});

button_view.addClickListener(event -> {
// Display details
});

button_remove.addClickListener(event -> {
// Display form with confirmation message and YesNo buttons
});

List<Room> list = repository.findAll();
grid.setItems(repository.findAll());
grid.addColumn(Room::getId).setHeader("Id");
grid.addColumn(Room::getName).setHeader("Name");
grid.addColumn(Room::getLevel).setHeader("Terem");
grid.asSingleSelect().addValueChangeListener(selectionEvent -> {
if (selectionEvent.getValue() != null) {
selected = selectionEvent.getValue();
form.setRoom(selected);
} else {
selected = null;
form.setRoom(selected);
}
});
add(new Text("Termek"));
add(grid);
add(buttonLayout);
add(form);
add(button_execute);
}

@Override
public void processRefresh() {
grid.setItems(repository.findAll());
}
}

stackTrace 打印以下内容:

java.lang.NullPointerException
at com.example.beadando.repository.Impl.Repository_Room_Impl.save(Repository_Room_Impl.java:26)
at com.example.beadando.component.Form_Room.updateRoom(Form_Room.java:93)
at com.example.beadando.view.View_CRUD_Room.lambda$init$9b1b5227$1(View_CRUD_Room.java:55)
at com.vaadin.flow.component.ComponentEventBus.fireEventForListener(ComponentEventBus.java:205)
at com.vaadin.flow.component.ComponentEventBus.handleDomEvent(ComponentEventBus.java:373)
at com.vaadin.flow.component.ComponentEventBus.lambda$addDomTrigger$dd1b7957$1(ComponentEventBus.java:264)
at com.vaadin.flow.internal.nodefeature.ElementListenerMap.lambda$fireEvent$2(ElementListenerMap.java:420)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at com.vaadin.flow.internal.nodefeature.ElementListenerMap.fireEvent(ElementListenerMap.java:420)
at com.vaadin.flow.server.communication.rpc.EventRpcHandler.handleNode(EventRpcHandler.java:59)
at com.vaadin.flow.server.communication.rpc.AbstractRpcInvocationHandler.handle(AbstractRpcInvocationHandler.java:64)
at com.vaadin.flow.server.communication.ServerRpcHandler.handleInvocationData(ServerRpcHandler.java:387)
at com.vaadin.flow.server.communication.ServerRpcHandler.lambda$handleInvocations$1(ServerRpcHandler.java:368)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at com.vaadin.flow.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:368)
at com.vaadin.flow.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:310)
at com.vaadin.flow.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:89)
at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1507)
at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:242)
at com.vaadin.flow.spring.SpringServlet.service(SpringServlet.java:81)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:712)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:459)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:352)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312)
at org.springframework.web.servlet.mvc.ServletForwardingController.handleRequestInternal(ServletForwardingController.java:141)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:177)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:52)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:836)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1747)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

我修改了 setRoom() 方法以尝试添加新元素而无需处理我的 TextFields。尝试调用我的 updateRoom() 方法时在表单中获取 NullReferenceException。请指出我正确的方向..谢谢!

最佳答案

抱歉之前看的有点太快了

但这是你的问题,你正在自己创建 Autowiring 类,而不是让 Spring 为你创建它们。

@Autowired
private Repository_Room repository;

public Form_Room() {
repository = new Repository_Room_Impl(); //remove this line

//rest of the code
}

然后在您的 View 中再次执行此操作:

@Route(value = "rooms")
public class View_CRUD_Room extends VerticalLayout implements Reloader {

@PostConstruct
private void init(){
form = new Form_Room(); //remove this line

//rest of the code
}

你看到 Autowiring 是一种叫做依赖注入(inject)的东西。这是一个奇特的词,表示“框架”(在本例中为 spring)将为您完成所有类的实例化。

Spring会为你做"new"的。并在运行时注入(inject)一个类供您使用,因此您不必这样做。 Spring 还会在它执行“new RepositoryImpl()”之前检查该类具有哪些依赖项并查看它是否需要 EntityManager,然后自动为您实例化一个并将其放入 RepositoryImpl。

这就是使用 Autowire 的全部意义所在,您不必进行新的操作。你只需要相信当你使用它时那里会有类。

然后是关于您的代码的其他一些提示。

在 Java 中,我们使用驼峰式大小写,所以“Repository_Room”应该是“RepositoryRoom”,“Repository_Room_Impl”应该是“RepositoryRoomImpl”

关于java - Vaadin EntityManager NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56499564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com