尝试将数据保存到Vaadin中的数据库 - 未知来源

时间:2014-10-16 13:16:28

标签: java spring hibernate vaadin vaadin7

我试图通过Vaadin视图向数据库发送数据。使用测试java类 - 一切都很顺利。数据落在数据库中,每个人都很高兴。

但是,当通过Vaadin视图发送数据时,我得到如下错误:

SEVERE: 
java.lang.NullPointerException
at com.example.datatest2.Datatest2UI$1.buttonClick(Datatest2UI.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:979)
at com.vaadin.ui.Button.fireClick(Button.java:393)
at com.vaadin.ui.Button$1.click(Button.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:168)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:287)
at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:180)
at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:93)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1402)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:305)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

Vaadin观看代码:

package com.example.datatest2;

import javax.inject.Inject;
// all the imports go here

@SuppressWarnings("serial")
@Theme("datatest2")
public class Datatest2UI extends UI {

@Inject
private PersonService ps;

@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = Datatest2UI.class)
public static class Servlet extends VaadinServlet {
}

@Override
protected void init(VaadinRequest request) {
    final VerticalLayout layout = new VerticalLayout();
    layout.setMargin(true);
    setContent(layout);

    final PropertysetItem item = new PropertysetItem();
      item.addItemProperty("name", new ObjectProperty<String>(""));
      item.addItemProperty("surname", new ObjectProperty<String>(""));

      FormLayout form = new FormLayout();
      TextField nameField = new TextField("Imie");
      form.addComponent(nameField);

      TextField subnameField = new TextField("Nazwisko");
      form.addComponent(subnameField);

      final FieldGroup personalData = new FieldGroup(item);
      personalData.setBuffered(true);
      personalData.bind(nameField, "name");
      personalData.bind(subnameField, "surname");

      // Accept button
        Button send = new Button("Akceptuj");
        send.addClickListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                try {

                     ps.addPerson(item.getItemProperty("name").getValue(), item.getItemProperty("surname").getValue());
                     personalData.commit();
                    } catch (CommitException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                    }
                    Notification.show("Yebau pies ten program :P");
            }
        });
        form.addComponent(send);
        layout.addComponent(form);
}

}

PersonService.java:

package com.example.datatest2.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.example.datatest2.dao.PersonDao;
import com.example.datatest2.entity.Person;

@Component
public class PersonService {

private PersonDao personDao;

public PersonDao getPersonDao() {
    return personDao;
}

@Autowired
public void setPersonDao(PersonDao personDao) {
    this.personDao = personDao;
}

public void addPerson(Object name, Object surname) {
    personDao.insert(name, surname);
}

public List<Person> getAll() {
    return personDao.selectAll();
}
}

PersonDao.java:

package com.example.datatest2.dao;

import java.util.List;
//and other imports
@Repository("personDao")
@Transactional(propagation = Propagation.REQUIRED)
public class PersonDao {

private static final String SELECT_QUERY = "select p from Person p";

@PersistenceContext
private EntityManager entityManager;

public EntityManager getEntityManager() {
    return entityManager;
}

public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}

public void insert(Object name, Object surname) {
      Person person = new Person();
      person.setName((String)name);
      person.setSurname((String)surname);
      entityManager.persist(person);
     }

@SuppressWarnings("unchecked")
public List<Person> selectAll() {
    return entityManager.createQuery(SELECT_QUERY).getResultList();
}
}

最后,Person.java

package com.example.datatest2.entity;

import javax.persistence.Column;
//and all other imports

@Entity
@Table(name = "PERSON")
public class Person {

@Id
@Column(name = "ID")
@GeneratedValue
private long id;
private String name;
private String surname;

//getters and setters

@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", surname=" + surname + "]";
}

}

我认为我在Vaadin UI视图中做了一些非常错误的事情,因为 - 正如我之前所写的那样 - 在使用将数据发送到数据库的java主类时一切正常。

0 个答案:

没有答案