JavaScript transform NULL pointer Error

273 views
Skip to first unread message

Luke B

unread,
Jun 16, 2014, 7:43:30 AM6/16/14
to ope...@googlegroups.com

I'm having a problem with JavaScript throwing a NULL pointer error.

It was occurring in Openhab 1.4 and I've just upgraded to Openhab 1.5 and it's still occurring.

 

I'm trying to transform the output of an MQTT binding to display a googlemap link. Although at the moment I'd be happy with it displaying anything.

 

Here is the sitemap section to display the link and attached is the sitemap screenshot

Frame {

Text item=gps_string

Text item=Map_Luke_Phone

}

As you can see the gps_string displays however the other doesn't. 

This is the error it throws from the debug console


21:36:33.684 INFO  runtime.busevents[:26] - gps_string state updated to {"_type": "location", "lat": "-42.8533391", "lon": "147.2942751", "tst": "1402918551", "acc": "30.0", "batt": "77"}

21:36:33.684 DEBUG o.o.c.t.i.s.JavaScriptTransformationService[:61] - about to transform '{"_type": "location", "lat": "-42.8533391", "lon": "147.2942751", "tst": "1402918551", "acc": "30.0", "batt": "77"}' by the Java Script 'owntrack_maps.js'

21:36:33.687 ERROR o.o.b.m.i.MqttMessageSubscriber[:138] - Error processing MQTT message.

java.lang.NullPointerException: null

        at org.openhab.core.transform.internal.service.JavaScriptTransformationService.transform(JavaScriptTransformationService.java:77)

        at org.openhab.binding.mqtt.internal.MqttMessageSubscriber.processMessage(MqttMessageSubscriber.java:121)

        at org.openhab.io.transport.mqtt.internal.MqttBrokerConnection.messageArrived(MqttBrokerConnection.java:566)

        at org.eclipse.paho.client.mqttv3.internal.CommsCallback.handleMessage(CommsCallback.java:336)

        at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:148)

        at java.lang.Thread.run(Thread.java:744)

 

And here is the section from the item and transform files.

/* GeoLocation */

Group Location (All)

String gps_string "prior [%s]" <wind> (Location) { mqtt="<[mosquitto:owntracks/luke/#:state:default]" }

String Map_Luke_Phone "after [%s]" (Location) { mqtt="<[mosquitto:owntracks/luke/#:state:JS(owntrack_maps.js)]" }

 

server transform # pwd

/opt/openhab-1.5/configurations/transform

server transform # ls -l owntrack_maps.js

-rwxr-xr-x 1 root root 145 Jun 16 21:23 owntrack_maps.js

server transform # cat owntrack_maps.js

//var location = eval('{' + input + '}');

//result = "http://maps.google.com/maps?z=12&t=m&q=loc:" + location.lat + "+" + location.lon;

//"http://maps.google.com/maps?z=12&t=m&q=loc:" + location.lat + "+" + location.lon;

BLAHBLAH

 

As you can see I've tried the JS file just about all different ways and all throw NULL pointer errors.

I'm pretty confident the Mosquitto MQTT broker side is OK because I'm running a mosquitto_sub on the same topic and it's fine, pluis the gps_string item shows the correct pre-transform data as well.

I'm sure it's something stupid I've done in the item binding however I don’t know what or wher eto look next.

 

Thanks,

Luke

sitemap.png

dan cunningham

unread,
Jun 16, 2014, 9:11:07 AM6/16/14
to ope...@googlegroups.com
Hi Luke, are the lines commented out with '//' ?  Is that you testing?  The "result" keyword is what needs to be set to something.  The file should look like so with no comments:

var location = eval('(' + input + ')');
result = "http://maps.google.com/maps?z=12&t=m&q=loc:" + location.lat + "+" + location.lon;

As you can see it is setting the "result" variable to the location, this is what openHAB is looking for.  If you want to take mqtt out of it and try to isolate the transform for debugging, set the result to some thing like:

--
You received this message because you are subscribed to the Google Groups "openhab" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
For more options, visit https://groups.google.com/d/optout.

Luke B

unread,
Jun 16, 2014, 10:19:10 AM6/16/14
to ope...@googlegroups.com
Thanks Dan for the response,

Yes the // Comments are me testing. I had seen various posts saying it needed to be either way, both setting result for the returned value/string and without setting result.

I tested it with just the code from below
But still no joy, it got me to thinking maybe though it somehow redirects stdout or something so I also tested it as 


But that also returned a NULL pointer error.

Thanks,
Luke

Luke B

unread,
Jun 16, 2014, 10:52:56 AM6/16/14
to ope...@googlegroups.com
OK so some more testing. Removing strings and MQTT from the equation I thought tr a simpler transform

So I created a page called num.html that simply contained a number (1230)
wget -O - localhost/num.html | more
--2014-06-17 00:32:07--  http://localhost/num.html
Resolving localhost... 127.0.0.1
Connecting to localhost|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5 [text/html]
Saving to: âSTDOUTâ

100%[============================================================================================================================================================================>] 5           --.-K/s   in 0s
2014-06-17 00:32:07 (448 KB/s) - written to stdout [5/5]

1230


Created an item
Number Num_blah { http="<[http://localhost/num.html:60000:JS(number.js)]" }

This also produced a NULL pointer

00:27:24.700 DEBUG o.o.b.h.internal.HttpBinding[:157] - item 'Num_blah' is about to be refreshed now
00:27:24.828 DEBUG o.o.c.t.i.s.JavaScriptTransformationService[:61] - about to transform '1230
' by the Java Script 'number.js'
00:27:24.831 ERROR o.o.c.s.AbstractActiveService[:175] - Error while executing background thread HTTP Refresh Service
java.lang.NullPointerException: null
        at org.openhab.core.transform.internal.service.JavaScriptTransformationService.transform(JavaScriptTransformationService.java:77)
        at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:176)
        at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:156)
        at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:173)

Wondering do I need some sort of JavaScript interpreter locally installed, I would guess yes, but also assuming it's installed as part of a java binary package.
I'm currently running with icedtea 7.2.4.7 binary install of java (under Gentoo). All else java related seems to be fine.

server items # java-config -v
java version "1.7.0_55"
OpenJDK Runtime Environment (IcedTea 2.4.7) (Gentoo build 1.7.0_55-b14)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)

I did find two binaries js17 and js24 which look like they maybe javascript interpreters but there is no man entry for them and google provides nothing obvious.
On a whim I tried symlinking them to /usr/bin/js but that still failed to work.

Is there anyway I can test the file runs on the cli with javascript? Or does the JS transform library/function require anything special to be insalled/setup?

Thanks

Luke B

unread,
Jun 16, 2014, 12:16:33 PM6/16/14
to ope...@googlegroups.com
So yet another reply to my own post, but this time with a solution

I moved from icedtea-bin java to oracle-jre-bin. Under Gentoo this isn't too hard you can google it easily.
So now I am running:
server oracle-jre-bin # java -version
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

Low and behold all my Javascript bindings work straight out of the box.
01:51:59.989 DEBUG o.o.c.t.i.s.JavaScriptTransformationService[:61] - about to transform '{"_type": "location", "lat": "-42.8533978", "lon": "147.2943377", "tst": "1402923666", "acc": "24.64", "batt": "70"}' by the Java Script 'owntrack_maps.js'
01:52:01.334 INFO  runtime.busevents[:26] - gps_string state updated to {"_type": "location", "lat": "-42.8533978", "lon": "147.2943377", "tst": "1402923666", "acc": "24.64", "batt": "70"}
01:52:01.343 INFO  runtime.busevents[:26] - Map_Luke_Phone state updated to http://maps.google.com/maps?z=12&t=m&q=loc:0+0

The slightly incorrect response thogh is expected as I still have the JS file as:
I also tested quickly and it doesn't seem to make a difference if you assign it to result or just leave it blank. I'll test this further though.

Lastly something I also discovered is that icedtea java doesn't seem to have the ScriptEngine "javascript", they have something called Rhinoscript and wants to be called like
ScriptEngine engine = manager.getEngineByName("js");

Now I don;t know how to submit a code change but perhaps someone with more skills in that area could implement in https://code.google.com/p/openhab/source/browse/bundles/core/org.openhab.core.transform/src/main/java/org/openhab/core/transform/internal/service/JavaScriptTransformationService.java line 105
modify something like (note this code wont run ever it's my poor attempt at java)

ScriptEngineManager manager = new ScriptEngineManager();
if ScriptEngine engine = manager.getEngineByName("javascript")
else ScriptEngine engine = manager.getEngineByName("JavaScript")
else    ScriptEngine engine = manager.getEngineByName("js")
if (engine == null) {
    System.out.println("Warning: No ScriptEngine engine found.");
    return;
}

Basically it should test a bunch of ScriptEngines (I saw mixedcase as an example too), and if it cant find one fail gracefully.

TL;DR:
Until openhab is patched for various java versions run "standard" Oracle Java JRE

Thanks.

Kai Kreuzer

unread,
Jun 17, 2014, 4:02:52 PM6/17/14
to ope...@googlegroups.com
Thanks for figuring this out!
I didn’t know that there are different script engines for the different JVMs…
Is openHAB running fine on icetea, apart from this problem?

Is anybody interested in creating a PR for https://github.com/openhab/openhab/issues/1156?

Best regards,
Kai


Reply all
Reply to author
Forward
0 new messages