如何在JavascriptObject中为double类型字段设置默认值?

时间:2014-03-02 11:40:16

标签: gwt

我有一个像这样的JavaScriptObject:

@SingleJsoImpl(Test1Impl.class)
public interface Test1 
{
     double getValue();

     void setValue(String Value);
}

public class Test1Impl extends JavaScriptObject implements Test1
{
    protected Test1Impl()
    {
    }

         @Override
     public final native double getValue()/*-{
      return this.Value||(this.Value=0);
     }-*/;


     @Override
     public final native void setValue(String Value)/*-{
     this.Value = Value;
    }-*/;
}

我想用这个:

return this.Value||(this.Value=0);

设置默认值,但是当我测试它时

public class GWT_Test implements EntryPoint{

    public static native void log(Object message)/*-{
           console.log(message);
    }-*/;


    @Override
    public void onModuleLoad()
    {
    Test1 t1=(Test1) Test1Impl.createObject();

    log(t1.getValue());
    }
}

我得到了这些错误:

com.google.gwt.core.client.JavaScriptException: (null)             @com.gwt.test.client.GWT_Test::log(Ljava/lang/Object;)([Java object: java.lang.Double@239824989]): null
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:249)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:576)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeVoid(ModuleSpace.java:304)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid(JavaScriptHost.java:107)
at com.dsc.gwt.test.client.GWT_Test.log(GWT_Test.java)
at com.dsc.gwt.test.client.GWT_Test.onModuleLoad(GWT_Test.java:51)
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:606)
at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:411)
at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200)
at     com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:526)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Thread.java:744)

那么这里有什么问题?或

 How to set default value for double type field?

更新

@Override
public void onModuleLoad()
{
    Test1 t1=(Test1) Test1Impl.createObject();

    printDouble(t1.getValue());
}

private void printDouble(Double num){
    log(num.toString());
}

和这个

    @Override
public void onModuleLoad()
{
    Test1 t1=(Test1) Test1Impl.createObject();

    log(Double.toString(t1.getValue()));
}

按照我的预期工作,任何人都可以解释第一行错误消息吗?

com.google.gwt.core.client.JavaScriptException: (null)@com.gwt.test.client.GWT_Test::log(Ljava/lang/Object;)([Java object: java.lang.Double@239824989]): null

1 个答案:

答案 0 :(得分:0)

您看到的错误是JS等同于NullPointerException

GWT在iframe中运行,取决于浏览器iframe并不总是有console个对象。使用$wnd.console引用顶部窗口的控制台,您的窗口小部件等等。住。