Avatar

Please consider registering
guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed Topic RSS sp_TopicIcon
NullPointerException when creating a subscription for data changes
June 18, 2018
15:54, EEST
Avatar
Thomas Reuther
Member
Members
Forum Posts: 33
Member Since:
September 18, 2017
sp_UserOfflineSmall Offline

Hi,

I’m using SDK 3.1.2 and I followed the steps of the client tutorial (chapter 12) to create my first subscription for data changes:

Subscription subscription = new Subscription();
subscription.setPublishingEnabled(true);
subscription.setPublishingInterval(100D);

MonitoredDataItem monItem = new MonitoredDataItem(myExpandedNodeId, UnsignedInteger.getfromBits(13), MonitoringMode.Reporting);

subscription.addItem( monItem, 1 );
uaClient.addSubscription( subscription ); // NullPointerException

And the stack trace of the exception is as follows:

java.lang.NullPointerException
at com.prosysopc.ua.client.MonitoredItem.getNodeId(Unknown source)
at com.prosysopc.ua.client.Subscription.a(Unknown source)
at com.prosysopc.ua.client.Subscription.createMonitoredItems(Unknown source)
at com.prosysopc.ua.client.Subscription.createItems(Unknown source)
at com.prosysopc.ua.client.UaClient.a(Unknown source)
at com.prosysopc.ua.client.UaClient.addSubscription(Unknown source)

Without the SDK’s source code I cannot see what I did wrong here. Some missing initialization?
The node I subscribe is a variable node with a custom defined enumeration as data type.

Kind regards
Thomas Reuther

June 18, 2018
17:04, EEST
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 1026
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

Hi,

Is the client connected? How is your myExpandedNodeId constructed? If with a Namespace uri, does the server have a matching entry in the NamespaceArray?

Also if you are subscribing to the Value Attribute, you can use just the MonitoredDataItem constructor that just takes (Expanded)NodeIds. The Attributes class (of package org.opcfoundation.ua.core contains constants for the Attributes).

It might be safer to convert the ExpandedNodeId to NodeId before passing it to the constructor (via UaClient.getNamespaceTable().toNodeId(), note that the client must be connected to the server, as the mapping is read from it).

June 19, 2018
17:05, EEST
Avatar
Thomas Reuther
Member
Members
Forum Posts: 33
Member Since:
September 18, 2017
sp_UserOfflineSmall Offline

I created the expanded nodes ids like this:

ExpandedNodeId myId = ExpandedNodeId.parseExpandedNodeId("nsu=http://mycompany.com/program;i=1234");

When I create MonitoredDataItems using a NodeId instead of a ExpandedNodeId then it works fine!

And yes, my client is connected and the namespace exists.

My new converter is implemented as follows:

private NodeId toNodeId(ExpandedNodeId expNodeId) throws ServiceResultException {
return uaClient.getNamespaceTable().toNodeId(expNodeId);
}

With this converter the creation of monitored items is successful. Using expanded node ids instead it fails.

Thank you for your information!!! 🙂

October 24, 2018
8:55, EEST
Avatar
Thomas Reuther
Member
Members
Forum Posts: 33
Member Since:
September 18, 2017
sp_UserOfflineSmall Offline

In SDK ver. 3.1.6 this problem still exists. This time (trying event subscriptions) I found out that the NullPointerException only occurs when I also specify a client handle.

subscription.addItem(expandedNode, uintHandle);
client.addSubscription(subscription); // NullPointerException

But when I omit the client handle, then there is no NullPointerException. But with ExpandedNodeId there is no notification for an event.

October 24, 2018
10:47, EEST
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 1026
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

Hi,

That is somewhat an error from the SDK side that the method taking the clientHandle is public, it should be protected instead (I should have noticed this from your orginal post…). SDK uses that internally. That is actually the source of the NPE (well at least one way it can happen) if you call it.

In practice you do not need/cannot to define the handle, SDK takes care of that for you. It generates one (internally in the monitored items constructors) and uses it to link the notifications to the item and informs you the changes via the listeners, setDataChangeListener(MonitoredDataItemListener listener) for MonitoredDataItems and setEventListener(MonitoredEventItemListener listener) for MonitoredEventItem.

Generally speaking the ExpandedNodeIds are a common problem, basically the SDK does internally in the item is:

getSubscription().getClient().getNamespaceTable().toNodeId(expandedNodeId);

to change it to a NodeId (as that is what the UA service calls take). Calling the addItem(MonitoredItem item) will set the Subscription to the item, the one taking handle does not -> NPE (SDK will internally call the additem taking the handle as part of the addItem process).

Forum Timezone: Europe/Helsinki

Most Users Ever Online: 1919

Currently Online:
27 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

Heikki Tahvanainen: 402

hbrackel: 144

rocket science: 88

pramanj: 86

Francesco Zambon: 83

Ibrahim: 78

Sabari: 62

kapsl: 57

gjevremovic: 49

Xavier: 43

Member Stats:

Guest Posters: 0

Members: 737

Moderators: 7

Admins: 1

Forum Stats:

Groups: 3

Forums: 15

Topics: 1524

Posts: 6450

Newest Members:

fannielima, kristiewinkle8, rust, christamcdowall, redaahern07571, nigelbdhmp, travistimmons, AnnelCib, dalenegettinger, howardkennerley

Moderators: Jouni Aro: 1026, Pyry: 1, Petri: 0, Bjarne Boström: 1026, Jimmy Ni: 26, Matti Siponen: 346, Lusetti: 0

Administrators: admin: 1