سؤال

I'm trying to figure out how to serialize a list using AutoBean in GWT, but I keep getting a Null Pointer Exception.

Here's what I have:

    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);

Can anyone help point me in the right direction? Thank you very much.

Here's the supporting classes and interfaces:

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;
    }
    };

Here's the 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)
هل كانت مفيدة؟

المحلول

This is unfortunately a known issue: https://github.com/gwtproject/gwt/issues/6903

The problem is that the items of the list are not wrapped into AutoBeans so AutoBeanUtils#getAutoBean in AutoBeanCodexImpl.ObjectCoder#encode returns null, hence the NPE in AutoBeanCodexImpl#doEncode.

The workaround involves replacing the list items with AutoBeans that wrap the actual value.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top