cancel
Showing results for 
Search instead for 
Did you mean: 

Java Mapping error

Former Member
0 Kudos

Hi Experts,

I am practicing the java mapping in PI,  My requirement is simple I just want to concatenate the two input strings. And my code is like this

I have few doubts :-

1) execute method is mandatory 

CODE :-

import java.io.*;
import java.util.*;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import com.sap.aii.mapping.api.AbstractTrace;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

public class Java_Test extends AbstractTransformation {
public String var1;
public String var2;

public void transform(TransformationInput input, TransformationOutput output)throws StreamTransformationException
{
 
  InputStream ins=input.getInputPayload().getInputStream();
  OutputStream outs=output.getOutputPayload().getOutputStream();
  int n,m;
  String result=null;
   
  try{
   DocumentBuilderFactory dfactory=DocumentBuilderFactory.newInstance();
   DocumentBuilder dbuilder=dfactory.newDocumentBuilder();
   Document doc=dbuilder.parse(ins);
   NodeList nl=doc.getElementsByTagName("PMI_SOURCE_PROD");
   NodeList nl1=doc.getElementsByTagName("LOGISTA_CODE");
  
   String PMI_SOURCE_PROD=nl.item(0).getTextContent();
   String LOGISTA_CODE=nl1.item(0).getTextContent();
  
   n=nl.getLength();
   m=nl.getLength();
   if(n==m)
   {
   for(int i=0;i<=n;i++)
   {
    result=PMI_SOURCE_PROD+LOGISTA_CODE;
   }
  
   outs.getOutputPayload().getOutputStream().Writes(result.getBytes());  // I am getting the error in this line Kindly check and help me in this.
    }
  }
  catch(Exception e)
  {
   System.out.println(e);
  }
 
}
 
  }

Thanks ,

Vijay Kumar K.V.N

Accepted Solutions (1)

Accepted Solutions (1)

steve_coombes
Participant
0 Kudos

Hello Vijay

It would be useful to see the Java stack trace, but the error might be because 'result' is null. It will only be populated if the number of nodes in PMI_SOURCE_PROD is the same as the number of nodes in LOGISTA_CODE. You could check if result is null before writing to the OutputStream.

The loop for(int i=0;i<=n;i++) will fail because the index starts at 0 and goes up to n, so you will be reading values for n+1 elements in the array where the number of elements is n. It should probably be i<n.

Also, you appear to be setting result several times but with the same value every time, although this wouldn't cause an error.

regards

Steve

Former Member
0 Kudos

Hi Steve,

I am getting the below error .

Error:-

The method getOutputPayload() is undefined for the type OutputStream.

Please tell me what to do for this.

Thanks,

Vijay Kumar K.V.N

steve_coombes
Participant
0 Kudos

Thanks Vijay.

This line looks fine.

OutputStream outs=output.getOutputPayload().getOutputStream();


Then you can write to the output stream


outs.write(result.getBytes());


I think you are using

outs.getOutputPayload().getOutputStream()

Which is trying to get an OutputPayload object from the OutputStream which will fail.

regards

Steve


Answers (2)

Answers (2)

Muniyappan
Active Contributor
0 Kudos

Hi Vijay,

check this thread in case if you have missed this.

Regards,

Muni.

iaki_vila
Active Contributor
0 Kudos

Hi vijay,

Try with:

outs.getOutputPayload().getOutputStream().write(result.getBytes("UTF-8"));


Check also that the result variable has any value, make a trace before the instruction that throws the exception.


On the other way, i think this is only for testing the java mapping development isn´t it?, because the concatenate function could be done with the standard PI functions.


Regards.