Skip to Content
avatar image
Former Member

Calling BAPI via Jco 3.0

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • avatar image
    Former Member
    Jun 09, 2009 at 02:13 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member
      	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);
      			}
      		}
      }