-1

I need insert a new value in a exist array with Drools. My example:

rule "insert new address"
dialect "java"
when
     $data : Data( source.address != null)
then
     Address address = (Address) $data.source.address
     System.out.println("Element: "+address );
     $data.target.addressList.add(address);
end

The error that happend is this: Exception executing consequence for rule "insert new address" in rules: [Error: $data.target.addressList.add(address): null]

EDIT: Added the model

public class Data {
  private Source source;
  private Client target;
}

public class Source {
  ...
  private Address address;
}

public class Client {
  ...
  private List<Address> addressList;
}
Ransarot
  • 9
  • 1
  • 3

1 Answers1

0

In answer to the question in your title, which is how to add an element to array -- the answer is basically "the same way you would in Java."

To answer the question you actually asked, which has no arrays, your error is effectively a NullPointerException, or another indicator that the field cannot be modified (eg an immutable list.)

This:

Error: $data.target.addressList.add(address): null]

Means that either $data.target or $data.target.addressList is null, or possibly $data.target.addressList is an immutable list.

Make sure that whatever "target" is has been initialized, and that its "addressList" is also initialized as a mutable list type.

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
  • If y put this: System.out.println("Size"+$data.target.addressList.size()); the app write "3" (that is correct because exist 3 elements) – Ransarot Apr 20 '21 at 06:02
  • I've addded the model in post to see it – Ransarot Apr 20 '21 at 06:10
  • You've not indicated how or when any of these models are initialized, nor what their getters are. – Roddy of the Frozen Peas Apr 20 '21 at 06:20
  • The models is a DTO, come from a client like a JSON. I've found the problem. The problem is this: java.lang.UnsupportedOperationException for inmmutability. I've created a method with this this.addressList = new ArrayList<>(this.addressList); and then .add works. – Ransarot Apr 20 '21 at 07:02