cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

MQTT Datachange NullPointerException

drichter
14-Alexandrite

MQTT Datachange NullPointerException

Hi,

 

I have a Thing (based on MQTT) with some properties. One of them is called MQTT-Value which is configurate as MQTT-Target. Than I have a subscrption on datachange of the MQTT-Value-property.

 

The subscription looks like this:

me.Environment_Humidity = eventData.newValue.value.humidity;
me.Environment_Particles = eventData.newValue.value.particles;
me.Environment_Temperature = eventData.newValue.value.temp;

...

 

Now I get an error on every time where a new mqtt-value arrives:

[message: Execution error in service script [Event.DataChange:Property.MQTT_Environment] :: Wrapped java.lang.NullPointerException (Event.DataChange:Property.MQTT_Value#1)]

 

The MQTT-Value lookes like this:

{"temp":17.540277128455315,"humidity":48.41761546496747,"particles":409}

 

If I set the MQTT-Value by hand everything works. I think this had todo with permissions. I set visibility&run time-permissions for "system"-user but this doesn't work.

 

I working with ThingWorx 8.2.1-b140

1 ACCEPTED SOLUTION

Accepted Solutions
PaiChung
22-Sapphire I
(To:drichter)

Ok so potentially it is your logging that isn't working.

Please check if you have Visibility permissions for the user on the ValueStream and more importantly the Persistence Provider

It might relate to this:

https://community.ptc.com/t5/IoT-AR-Tech-Tips/Visibility-Permissions-when-logging-properties/m-p/534564#M690

 

View solution in original post

8 REPLIES 8
PaiChung
22-Sapphire I
(To:drichter)

It might just be that you aren't parsing the JSON properly.

did you try:

eventData.newValue.humidity;

plus you may have to cast it to a number since it is a string.

 

drichter
14-Alexandrite
(To:PaiChung)

I can watch the data with:

logger.warn("eventData.newValue.value.humidity");

 

and test the type with:

logger.warn("type of eventData.newValue.value.humidity");

 

eventData.newValue gives me an infotable with field time, quality and value.

PaiChung
22-Sapphire I
(To:drichter)

Since the logger.warn is working fine, perhaps you just need to cast it to a variable before assigning it to the property.

So just do a 

var newHumidity = ...

and me.Humidity = newHumidity

 

drichter
14-Alexandrite
(To:PaiChung)

Nope, this does not work either :/

 

Now I on a older thingworx maschine (8.1.1-b108) and I have the same problem.

drichter
14-Alexandrite
(To:drichter)

I think the problem have something todo with the property writes of me.xxx. I change my subscription code to this:

 

try {
    var newValue = eventData.newValue.value;

    var newHumidity = newValue.humidity;
    var newParticles = newValue.particles;
    var newTemperature = newValue.temp;

    me.Environment_Humidity = 1;
    me.Environment_Particles = 2;
    me.Environment_Temperature = 3;
}
catch(err) {
    logger.error("Error on line " + err.lineNumber + ": " + err.message);
}

So you can see I add some numbers and ignore the mqtt stuff. The mqtt value will changed every 5s (means every 5 seconds the subscription will be called). Now I get this errors (every entry is 5s apart):

  • Error on line 7: java.lang.NullPointerException: null
  • Error on line 8: java.lang.NullPointerException: null
  • Error on line 9: java.lang.NullPointerException: null

The values in the thing are changed (each with 5 seconds distance).

drichter
14-Alexandrite
(To:drichter)

If I set DataChange of my properties (Environment_Humidity, Environment_Particles, Environment_Temperature) to "Never" I get no errors and the values will be set. But than I can't log the value history.

PaiChung
22-Sapphire I
(To:drichter)

Ok so potentially it is your logging that isn't working.

Please check if you have Visibility permissions for the user on the ValueStream and more importantly the Persistence Provider

It might relate to this:

https://community.ptc.com/t5/IoT-AR-Tech-Tips/Visibility-Permissions-when-logging-properties/m-p/534564#M690

 

drichter
14-Alexandrite
(To:PaiChung)

Thx this works :)

Top Tags