cancel
Showing results for 
Search instead for 
Did you mean: 

PI 7.3: Java method error - Dynamic Configuration

peter_wallner2
Active Contributor
0 Kudos

Dear experts,

I have a java method I am calling from an XSLT mapping and I get a java error. I think I know where

the problem is but I do not know how to fix it. The java code reads out the Directory from the Dynamic configuration.

Then I try to get a substring from the Directory. The substring is set when calling the java method from XSLT.

Here is the java code:

package pi_mappings;

import java.util.*;

import com.sap.aii.mapping.api.*;

public class GetSourceFolder {

   

    static int intbegin=0;

    static int intlength=0;

   

    private static final DynamicConfigurationKey KEY_DIRECTORY = DynamicConfigurationKey

            .create("http://sap.com/xi/XI/System/File", "Directory");

    private Map param;

    

      public void setParameter(Map param) {

          this.param = param;

      }

    public String GetSubstringFromDirectory(int begin, int length) {

        // access dynamic configuration

        DynamicConfiguration conf = (DynamicConfiguration) param

                .get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

        // read source directory from where FTP picks file

        String directory = conf.get(KEY_DIRECTORY);

        // get the relevant characters from the Directory name

        String subdirectory = directory.substring(begin, length);

        return subdirectory;

    }

    public static String retString (String begin, String length){

       

        if (begin != null && begin.length() > 0) {

            intbegin = Integer.parseInt(begin);

        } else {

            return "Error:Specify begin";

        }

       

        if (length != null && length.length() > 0) {

            intlength = Integer.parseInt(length);

        } else {

            return "Error:Specify length";

        }

       

        GetSourceFolder mt = new GetSourceFolder();

        String ret = mt.GetSubstringFromDirectory(intbegin, intlength);

        return ret;

    }

}

From my XSLT I am calling the method "retString" who is supposed to retrun the subdirectory from method "GetSubstringFromDirectory".

But to call that method I need a class instance. And I can't make "GetSubstringFromDirectory" a static method because of the use of "param".

I get this error in SXMB_MONI:

Caused by: java.lang.NullPointerException: while trying to invoke the method java.util.Map.get(java.lang.Object) of an object loaded from field pi_mappings.GetSourceFolder.param of an object loaded from local variable 'this' at pi_mappings.GetSourceFolder.GetSubstringFromDirectory(GetSourceFolder.java:28) at pi_mappings.GetSourceFolder.retString(GetSourceFolder.java:39)

Can someone help me please. I also tried to locally test the mapping which worked for me. I don't know why in PI it does not work. Maybe I am

not allowed to create an instance of the class to call a method?

Thank you very much,

Peter

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member192851
Active Participant
0 Kudos

Yes, you should create class instance.

How do you call retString from xslt?

peter_wallner2
Active Contributor
0 Kudos

Hello Artem Solohin,

I call it with:

<xsl:value-of select="getdir:retString(1,4)"/>

In the XSL declaration I have:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:getdir="pi_mappings.GetSourceFolder"

    version="1.0">

This should be okay I think.

In the java mapping I convert the (1,4) to int values before using them.

Some how the mapping though gives a NullPointerException.

Thank you for your help with this,

Peter

former_member192851
Active Participant
0 Kudos

hmm....

check your Dynamic Configuration in the message - maybe it is empty and param.get returns null?

peter_wallner2
Active Contributor
0 Kudos

Hello Artem,

No, the Dynamic Configuration is okay, it is filled. I think the problem is using non-static methods like my "GetSubstringFromDirectory".

I solved it using one of our java extensions to retrieve the folder name of the source and in XSLT I take the substring then.

thank you for your help and best regards,

Peter

Ryan-Crosby
Active Contributor
0 Kudos

Hi Peter,

It appears to be an issue with instantiating the class GetSourceFolder at line 47.  If you notice lines 15-19 where the Map is declared and set... private Map param is null when you instantiate a new object of the GetSourceFolder class which then will result in the NullPointerException when you try to access anything within the Map.  Adjusting the code to solve that issue should take care of it for you.

Regards,

Ryan Crosby