1

I'm getting an error on a class I've written

"System.NullPointerException: Attempt to de-reference a null object Class.Salestreegetcontacts.execute: line 67, column 1"

I'm trying to send an auth key save on User, can anyone tell me where I'm messing up?

global class  Salestreegetcontacts implements schedulable{
global User STUser{get;set;}
public Id STUserId{get;set;}
 public string cstUsername {get;set;}
 public string cstPassword {get;set;}
 public string cstNode {get;set;}
 public string ClientAcctId {get;set;}
 public string xAuthToken{get;set;}
 public url st_Today{get;set;}
 public url st_Vault{get;set;}
 public string kind {get;set;}
 public string title {get;set;}
 public string description{get;set;}
 public date startdate{get;set;}
 public datetime startdatetime{get;set;}
 public url self{get;set;}
 public Contact__c contact;
 public map<string, string> eventmap;  
 public map<string, string> UserMap;   
 public map<string, string> contactmap;


global User getUserData(){

    If (STUser == null){
    User STUser = [select Id, X_Auth_token__c, CSTlastupdate__c, CST_Node__c, Client_Account_Id__c from User where X_Auth_token__c != null ];
    }
    return STUser;
    }
    global void Userdatamap(){
    if(STUser.X_Auth_Token__c != null){
map<string, string> Usermap = new map<string, string> ();
Usermap.put('Id', string.valueof(STUser.Id));
Usermap.put('xAuth', STUser.X_Auth_token__c);
Usermap.put('LastUpdate', string.valueof(STUser.CSTlastupdate__c));
Usermap.put('Node', STUser.CST_Node__c);
Usermap.put('Client_Account', STUser.Client_Account_Id__c);
}

}
public string xAuth(){
    if(xAuthToken != null){


    xAuthToken = STUser.X_Auth_token__c;
    cstNode = STUser.CST_Node__c;
    ClientAcctId = STUser.Client_Account_Id__c;
    STUserId = STUser.Id;
    }
    return xAuthToken;
    return cstNode;
    return ClientAcctId;
    return STUserId;
}


 global void execute(SchedulableContext sc){
        map<string, string> eventmap = new map<string, string>();


    HttpRequest req1 = new HttpRequest();
    HttpResponse res1 = new HttpResponse();
    Http http1 = new Http();

    req1.setMethod('POST' ); // Method Type
    req1.setEndpoint('https://web210.ntree.com/twig-cstc3/v1/node');
    req1.setheader('X-auth-token: ' , Usermap.get('xAuth'));
    string bodyrequest = '';
    Date Todaysdate = Date.today();
    String sMonth = String.valueof(Todaysdate.month());
    String sDay = String.valueof(Todaysdate.day());
    if(sMonth.length()==1){
        sMonth = '0' + sMonth;
        }
    if(sDay.length()==1){
      sDay = '0' + sDay;
      }
      String sToday = String.valueof(Todaysdate.year()) + sMonth + sDay ;
    bodyrequest += '/' + cstNode;
    bodyrequest += '/calendar?from=' + sToday;
    bodyrequest += '&to=' + sToday;

    req1.setbody(bodyrequest);


        res1 = http1.send(req1);
        if(res1.getBody() != null){

Any idea what I'm doing wrong?

2
  • 2
    One thing is that map<string, string> Usermap = new map<string, string> (); is assigning to a local variable rather than the field of the class. Removing that type at the start map<string, string> will avoid that.
    – Keith C
    Aug 24, 2015 at 13:09
  • There are 4 returns in a method return xAuthToken; return cstNode; return ClientAcctId; return STUserId; Which one you get?
    – Ashwani
    Aug 24, 2015 at 13:12

2 Answers 2

1

In this block here:

if(STUser.X_Auth_Token__c != null){
  map<string, string> Usermap = new map<string, string> ();
  Usermap.put('Id', string.valueof(STUser.Id));
  Usermap.put('xAuth', STUser.X_Auth_token__c);
  Usermap.put('LastUpdate', string.valueof(STUser.CSTlastupdate__c));
  Usermap.put('Node', STUser.CST_Node__c);
  Usermap.put('Client_Account', STUser.Client_Account_Id__c);
}  

you are conditionally setting the map when STUser.X_Auth_Token__c is not null. However, you are accessing the map without condition on line 67. To prevent the dreaded "NullPointerException", you can first check if the map contains the key.
if (Usermap.containsKey('xAuth'))... or use the same check you had above if(STUser.X_Auth_Token__c != null){

0

Try initiating User STUser = new User(); before using it. It might help.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .