cancel
Showing results for 
Search instead for 
Did you mean: 

jsonUtils.toObject() fails with NullPointer or EcmaError/TypeError

mathias_lin
Star Contributor
Star Contributor
I try to use jsonUtils.toObject with a json string or a json object as parameter in the javascript console in Alfresco Enterprise 4.1.5, and getting the following errors.

<blockquote>
<strong>Reference:</strong>
http://docs.alfresco.com/4.0/index.jsp?topic=%2Fcom.alfresco.enterprise.doc%2Freferences%2Fapi-ws-ob...


toObject(jsonString)
Takes a JSON string and converts it to a native JavaScript object.

toObject(jsonObject)
Takes a JSON object and converts it to a native JavaScript object.
</blockquote>

<strong>Test 1</strong>
                
var jsonString = '{"test": "test"}';
logger.log( jsonUtils.toObject(jsonString) );



500 Internal Error
An error inside the HTTP server which prevented it from fulfilling the request.
10080038 Wrapped Exception (with status template): 10080143 Failed to execute script 'Javascript Console Script': 10080142 TypeError: Cannot find default value for object. (fb91e06fb1303a88bc57085eaaf9692f.js#2)

Wrapped Exception (with status template): 10080063 Failed to execute script 'Javascript Console Script': 10080062 TypeError: Cannot find default value for object. (b7b11bfd779a718cabdce9ea9162012f.js#5)
org.springframework.extensions.webscripts.WebScriptException: 10080013 Wrapped Exception (with status template): 10080063 Failed to execute script 'Javascript Console Script': 10080062 TypeError: Cannot find default value for object. (b7b11bfd779a718cabdce9ea9162012f.js#5)
   at org.springframework.extensions.webscripts.AbstractWebScript.createStatusException(AbstractWebScript.java:1067)
   at de.fme.jsconsole.ExecuteWebscript.executeScriptContent(Unknown Source)
   at de.fme.jsconsole.ExecuteWebscript.access$100(Unknown Source)
   at de.fme.jsconsole.ExecuteWebscript$2.execute(Unknown Source)
   at de.fme.jsconsole.ExecuteWebscript$2.execute(Unknown Source)
   at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:401)
   at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:291)
   at de.fme.jsconsole.ExecuteWebscript.runWithTransactionIfNeeded(Unknown Source)
   at de.fme.jsconsole.ExecuteWebscript.access$000(Unknown Source)
   at de.fme.jsconsole.ExecuteWebscript$1.doWork(Unknown Source)
   at de.fme.jsconsole.ExecuteWebscript$1.doWork(Unknown Source)
   at org.alfresco.repo.security.authentication.AuthenticationUtil.runAs(AuthenticationUtil.java:529)
   at de.fme.jsconsole.ExecuteWebscript.runScriptWithTransactionAndAuthentication(Unknown Source)
   at de.fme.jsconsole.ExecuteWebscript.execute(Unknown Source)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:365)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:517)
   at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:333)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:377)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:209)
   at org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:132)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:61)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:465)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
   at java.lang.Thread.run(Thread.java:680)
Caused by: org.alfresco.scripts.ScriptException: 10080063 Failed to execute script 'Javascript Console Script': 10080062 TypeError: Cannot find default value for object. (b7b11bfd779a718cabdce9ea9162012f.js#5)
   at org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:202)
   at org.alfresco.repo.processor.ScriptServiceImpl.execute(ScriptServiceImpl.java:212)
   at org.alfresco.repo.processor.ScriptServiceImpl.executeScript(ScriptServiceImpl.java:174)
   at org.alfresco.repo.web.scripts.RepositoryScriptProcessor.executeScript(RepositoryScriptProcessor.java:102)
   … 36 more
Caused by: org.alfresco.error.AlfrescoRuntimeException: 10080062 TypeError: Cannot find default value for object. (b7b11bfd779a718cabdce9ea9162012f.js#5)
   at org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:516)
   at org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:198)
   … 39 more
Caused by: org.mozilla.javascript.EcmaError: TypeError: Cannot find default value for object. (b7b11bfd779a718cabdce9ea9162012f.js#5)
   at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3350)
   at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3340)
   at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3356)
   at org.mozilla.javascript.ScriptRuntime.typeError1(ScriptRuntime.java:3368)
   at org.mozilla.javascript.ScriptableObject.getDefaultValue(ScriptableObject.java:758)
   at org.mozilla.javascript.ScriptableObject.getDefaultValue(ScriptableObject.java:677)
   at org.mozilla.javascript.ScriptRuntime.toString(ScriptRuntime.java:707)
   at org.mozilla.javascript.NativeJavaObject.coerceTypeImpl(NativeJavaObject.java:633)
   at org.mozilla.javascript.Context.jsToJava(Context.java:1705)
   at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:209)
   at org.mozilla.javascript.optimizer.OptRuntime.call1(OptRuntime.java:66)
   at org.mozilla.javascript.gen.c19._c0(b7b11bfd779a718cabdce9ea9162012f.js:5)
   at org.mozilla.javascript.gen.c19.call(b7b11bfd779a718cabdce9ea9162012f.js)
   at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393)
   at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2834)
   at org.mozilla.javascript.gen.c19.call(b7b11bfd779a718cabdce9ea9162012f.js)
   at org.mozilla.javascript.gen.c19.exec(b7b11bfd779a718cabdce9ea9162012f.js)
   at org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:492)        
   
   
   
<strong>Test 2</strong>
          
var jsonString = "{'test': 'test'}";
logger.log( jsonUtils.toObject(jsonString) );



500 Internal Error
An error inside the HTTP server which prevented it from fulfilling the request.
10080036 Wrapped Exception (with status template): 10080132 Failed to execute script 'Javascript Console Script': null

org.springframework.extensions.webscripts.WebScriptException: 10080037 Wrapped Exception (with status template): 10080138 Failed to execute script 'Javascript Console Script': null
   at org.springframework.extensions.webscripts.AbstractWebScript.createStatusException(AbstractWebScript.java:1067)
   at de.fme.jsconsole.ExecuteWebscript.executeScriptContent(Unknown Source)
   at de.fme.jsconsole.ExecuteWebscript.access$100(Unknown Source)
   at de.fme.jsconsole.ExecuteWebscript$2.execute(Unknown Source)
   at de.fme.jsconsole.ExecuteWebscript$2.execute(Unknown Source)
   at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:401)
   at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:291)
   at de.fme.jsconsole.ExecuteWebscript.runWithTransactionIfNeeded(Unknown Source)
   at de.fme.jsconsole.ExecuteWebscript.access$000(Unknown Source)
   at de.fme.jsconsole.ExecuteWebscript$1.doWork(Unknown Source)
   at de.fme.jsconsole.ExecuteWebscript$1.doWork(Unknown Source)
   at org.alfresco.repo.security.authentication.AuthenticationUtil.runAs(AuthenticationUtil.java:529)
   at de.fme.jsconsole.ExecuteWebscript.runScriptWithTransactionAndAuthentication(Unknown Source)
   at de.fme.jsconsole.ExecuteWebscript.execute(Unknown Source)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:365)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:517)
   at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:333)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:377)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:209)
   at org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:132)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:61)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:465)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
   at java.lang.Thread.run(Thread.java:680)
Caused by: org.alfresco.scripts.ScriptException: 10080138 Failed to execute script 'Javascript Console Script': null
   at org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:202)
   at org.alfresco.repo.processor.ScriptServiceImpl.execute(ScriptServiceImpl.java:212)
   at org.alfresco.repo.processor.ScriptServiceImpl.executeScript(ScriptServiceImpl.java:174)
   at org.alfresco.repo.web.scripts.RepositoryScriptProcessor.executeScript(RepositoryScriptProcessor.java:102)
   … 36 more
Caused by: java.lang.NullPointerException
   at org.springframework.extensions.webscripts.json.JSONUtils.toObject(JSONUtils.java:138)
   at org.springframework.extensions.webscripts.json.JSONUtils.toObject(JSONUtils.java:118)
   at sun.reflect.GeneratedMethodAccessor582.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:155)
   at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:243)
   at org.mozilla.javascript.optimizer.OptRuntime.call1(OptRuntime.java:66)
   at org.mozilla.javascript.gen.c48._c0(9070d44753d964f375056b15cfef5a86.js:7)
   at org.mozilla.javascript.gen.c48.call(9070d44753d964f375056b15cfef5a86.js)
   at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393)
   at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2834)
   at org.mozilla.javascript.gen.c48.call(9070d44753d964f375056b15cfef5a86.js)
   at org.mozilla.javascript.gen.c48.exec(9070d44753d964f375056b15cfef5a86.js)
   at org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:492)
   at org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:198)
   … 39 more   
                     
                                                                     


<strong>Test 3</strong>
       
var jsonString = {'test': 'test'};
logger.log( jsonUtils.toObject(jsonString) );                



500 Internal Error
An error inside the HTTP server which prevented it from fulfilling the request.
10080041 Wrapped Exception (with status template): 10080162 Failed to execute script 'Javascript Console Script': null


2 REPLIES 2

kaynezhang
World-Class Innovator
World-Class Innovator
write like this

var jsonString = '{"test": "test"}';
jsonUtils.toObject(jsonString) ;

jsonUtils will convert it correctly, but you can't pass the return result to logger.log(),logger.log only accept string parameter.
so

logger.log( jsonUtils.toObject(jsonString) );

will not work ,but
 var jsonString = '{"test": "test"}';
jsonUtils.toObject(jsonString) ;
will work

kaynezhang
World-Class Innovator
World-Class Innovator
or you can use double quote mark format string

var jsonString  = "{\"lastName\":\"zhang\",\"location\":\"N.A\",\"firstName\":\"kayne\"}";
    jsonUtils.toObject(jsonString);


you can't use json object directly like below

var jsonString = {'test': 'test'};
jsonUtils.toObject(jsonString);
, It 'll throw null pointer exception