7

我试图弄清楚如何在 GWT 中使用 AutoBean 序列化列表,但我不断收到空指针异常。

这是我所拥有的:

    GuideCreatorFactory beanFactory = AutoBeanFactorySource.create(GuideCreatorFactory.class);

    List<Guide> guides = new LinkedList<Guide>();
    Guide guide = new Guide();
    guide.setText("this is the text");
    guide.setTitle("this is the title");
    guides.add(guide);

    GuideCreatorList<Guide> impl = new GuideCreatorListImpl();
    impl.setGuides(guides);

    System.out.println("Serializing the given parameter to JSON");

    // Fails on the below lines w/ NPE
    AutoBean<GuideCreatorList> bean = beanFactory.create(GuideCreatorList.class, impl);
    String json = AutoBeanCodex.encode(bean).getPayload();
    System.out.println("guides as json: " + json);

谁能帮我指出正确的方向?非常感谢你。

这是支持的类和接口:

public interface GuideCreatorFactory extends AutoBeanFactory {
  AutoBean<GuideCreator> createGuide();

  AutoBean<GuideCreatorList> createGuideList();
}


public interface GuideCreator {

  public String getText();

  public void setText(String text);

  public String getTitle();

  public void setTitle(String title);

}


public interface GuideCreatorList<T extends GuideCreator> {
  public List<T> getGuides();

  public void setGuides(List<T> guides);
}



class GuideCreatorListImpl implements GuideCreatorList<Guide> {
    private List<Guide> guides;

    public GuideCreatorListImpl() {

    }

    @Override
    public List<Guide> getGuides() {
      return guides;
    }

    @Override
    public void setGuides(List<Guide> guides) {
      this.guides = guides;
    }
    };

这是NPE:

java.lang.NullPointerException
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl.doEncode(AutoBeanCodexImpl.java:558)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$ObjectCoder.encode(AutoBeanCodexImpl.java:321)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$CollectionCoder.encode(AutoBeanCodexImpl.java:163)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$PropertyGetter.encodeProperty(AutoBeanCodexImpl.java:413)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$PropertyGetter.visitReferenceProperty(AutoBeanCodexImpl.java:389)
    at com.google.web.bindery.autobean.shared.AutoBeanVisitor.visitCollectionProperty(AutoBeanVisitor.java:229)
    at com.google.web.bindery.autobean.vm.impl.ProxyAutoBean.traverseProperties(ProxyAutoBean.java:300)
    at com.google.web.bindery.autobean.shared.impl.AbstractAutoBean.traverse(AbstractAutoBean.java:166)
    at com.google.web.bindery.autobean.shared.impl.AbstractAutoBean.accept(AbstractAutoBean.java:101)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl.doEncode(AutoBeanCodexImpl.java:558)
    at com.google.web.bindery.autobean.shared.AutoBeanCodex.encode(AutoBeanCodex.java:83)
    at com.districthp.core.ui.client.review.JsonSerializationText.testMyObject(JsonSerializationText.java:82)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
4

1 回答 1

11

不幸的是,这是一个已知问题:https ://github.com/gwtproject/gwt/issues/6903

问题是列表中的项目没有包装到AutoBeans中,因此AutoBeanUtils#getAutoBeanAutoBeanCodexImpl.ObjectCoder#encode返回null中,因此 NPE 在中AutoBeanCodexImpl#doEncode

AutoBean解决方法涉及用包装实际值的 s替换列表项。

于 2012-07-17T23:29:17.767 回答