cancel
Showing results for 
Search instead for 
Did you mean: 

Exception: Must not add elements to a Node of cardinality 0..1 or 1..1

Former Member
0 Kudos

Dears,

I met a problem while studying the official tutorial "MasterDetail". The code in tutorial works well. I made a little modification to it, and got the exception as titled. I'm rather confused at the cause of the exception.

As you know, the scenario is that, the view context has node "Customer" (cardinality: 0..n; singleton: true), and the "Customer" has a child node "Address" (cardinality: 0..1; singleton: false).

My error occurs when: Adding Element "Address" to node "Customer".

The code in the turorial is something like that:

// Create and Bind Customer Element
IPrivateMasterDetailView.ICustomerElement newCustomerElement = wdContext.createCustomerElement();
newCustomerElement.setName(customerObj.getCustomerName());
wdContext.nodeCustomer().addElement(newCustomerElement);
		
// Create new Address Element
IPrivateMasterDetailView.IAddressElement addressElement = wdContext.createAddressElement();
addressElement.setAddress(customerObj.getAddress());

// Add AddressElement to CustomerNode
newCustomerElement.nodeAddress().addElement(addressElement);

My modification is just on the last sentence:

wdContext.nodeCustomer().currentCustomerElement().nodeAddress().addElement(addressElement);

The modification results the exception:

<b>com.sap.tc.webdynpro.progmodel.context.ContextException: Node(MasterDetailView.Customer.0.Address): must not add elements to a Node of cardinality 0..1 or 1..1</b>

I'm wondering, isn't "newCustomerElement" equals to "wdContext.nodeCustomer().currentCustomerElement()"?

Why is it allowed to add elements in the tutorial, but not in my code?

Thanks in advance.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

You can try this:

wdContext.nodeCustomerElement().getChildNode("Address", 0).addElement(addressElement);

Regards,

Kiran Chennapai

Former Member
0 Kudos

Dear Ayyapparaj,

Thanks for your quick response.

Dear Kiran,

Thank you. your code works.

It seems I havn't made myself clear:

I'm not seeking a solution for a piece of code working well - the tutorial code works well.

I just want to know, what's the difference between the tutorial code and my code, and why this exception is thrown out?

Message was edited by:

gangtee gangtee

Former Member
0 Kudos

HI,

One thing we have to observe here..

Adding an element to a child without adding to Parent will throw this sort of Exception

If we use like in the tutorial.. It is first adding an element to the Parent <Custoemer>

But if you use

wdContext.nodeCustomer().currentCustomerElement().nodeAddress().addElement(addressElement);

Here No parent element will be created in this statement

so, we have to add child element <addressElement> to the parentelement <newCustomerElement> which is previously created

Regards

LakshmiNarayana

Former Member
0 Kudos

Dear LakshmiNarayana,

What I modify on the turorial code is just the last sentense:

from -->

<b>

newCustomerElement

</b>

.nodeAddress().addElement(addressElement);

to -->

<b>

wdContext.nodeCustomer().currentCustomerElement()

</b>

.nodeAddress().addElement(addressElement);

As you have seen in the former code,

wdContext.nodeCustomer().addElement(newCustomerElement);

Since an CustomerElement has been added to CustomerNode, in my opinion,

"<b>wdContext.nodeCustomer().currentCustomerElement()</b>" should return the "<b>newCustomerElement</b>" or something equivalent,

shouldn't it?

Former Member
0 Kudos

Hi,

You have to add the child element to the object of parentElement..

So, it should be added to "newCustomerElement."

I don't think if wdContext.nodeCustomer().currentCustomerElement is an object of Parent

Regards

LakshmiNarayana

former_member197348
Active Contributor
0 Kudos

Hi gangtee,

<b>newCustomerElement</b> is an object of class <b>IPrivateMasterDetailView.ICustomerElement</b>. You can perform all functionality <b>[.setX(),.addX() etc.]</b> of <b>IPrivateMasterDetailView.ICustomerElement</b> for <b>newCustomerElement</b>. But <b>wdContext.nodeCustomer().currentCustomerElement()</b> is an element that is at lead selection. By creating and adding element to node does not set the lead selection. So, in this case <b>wdContext.nodeCustomer().currentCustomerElement()</b> returns <b>null</b>, since there is no element at lead selection.

Hope I have made it clear.

regards,

Siva

Message was edited by:

Siva Rama Krushna

Former Member
0 Kudos

Dear Siva,

Thanks for your explanation. I'm clearer now.

But, if method <b>addX()</b> just adds element, without setting lead selection, then, when and how is "lead selection" set?

And, when can I use method <b>wdContext.nodeCustomer().currentCustomerElement()</b> as expected?

Former Member
0 Kudos

Hi,

Add this statement after adding parentElement newCustomerElement..

wdContext.nodeCustomer().setLeadSelection(1);

Then use wdContext.nodeCustomer().currentCustomerElement()

Regards

LakshmiNarayana

former_member197348
Active Contributor
0 Kudos

Hi gangtee,

As you know to set the leadSelection we use node.setLeadSelection(int); Coming to your question wdContext.nodeCustomer().currentCustomerElement() should be handled with care. Generally we use in the table post Output oprations such as update/delete the selected element in table. It always throws an error if you use currenElement while creating even after the element is created and bound to the parent node.Once you start the node and element operations you will understand the cocepts gradually.

Best reagrds,

Siva

Former Member
0 Kudos

A fresh week starts. Hopes everyone feel fresh again. -


Dear LakshmiNarayana and Siva,

Thank you very much. It works. I understand now.

Dear Mithu,

Thank you. It also works.

Message was edited by:

gangtee gangtee

Answers (2)

Answers (2)

former_member751941
Active Contributor
0 Kudos

Hi Gangtee,

use "bind" method instead off "addElement" for node cardinality 0..1 or 1..1.

wdContext.nodeCustomer().currentCustomerElement().nodeAddress().bind(addressElement);

For 0..n node use code

wdContext.node<node_name>().addElement(wdContext.create<node_name>Element());

For 0..1 node use code.

wdContext.node<node_name>().bind(wdContext.create<node_name>Element());

Make sure cardinality of node Customer is 0..1 .

Regards,

Mithu

Former Member
0 Kudos

Hi,

Just check the cardinality of the address node , is it 1..1

Regards

Ayyapparaj