|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
Is null for NBElement? Getting Object reference not set... |
« View previous topic :: View next topic » |
Author |
Message
|
grasher134 |
Posted: Thu Aug 25, 2016 5:07 am Post subject: Is null for NBElement? Getting Object reference not set... |
|
|
Acolyte
Joined: 22 Oct 2014 Posts: 62
|
Hello,
In ESQL if I try code like this if SomeElement doesn't exist in inputroot i'll get NULL. I can then use it, check for it, use coalesce, etc..
Code: |
SET tempVal = InputRoot.XMLNSC.SomeRoot.SomeElement;
|
In c#
Code: |
inputRoot["XMLNSC"]["Email"]["SomeElement"] |
gives me null in Locals, BUT it gives me "Object reference not set to an instance of an object" when I try something like
Code: |
if (inputRoot["XMLNSC"]["Email"]["SomeElement"] != null) |
Is there any method like isNull in c# for NBElement? |
|
Back to top |
|
 |
mqjeff |
Posted: Thu Aug 25, 2016 5:12 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
There are element that don't exist, and then there are elements that have a null value. _________________ chmod -R ugo-wx / |
|
Back to top |
|
 |
grasher134 |
Posted: Thu Aug 25, 2016 5:16 am Post subject: |
|
|
Acolyte
Joined: 22 Oct 2014 Posts: 62
|
mqjeff wrote: |
There are element that don't exist, and then there are elements that have a null value. |
I get you, but I haven't found anything like isExists either. |
|
Back to top |
|
 |
mqjeff |
Posted: Thu Aug 25, 2016 6:06 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
ESQL will create members of a tree when you try to access them, if they don't exist... At least when you try and set a value.
If you're trying to do something if the element has a null value, you can use the findOrCreate method.
If you're trying to do something if the element doesn't exist, you'll have to use the getPlainEnumerator and iterate over the children and check the fieldname. _________________ chmod -R ugo-wx / |
|
Back to top |
|
 |
grasher134 |
Posted: Thu Aug 25, 2016 10:56 pm Post subject: |
|
|
Acolyte
Joined: 22 Oct 2014 Posts: 62
|
mqjeff wrote: |
ESQL will create members of a tree when you try to access them, if they don't exist... At least when you try and set a value.
If you're trying to do something if the element has a null value, you can use the findOrCreate method.
If you're trying to do something if the element doesn't exist, you'll have to use the getPlainEnumerator and iterate over the children and check the fieldname. |
Thank you for your help.
So ESQL creates an element and assigns null to it. And when I propogate the tree to another node it deletes elements with the null value.
I thought about findOrCreate method, but I then rejected the idea, as it appeared to me as a clumsy one. I wanted to check whether the element exists or not. I think findOrCreate will suit me in both cases, as the value of new created element will be null, and I can check that. I'll look into iteration too. Still I'm curious why I see null in debug, but I cannot use it in "!= null" clause..
EDIT: Message is read-only tells me that getPlainEnumerator is the only true way.
EDIT2: So for the sake of future generations.. NBElement doesn't support foreach method, so you will need something like this
Code: |
class EnumerableWrapper
{
private readonly NBElement obj;
public EnumerableWrapper(NBElement obj)
{
this.obj = obj;
}
public IEnumerator<NBElement> GetEnumerator()
{
return obj.GetGenericEnumerator();
}
}
|
And this is the cycle (maybe ToString() was excessive..)
Code: |
foreach (NBElement item in new EnumerableWrapper(someElement))
{
if (item.Name.ToString() == "YourName")
{
// some Code
}
}
|
|
|
Back to top |
|
 |
smdavies99 |
Posted: Thu Aug 25, 2016 11:47 pm Post subject: |
|
|
 Jedi Council
Joined: 10 Feb 2003 Posts: 6076 Location: Somewhere over the Rainbow this side of Never-never land.
|
Quote
Code: |
SET tempVal = InputRoot.XMLNSC.SomeRoot.SomeElement;
|
If you look at a usertrace output for the above when
InputRoot.XMLNSC.SomeRoot.SomeElement;
Does not exist then you will see that the 'tempVal' object is actually deleted.
If you want a positive indicator of an elements existence then
Code: |
SET tempVal = COALESCE(InputRoot.XMLNSC.SomeRoot.SomeElement,'XXYYXXSS');
|
Where 'XXYYXXSS' is a string (or an integer or whatever data type it is) that can NEVER exist in the target element.
Then when you test 'tempVal' you test for it not equal to that impossible value.
As an old skool developer I like to do it this way. i.e. the way we used to code stuff before this dynamic allocation and deletion of variables muddied the waters. _________________ WMQ User since 1999
MQSI/WBI/WMB/'Thingy' User since 2002
Linux user since 1995
Every time you reinvent the wheel the more square it gets (anon). If in doubt think and investigate before you ask silly questions. |
|
Back to top |
|
 |
|
|
 |
|
Page 1 of 1 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|