cancel
Showing results for 
Search instead for 
Did you mean: 

dump at if condition statement. Null pointer Exception

Former Member
0 Kudos

hi all,

I have a recursive node as a child node for a value node. I have created an instance of the value node.

I also have accessed current elem of this value node. Since every elem of this value node will have recursive node also in it, I want to access the recursive node present in the elem.

I want to do this using general node and element API - ie IWDNode and IWDNodeElement.

No specific node name can be used coz this is done for a generic method which takes a value node a as parameter and tries to create child elems for the node accessing the recursive node present in it.

How can this be done ?

I am using the following code.

String ChildName = SourceNode.getNodeInfo().getName().split("_")[1] + "Rec";

IWDNode childNode = SourceNode.getChildNode(ChildName, 0);

if (childNode == null || childNode.size() == 0 ) {

It throws a dump at if condition statement. ( Null pointer Exception)

Any help appreciated !!

Edited by: ymb on Mar 25, 2009 1:23 PM

Accepted Solutions (1)

Accepted Solutions (1)

pravesh_verma
Active Contributor
0 Kudos

Hi,

The code is perfectly fine, just a small chage.. Change it to:


if (childNode == null && childNode.size() == 0 ) {

}

I hope that fix the issue!! The issue is even if the childNode is null it will try to execute the next if which is: childNode.size() and therefore it will give error.

Thanks nad Regards,

Pravesh

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi

Use this

if (null == childNode && childNode.size() == 0 )

Regards

-SS

Edited by: Sridhar S on Mar 25, 2009 1:36 PM

Former Member
0 Kudos

it throws a dump with this also ( null pointer )

if (childNode.size() != 0 ) {

Former Member
0 Kudos

Your child node might be null so when you apply the size method on it, it would be throwing null pointer

"childNode.size()"

Former Member
0 Kudos

Is there any way by whcih I can access child node ( of recursive type ) of a value node. in a generic way

not thru wdContext API but by using IWDNode and IwdNodelementAPIs

pravesh_verma
Active Contributor
0 Kudos

Hi,

Its actually pity simple. If you will read the Javadocs you will find that there are various ways in which you can get the list of all chil nodes of a parent node.

You just have to iterate over the chil nodes and get the names of the node.

Please check this link for JAVA docs: http://help.sap.com/javadocs/nwce/current/wdr/index.html

However for your requirement there can be following ways to get the list of the chil nodes. Please check these two code snippets:


    IWDNode node = wdContext.nodeNode1();
    Iterator itr = node.getNodeInfo().iterateChildren();
    String nodeName = null;
    String parentName = null;
    while(itr.hasNext()){
    	nodeName = ((IWDNode)itr.next()).getNodeInfo().getName();
    	parentName = ((IWDNode)itr.next()).getNodeInfo().getParent().getName();
    	wdThis.wdGetAPI().getComponent().getMessageManager().reportSuccess("Node Name is: " 
+ nodeName +"and it is under the parent Node named: " +parentName);
		
    }

Also there is another approach to do teh same, please have a look to this code snippet:


List list = node.getNodeInfo().getChildren();
String nodeName = null;
String parentName = null;
Iterator itr1 = list.iterator();
	
while (itr1.hasNext()) {
	nodeName = ((IWDNode) itr1.next()).getNodeInfo().getName();;
	parentName = ((IWDNode)itr1.next()).getNodeInfo().getParent().getName();
	wdThis.wdGetAPI().getComponent().getMessageManager().reportSuccess("Node Name is: " 
+ nodeName +"and it is under the parent Node named: " +parentName);
	}

I am sure this will all your issues and also you will not get the null pointer exception which was your initial issue.

Please revert back in case you have any further issues.

Thanks and Regards,

Pravesh

Former Member
0 Kudos

Hello guys,

I think the code you have its confusing, but I will explain why you're getting the Null Pointer.


if (childNode == null || childNode.size() == 0 )

In this case, if the statement " childNode == null " evaluates true, you have a NULL in that guy.

Then you're asking for a NULL do get its size(). (The next statement..)

Pravesh code has one fault:


while (itr1.hasNext()) {
	nodeName = ((IWDNode) itr1.next()).getNodeInfo().getName(); // THIS IS POINTING TO NEXT
	parentName = ((IWDNode)itr1.next()).getNodeInfo().getParent().getName(); // NEXT AGAIN..
	wdThis.wdGetAPI().getComponent().getMessageManager().reportSuccess("Node Name is: " + nodeName +"and it is under the parent Node named: " +parentName);
}

It should be:


while (itr1.hasNext()) {
    IWDNode currentNode = (IWDNode) itr1.next();
    nodeName = currentNode.getNodeInfo().getName();
    parentName = currentNode.getNodeInfo().getParent().getName();
    wdThis.wdGetAPI().getComponent().getMessageManager().reportSuccess("Node Name is: " + nodeName +"and it is under the parent Node named: " +parentName);
}

Hope it helps,

Regards,

Daniel

Edited by: Daniel Ruiz on Mar 26, 2009 3:35 AM

pravesh_verma
Active Contributor
0 Kudos

Hi,

Yes he is correct!! I have done a very stupid mistake.. Actually I had just copid and pasted the code of first line to the next line. Have not realized that the iterator will be moved to next element.

Whatsoever, the second statement for getting the parent name was anyways not your requirement, But I just tried to make it more elaborative.

Please do the needful.. Just get the iterator in one statement and then use the iterator.

I hope this solves the problem.

Thanks and Regards

Pravesh

Former Member
0 Kudos

Hi,

Instead of "OR" || use "AND" Operator &&

Regards

Ayyapparaj

Former Member
0 Kudos

thanks for reply. Yes, the problem is wrong usage of operators but there seems to be one more problem.

When I try to access the name of chilNode ie by childNode.getNodeInfo.getname(), it thorws a dump again Null pointer.

Any ideas on this ?

Former Member
0 Kudos

Why are you using

 if (childNode == null || childNode.size() == 0 ) 

And not

 if (childNode == null && childNode.size() == 0 )

Sorry had posted the 2nd line by mistake.

Edited by: Anagha Jawalekar on Mar 25, 2009 6:00 PM