cancel
Showing results for 
Search instead for 
Did you mean: 

Calling BAPI via Jco 3.0

Former Member
0 Kudos

Hi all,

I wrote a simple program that uses jco 3.0 to call a BAPI on R/3 4.6c. I followed the example at http://www.vogella.de/articles/SAPJCo/article.html and replaced the BAPI to BAPI_USER_GET_DETAIL since i dont have the one in the article in my system.

I get a NullPointerException when calling the function. Below is the exception trace + code, any help is appreciated.

The happens at JCoFunction function = connect.getFunction("BAPI_USER_GET_DETAIL");

SORRY THE MARKUP DOES NOT SEEM TO WORK...

public class JcoTester {

static String SAP = "SAP_SERVER";

public static void main(String[] args) {

//SAP System

SapSystem system = new SapSystem();

system.setClient("220");

system.setHost("myservername");

system.setLanguage("en");

system.setSysID("00");

system.setUser("myuserID");

system.setPassword("mypassword");

Connection connect = new Connection(system);

JCoFunction function = connect.getFunction("BAPI_USER_GET_DETAIL");

function.getImportParameterList().setValue("USERNAME", "myuserID");

connect.execute(function);

JCoTable table = function.getTableParameterList().getTable("PARAMETER");

System.out.println(table.isEmpty());

TableAdapterReader tableAdapter = new TableAdapterReader(table);

System.out.println("Number of parameters: " + tableAdapter.size());

System.out.println("parameter ID" + " " + "Parameter value");

for (int i = 0; i < tableAdapter.size(); i++){

System.out.println(tableAdapter.get("PARID") + " " + tableAdapter.get("PARVA"));

tableAdapter.next();

}

}

}


public class Connection {

static String SAP_SERVER = "SAP_SERVER";

private JCoRepository repos;

private JCoDestination dest;

private final Properties properties;

public Connection(SapSystem system){

properties = new Properties();

properties.setProperty(DestinationDataProvider.JCO_ASHOST, system.getHost());

properties.setProperty(DestinationDataProvider.JCO_SYSNR, system.getSysID());

properties.setProperty(DestinationDataProvider.JCO_CLIENT, system.getClient());

properties.setProperty(DestinationDataProvider.JCO_PASSWD, system.getPassword());

properties.setProperty(DestinationDataProvider.JCO_USER, system.getUser());

properties.setProperty(DestinationDataProvider.JCO_LANG, system.getLanguage());

MyDestinationDataProvider myProvider = new MyDestinationDataProvider();

myProvider.changePropertiesForABAP_AS(properties);

com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider);

try{

dest = JCoDestinationManager.getDestination("SAP_SERVER");

System.out.println("Attributes:");

System.out.println(dest.getAttributes());

System.out.println();

}catch(JCoException e){

throw new RuntimeException(e);

}

}

public JCoFunction getFunction(String functionStr){

JCoFunction function = null;

try{

function = repos.getFunction(functionStr);

}catch (Exception e){

e.printStackTrace();

throw new RuntimeException("Problem retrieving JCO.Function object.");

}

if (function == null){

throw new RuntimeException("Not possible to receive function.");

}

return function;

}

public void execute(JCoFunction function){

try{

JCoContext.begin(dest);

function.execute(dest);

JCoContext.end(dest);

}catch (JCoException e){

e.printStackTrace();

}

}

}

java.lang.NullPointerException

at com.mk.demo.jco.Connection.getFunction(Connection.java:69)

at com.mk.demo.jco.JcoTester.main(JcoTester.java:33)

Exception in thread "main" java.lang.RuntimeException: Problem retrieving JCO.Function object.

at com.mk.demo.jco.Connection.getFunction(Connection.java:72)

at com.mk.demo.jco.JcoTester.main(JcoTester.java:33)

Edited by: MonkD on Jun 9, 2009 3:20 PM

Edited by: MonkD on Jun 9, 2009 3:21 PM

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

I tried a couple more BAPI's and i get the same NULLPOINTEREXCEPTION, look like i can't call any BAPI's....

All of this doesn't make sense, the code looks good to me even though i'm no expert in java.

Thanks for your help.


public class JcoTester {
	
	static String SAP = "SAP_SERVER";
	
	public static void main(String[] args) {
		//SAP System
		SapSystem system = new SapSystem();
		system.setClient("220");
		system.setHost("myserver");
		system.setLanguage("en");
		system.setSysID("00");
		system.setUser("myID");
		system.setPassword("myPWD");
		
		Connection connect = new Connection(system);
		
		JCoFunction function = connect.getFunction("BAPI_SALESORDER_GETLIST");
		function.getImportParameterList().setValue("CUSTOMER_NUMBER", 1326181);
		function.getImportParameterList().setValue("SALES_ORGANIZATION", "FR30");
		connect.execute(function);
		JCoTable table = function.getTableParameterList().getTable("SALES_ORDERS");
		System.out.println(table.isEmpty());
		
		TableAdapterReader tableAdapter = new TableAdapterReader(table);
		System.out.println("Number of orders: " + tableAdapter.size());
		for (int i = 0; i < tableAdapter.size(); i++){
			System.out.println(tableAdapter.get("SD_DOC"));
			tableAdapter.next();
		}
		
		
	}
}

Edited by: MonkD on Jun 9, 2009 4:16 PM

Edited by: MonkD on Jun 9, 2009 4:18 PM

Edited by: MonkD on Jun 9, 2009 4:19 PM

Former Member
0 Kudos

I turned Jco trace on and i found that the exception was FU_NOT_FOUND. Althought i can see and test the BAPI_USER_GETDETAIL in se37. It is not found when calling it via Jco.

I wrote a test program that makes use of the FUNCTION_EXISTS FM and i tested the BAPI and it did not find it. I don't understand why because testing the FM via se37 seems to find the BAPI....

The code above for BAPI_SALESORDER_GETLIST works when i added quotes to the customer#, i was also able to get BAPI_COMPANYCODE_GETLIST to work as well. So it seems that the issue is with that bapi user function.

Below is the test program, all methods work except for the getUserDetail which makes use of the BAPI_USER_GETDETAIL. I want to know how i can use the methods within the USER business object via JCo 3.0; if anyone knows how please do share your wisdom.

The main class is below, the SimpleClient class is in post after this one.


package mypackage;

import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.*;

public class SimpleTester {

	public static void main(String[] args) {
	
	int traceLevel = 8;
	JCo.setTrace(traceLevel, "stdout");
	SimpleClient client = new SimpleClient();
	try{
		client.simpleConnect();
		client.stfcConnection();
		client.functionExists();
                                client.getSalesOrders();
                                client.getCompanyCodes();
                                client.getUserDetail();          //DOES NOT WORK
	}catch (JCoException e){
		throw new RuntimeException("Error", e);
	}
	}
}


Former Member
0 Kudos

package mypackage;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;

import com.sap.conn.jco.AbapException;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;
import com.sap.conn.jco.ext.DestinationDataProvider;

public class SimpleClient {
               static String DEST1 = "ABAP_AS_WITHOUT_POOL";
	static String DEST2 = "ABAP_AS_WITH_POOL";

	static {
		Properties properties = new Properties();

		properties.setProperty(
			DestinationDataProvider.JCO_ASHOST,
			"myServer");
		properties.setProperty(DestinationDataProvider.JCO_SYSNR, "XX");
		properties.setProperty(DestinationDataProvider.JCO_CLIENT, "XX");
		properties.setProperty(DestinationDataProvider.JCO_PASSWD, "myPWD");
		properties.setProperty(DestinationDataProvider.JCO_USER, "myID");
		properties.setProperty(DestinationDataProvider.JCO_LANG, "EN");

		createDestinationDataFile(DEST1, properties);

		properties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");
		properties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10");

		createDestinationDataFile(DEST2, properties);

	}

	static void createDestinationDataFile(
		String destinationName,
		Properties properties) {
		File destCfg = new File(destinationName + ".jcoDestination");
		try {
			FileOutputStream fos = new FileOutputStream(destCfg, false);
			properties.store(fos, "for tests only!");
			fos.close();
		} catch (Exception e) {
			throw new RuntimeException(
				"Unable to create the destination file",
				e);
		}
	}
Former Member
0 Kudos

i have to break the class in several posts so that the formatting is not lost with the code markup...


public void simpleConnect() throws JCoException {
		JCoDestination destination =
			JCoDestinationManager.getDestination(DEST1);
		System.out.println("Attributes:");
		System.out.println(destination.getAttributes());
		System.out.println();
	}

	public void stfcConnection() throws JCoException {
		JCoDestination destination =
			JCoDestinationManager.getDestination(DEST1);
		JCoFunction function =
			destination.getRepository().getFunction("STFC_CONNECTION");
		if (function == null)
			throw new RuntimeException("STFC_CONNECTION not found");
		function.getImportParameterList().setValue("REQUTEXT", "HELLO");

		try {
			function.execute(destination);
		} catch (AbapException e) {
			System.out.println(e.toString());
			return;
		}

		System.out.println("STFC_CONNECTION FINISHED:");
		System.out.println(
			"echo: " + function.getExportParameterList().getString("ECHOTEXT"));
		System.out.println(
			"Response: "
				+ function.getExportParameterList().getString("RESPTEXT"));
		System.out.println();
	}

	public void getUserDetail() throws JCoException {
		JCoDestination destination =
			JCoDestinationManager.getDestination(DEST1);
		JCoFunction function =
			destination.getRepository().getFunction("BAPI_GET_USER_DETAIL");
		if (function == null)
			throw new RuntimeException("BAPI_GET_USER_DETAIL not found");
		function.getImportParameterList().setValue("USERNAME", "myID");

		try {
			function.execute(destination);
			JCoTable table =
				function.getTableParameterList().getTable("PARAMETER");
			for (int i = 0; i < table.getNumRows(); i++) {
				table.setRow(i);
				System.out.println(
					table.getString("PARID") + '\t' + table.getString("PARVA"));
			}

		} catch (JCoException e) {
			throw new RuntimeException(
				"Error executing BAPI_GET_USER_DETAIL",
				e);
		}
	}

Former Member
0 Kudos

public void getCompanyCodes() throws JCoException {
		JCoDestination destination =
			JCoDestinationManager.getDestination(DEST1);
		JCoFunction function =
			destination.getRepository().getFunction("BAPI_COMPANYCODE_GETLIST");
		if (function == null)
			throw new RuntimeException("BAPI_COMPANYCODE_GETLIST not found");
		try {
			function.execute(destination);
		} catch (AbapException e) {
			System.out.println(e.toString());
			return;
		}
		JCoStructure returnStructure =
			function.getExportParameterList().getStructure("RETURN");
		if (!returnStructure.getString("TYPE").equals("")
			|| returnStructure.getString("TYPE").equals("S")) {
			throw new RuntimeException(returnStructure.getString("MESSAGE"));
		}
		JCoTable codes =
			function.getTableParameterList().getTable("COMPANYCODE_LIST");
		for (int i = 0; i < codes.getNumRows(); i++) {
			codes.setRow(i);
			System.out.println(
				codes.getString("COMP_CODE")
					+ '\t'
					+ codes.getString("COMP_NAME"));
		}
	}

	public void functionExists() throws JCoException {
		JCoDestination destination =
			JCoDestinationManager.getDestination(DEST1);
		JCoFunction function =
			destination.getRepository().getFunction("FUNCTION_EXISTS");
		if (function == null)
			throw new RuntimeException("FUCNTION_EXISTS not found");
		function.getImportParameterList().setValue(
			"FUNCNAME",
			"BAPI_SALESORDER_GETLIST");

		try {
			function.execute(destination);
		} catch (AbapException e) {
			throw new RuntimeException("error executing FUNCTION_EXISTS", e);
		}
		function.getExportParameterList().getString("GROUP");
	}
Former Member
0 Kudos

	public void getSalesOrders() throws JCoException {
			JCoDestination destination =
				JCoDestinationManager.getDestination(DEST1);
			JCoFunction function =
				destination.getRepository().getFunction("BAPI_SALESORDER_GETLIST");
			if (function == null)
				throw new RuntimeException("BAPI_SALESORDER_GETLIST not found");
			function.getImportParameterList().setValue("CUSTOMER_NUMBER", "1326181");
			function.getImportParameterList().setValue("SALES_ORGANIZATION", "FR30");

			try {
				function.execute(destination);
				JCoTable table =
					function.getTableParameterList().getTable("SALES_ORDERS");
				for (int i = 0; i < table.getNumRows(); i++) {
					table.setRow(i);
					System.out.println(
						table.getString("SD_DOC"));
				}

			} catch (JCoException e) {
				throw new RuntimeException(
					"Error executing BAPI_SALESORDER_GETLIST",
					e);
			}
		}
}