Skip to main content
Published: August 04 2006, 12:17:00 PMUpdated: November 28 2020, 2:20:06 PM

Why am I not getting any API rules from JAVA SDK GetApiAccessRulesCall ? I am getting java.lang.NullPointerException with the code snippet below:

GetApiAccessRulesCall accessRules = new GetApiAccessRulesCall(api.getApiContext());
ApiAccessRuleType[] rules = accessRules.getReturnedApiAccessRules();
for (int i=0; i<rules.length; i++) {

// Iterate over rules here
}

The getReturnedApiAccessRules() is just a getter that simply returns ApiAccessRulesType array object, which is initiated to null in GetApiAccessRulesCall. The ApiAccessRulesType array object should
be set by getApiAccessRules() method that executes the API call, and the method is not called in the code snippet above thus the NullPointer exception.

The GetApiAccessRules is a simple api call that does not require any properties as input parameters other than base type definition of the request payload. Here is how it can be done with the JAVA SDK.


1. AppGetApiAccessRules.java
============================

import com.ebay.soap.eBLBaseComponents.*;
import com.ebay.sdk.*;
import com.ebay.sdk.call.*;

public class AppGetApiAccessRules {
   private String configFile= "request_header_param.properties";
    private ApiContext apiContext;
    private boolean _isCore =true;
    private ApiCall _apiCall;

   /** Creates a new instance of AppGetApiAccessRules */
    public AppGetApiAccessRules() {
        RequestHeaderParam requestHeaderParam = new  RequestHeaderParam(_isCore);
        apiContext = requestHeaderParam.createContext(configFile);
        apiContext.setErrorLanguage("en_US");
        _apiCall  = new com.ebay.sdk.ApiCall(apiContext);
        //Enable logging
        ApiLogging logging = new ApiLogging();
        apiContext.setApiLogging(logging);

        //Retry
        CallRetry callretry = new CallRetry();
        callretry.setMaximumRetries(3);
        callretry.setDelayTime(1000);
        org.apache.axis.types.Token [] apiErrorCodes = new org.apache.axis.types.Token[] {
             new org.apache.axis.types.Token("502"),
        };
        callretry.setTriggerApiErrorCodes(apiErrorCodes);
        java.lang.Class[] tcs = new java.lang.Class[] {
              java.net.SocketTimeoutException.class,
              org.apache.axis.AxisFault.class
        };
        callretry.setTriggerExceptions(tcs);
        _apiCall.setCallRetry(callretry);

    } //END OF AppGetApiAccessRules class constructor

     public static void  main(String[] args){
        AppGetApiAccessRules agaar = new AppGetApiAccessRules();
        try {
            GetApiAccessRulesCall gaar = new GetApiAccessRulesCall(agaar.apiContext);
            // execute the api request
            gaar.getApiAccessRules();
            ApiAccessRuleType[] accessRulesArray = gaar.getReturnedApiAccessRules();

            for (int i=0; i<accessRulesArray.length; i++){
                ApiAccessRuleType accessRule = accessRulesArray[i];
                System.out.println( "call name: " + accessRule.getCallName());
                System.out.println( "dayly soft limit " +accessRule.getDailySoftLimit());
                System.out.println (" Daily usage " +accessRule.getDailyUsage().doubleValue());
                System.out.println(" Rule Current Status " +accessRule.getRuleCurrentStatus().toString());
             }
        }catch(Exception e){
            e.printStackTrace();
        }
    }// END OF main()

}//END OF AppGetApiAccessRules() class

2. RequestHeaderParam.java
==========================

import com.ebay.sdk.*;
import com.ebay.soap.eBLBaseComponents.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class RequestHeaderParam {

   private boolean _isCore = true;

    public RequestHeaderParam(boolean isCore){
         _isCore = isCore;
    }

    public ApiContext createContext(String file){

        //Load the properties file into a Properties object
       Properties config = new Properties();
       try {
  config.load(new FileInputStream(file));
 } catch (IOException e) {
   System.out.println("config.properties file is missing from the current working directory.");
   return null;
 }
        ApiContext context = new ApiContext();
        ApiCredential credential= context.getApiCredential();
        credential.seteBayToken(config.getProperty("token") );

        // add ApiCredential to ApiContext
        String siteList = config.getProperty("apiServer");
        context.setApiServerUrl(siteList);
        if (getIsCoreFlag()){
           context.setSite(SiteCodeType.US);
        }else{
           context.setSite(SiteCodeType.Germany);
        }
        return(context);
    }
    private boolean getIsCoreFlag(){
        return _isCore;
    }
}// END OF RequestHeaderParam() class


3. request_header_param.properties
==================================

# api production user properties file
apiServer=https://api.sandbox.ebay.com/wsapi
siteid=US
internationSiteId=Germany
version=471
devid=
appid=
cert=
token=
userid=
password=

How well did this answer your question?
Answers others found helpful