GWT RequestFactory:在请求中检索相关/嵌套实体时发生NullPointerException

时间:2013-06-14 04:51:22

标签: gwt nullpointerexception requestfactory

我在检索Value Proxy时遇到空指针异常。代码如下。

@PersistenceCapable
public class Student {

    //**********Compulsory
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    Long id;

    Integer version;

    String name;
    List<String> hobbies;
    Address address;

    public Student(){
        version = 1;
    }

    public Long getId(){
        return id;
    }

    public Integer getVersion(){
        return version;
    }

    public static Student findStudent(Long id){
        return new Student();
    }

    public static Student findStudentAddress(){
        Student s = new Student();
        s.setAddress(new Address("city", "state"));
        return s;
    }
}


@PersistenceCapable
public class Address implements Serializable{
    @Persistent(defaultFetchGroup = "true")
    String city,state;

    public Address(){}

    public Address(String city,String state){
        this.city = city;
        this.state = state;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}

@ProxyFor(Student.class)
@ExtraTypes({AddressProxy.class})
public interface StudentProxy extends EntityProxy {
    //****Compulsory
    public Long getId();
    public Integer getVersion();
    //**************

    //Custom here onwards
    public String getName();

    public void setName(String name);

    public List<String> getHobbies();

    public void setHobbies(List<String> hobbies);

    public AddressProxy getAddress();

    public void setAddress(AddressProxy address);

    public void setId(Long id);

    public void setVersion(Integer version);
}

@Service(Student.class)
public interface StudentRequest extends RequestContext {
    //********Invoked without custom object - statically**** usually used to pull data from server
    Request<StudentProxy> findStudent(Long id);
    Request<StudentProxy> findStudentAddress();
}

@ProxyFor(Address.class)
public interface AddressProxy extends ValueProxy {
    public String getCity();
    public void setCity(String city);
    public String getState();
    public void setState(String state);
}

public class Home implements EntryPoint {
    MyRequestFactory rf;
    StudentRequest sr;
    @Override
    public void onModuleLoad() {

        GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
            public void onUncaughtException(Throwable e) {
                e.printStackTrace();                
            }
        });

        rf = GWT.create(MyRequestFactory.class);
        rf.initialize(new SimpleEventBus());

        sr = rf.studentRequest();
        sr.findStudentAddress().fire(new Receiver<StudentProxy>() {
            public void onSuccess(StudentProxy response) {
                System.out.println(response.getAddress().getCity());
            }
        });
    }
}

com.google.web.bindery.event.shared.UmbrellaException: Exception caught: null
    at com.google.web.bindery.requestfactory.shared.impl.AbstractRequestContext$StandardPayloadDialect.processPayload(AbstractRequestContext.java:414)
    at com.google.web.bindery.requestfactory.shared.impl.AbstractRequestContext$5.onTransportSuccess(AbstractRequestContext.java:1151)
    at com.google.web.bindery.requestfactory.gwt.client.DefaultRequestTransport$1.onResponseReceived(DefaultRequestTransport.java:136)
    at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:258)
    at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:412)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:242)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
    at com.gwtrf.client.Home$2.onSuccess(Home.java:28)
    at com.gwtrf.client.Home$2.onSuccess(Home.java:1)
    at com.google.web.bindery.requestfactory.shared.impl.AbstractRequest.onSuccess(AbstractRequest.java:129)
    at com.google.web.bindery.requestfactory.shared.impl.AbstractRequestContext$StandardPayloadDialect.processPayload(AbstractRequestContext.java:381)
    ... 30 more

请帮助我检索客户端上已在服务器端初始化的值代理。

1 个答案:

答案 0 :(得分:0)

您必须使用方法with()

指定要检索的relationships

在您的情况下,如果您添加以下代码,您的代码应该有效:

sr.findStudentAddress().with("address").fire(...);