cancel
Showing results for 
Search instead for 
Did you mean: 

NullPointerException on wdContext.nodeMyNode().getElementAt(i)

Former Member
0 Kudos

This is a strange one...

The following loop throws a NullPointerException :

<br>


   int size = wdContext.nodeZoneList().size(); // size is 42...
   for (int i = 0; i < size; i++)
   {
      // the following throws a NullPointerException when i == 0 !!
      Object obj = wdContext.nodeZoneList().getElementAt(i);
   }

Unless I'm missing something here, shouldn't getElementAt() return valid elements for i=0 to size-1 ??

Anyone with an explanation of what could cause the NullPointerException ?

On an unrelated note; can anyone tell me why my message gets surrounded by [ nobr ] and [ /nobr ] when I post ?

Accepted Solutions (1)

Accepted Solutions (1)

former_member286976
Active Participant
0 Kudos

Hi,

Your statement

// the following throws a NullPointerException when i == 0 !!

Do you mean to say that it throws the error only when i=0 and give the expected results for all other values of i?. That's quite impossible.

Also, is the node named 'ZoneList' a model binded node? Is this a parent node in your WD context or is it a child node in your context. If this is a child node and if the singleton property is set to 'true', you may have to access this child node by the code as below

IWDNode nodeZoneList = parentNode.getChildNode("ZoneList",parentNode.getLeadSelection());
int size = nodeZoneList.size(); // size is 42...
for (int i = 0; i < size; i++)
{
   // the following throws a NullPointerException when i == 0 !!
   IWDNodeElement obj = nodeZoneList.getElementAt(i);
}

But still the wdContext.nodeZoneList().size() returning 42 and the very same wdContext.nodeZoneList() returning null inside the for loop is strange. Here exception is thrown because 'wdContext.nodeZoneList()' returns null; not because getElementAt(i) returns null. It doesn't matter what getElementAt(i) returns and exception won't be thrown for this returning null.

Kindly also check for the cardinality setting (collection and selection, both) of the node in the WD context.

Regards,

Sudeep

Answers (6)

Answers (6)

Former Member
0 Kudos

I solved it !

This code is running inside a view called ZoneSelectionView - which is actually displayed in a popup window, and is used to select one or more zones. The code I've provide is inside a method called onActionOK which is called when the user clicks the OK button to signify his/her selection is complete.

Now, when the user clicks OK, the code should be executed, and the popup window destroyed. The problem was I was destroying the window (which contains the ZoneSelectionView) at the START of the onActionOK method, then trying to execute the rest of the code. By moving the window destruction code AFTER the context processing, the NullPointerException went away.

Which sort of makes sense, when you think about it - I was destroying a window that contained the view... then trying to process logic inside that view.

Thanks to all for your feedback and suggestions... Even though the real source of the problem could not be pinned down by anyone: I hadn't provided the information that I just did... thinking it was irrelevant. When it was in fact crucial..

abhijeet_mukkawar
Active Contributor
0 Kudos

Hi,

just try modifying your code like:

int size = wdContext.nodeZoneList().size(); // size is 42...

<b>wdContext.nodeZoneList().moveFirst();</b>

for (int i = 0; i < size; i++)

{

// the following throws a NullPointerException when i == 0 !!

Object obj = wdContext.nodeZoneList().getElementAt(i);

}

if you get exception at this new line then , i doubt if you have any elements in the collection.

let me know what it gives you

regards

abhijeet_mukkawar
Active Contributor
0 Kudos

Hi,

just try out this code instead of what you are trying,

int size = wdContext.nodeZoneList().size(); // size is 42...

wdContext.nodeZoneList().moveFirst();

for (int i = 0; i < size; i++)

{

// the following throws a NullPointerException when i == 0 !!

Object obj = wdContext.nodeZoneList().currentZoneListElement();

wdContext.nodeZoneList().moveNext();

}

not sure what difference it holds compared to your code , but give it a try

regards

Former Member
0 Kudos

Thanks for all your suggestions... but problem still there...

<p>

<b>Lavanya</b>: Your suggestion of setting object values is unworkable, because the code you suggest is unreachable ! (NullPointerException thrown before we get there)

<p>

<b>Sudeep</b>:

<ol>

<li>The exception gets thrown when i==0. For other values, I don't know... (i never gets past 0)

<li>ZoneList is a value node directly underneath Context root (singleton: 'true'; cardinality: 0..n; selection: 0..n)

<li>I tried 'IWDNode nodeZoneList = wdContext.getChildNode("ZoneList", 0)' - that also throws a NullPointerException... which I find a bit strange, since I can verify in the debugger that wdContext does indeed have a child Node with name='ZoneList'...

<li>exception is NOT thrown because wdContext.nodeZoneList() returns null - if that were the case, the statement 'size=wdContext.nodeZoneList().size()' would also throw the same exception.

<li>In fact, IWDNode nodeZoneList = wdContext.getNodeZoneList() does work. NullPointerException then gets thrown on 'IWDNodeElement element = nodeZoneList.getElementAt(i)'

</ol>

<p>

<b>Naga</b>: I did try using getZoneListElementAt(i) instead of getElementAt(i)... but it still throws a NullPointerException

<p>

<b>Lohitha</b>: typecasting the result of wdContext.nodeZoneList().getElementAt(i) won't work, because the NullPointerException is thrown BEFORE the typecast can be invoked... therefore, no available expression to typecast.

Former Member
0 Kudos

Hi,

int size = wdContext.nodeZoneList().size(); // size is 42...

for (int i = 0; i <<b> =</b>size; i++)

{

// the following throws a NullPointerException when i == 0 !!

IPrivateViewName.InodeElement() obj = (IPrivateViewName.InodeElement())wdContext.nodeZoneList().getElementAt(i);

// retrive ur Column value now.

String ram = obj.get<varName>();

}

it might helps u .

Thanks,

Lohi.

Former Member
0 Kudos

HI,

Use the below code

int size = wdContext.nodeZoneList().size(); 
   for (int i = 0; i < size; i++)
   {
    IWDElement element = wdContext.nodeZoneList().getZoneListElementAt(i);
String yourValue=element.getYourValue();
     //or
String yourValue=wdContext.nodeZoneList().getZoneListElementAt(i).getYourValue();

  }

Regards,

Naga

Former Member
0 Kudos

Hi,

Try this one.

int size = wdContext.nodeZoneList().size(); // size is 42...

for (int i = 0; i < size; i++)

{

// the following throws a NullPointerException when i == 0 !!

Object obj = wdContext.nodeZoneList().getElementAt(i);

//set the values to that obj here itself.

obj.setCtx_XXX(.getXXX);

}

->set all the attribute values according to ur requirement.

->finally bind these values using bind().

I hope it will work now.

Regards,

Lavanya.G