0

I have a custom button which I am trying to work with. But i keep on getting error " Attempt to de-reference a null object Error is in expression '{!updateDb}' in component in page routerprovisioningstatus: Class.ProvisioningController.updateDb: line 134, column 1"

Here is my class:enter code here

public with sharing class ProvisioningController {

public List<SelectOption> statusOptions { get; private set; }
public String defaultOption { get; set; }
public String deviceKind { get; private set; }
public String deviceId { get; private set; }
public Boolean success { get;  private set; }
public String error { get; private set; }
public Provisioneer.DeviceStatus device { get; private set; }
public Boolean activationFormIsVisible { get; private set; }
public String chosenServicePlan { get; set; }
public List<String> ProvisioningRequestIds { get; private set; }
public List<Provisioning_Request__c> ProvisioningRequests {
get {
  return [
    SELECT Id, Name, CreatedDate, Kind__c, Identifier__c, Command__c, Completed__c, Error__c 
    FROM Provisioning_Request__c 
    WHERE Name in :this.ProvisioningRequestIds
    ORDER BY CreatedDate DESC
  ]; 
}
}

public List<SelectOption> assignablePlans {
get {
  List<SelectOption> retval = new List<SelectOption>();

  if (this.device != null && this.device.account != null) {
    retval.add( new SelectOption('','--None--'));
    for(Service_plan__c onePlan: [select Account__c, Code__c from Service_plan__c where Account__c = :this.device.account]) {
      retval.add( new SelectOption(onePlan.Code__c, onePlan.Code__c));
    }
  }

  return retval;
}
 }

public String jbody = null;

private Provisioneer prov = null;
public MDN__c mdn;
public Router_custom_object__c router;
public Service_plan__c plan;

public ProvisioningController() {
statusOptions = new List<SelectOption>();
ProvisioningRequestIds = new List<String>();

Schema.DescribeFieldResult fieldResult = Router_custom_object__c.Status__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for(Schema.PicklistEntry p : ple)
  statusOptions.add(new SelectOption(p.getValue(), p.getValue()));        

defaultOption = 'Checked in';
activationFormIsVisible = false;

Map<String,String> params = ApexPages.currentPage().getParameters();
if (params.containsKey('mdn')) {
  deviceKind = 'mdn';
  deviceId = params.get('mdn');
} else if (params.containsKey('esn')) {
  deviceKind = 'esn';
  deviceId = params.get('esn');
} else if (params.containsKey('meid')) {
  deviceKind = 'meid';
  deviceId = params.get('meid');
} else if (params.containsKey('imei')) {
  deviceKind = 'imei';
  deviceId = params.get('imei');
}

this.prov = new Provisioneer();

if (String.isNotBlank(deviceKind) && String.isNotBlank(deviceId)) {
  Provisioneer.DeviceStatusResponse dsr = prov.getDeviceStatus(deviceKind, deviceId);
  this.success = dsr.success;
  this.error = dsr.error;
  this.device = dsr.result;
  this.jbody = dsr.rawJson;

  if (this.error != null)
    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR, this.error) );
} else {
  ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR, 'A valid ESN or MDN must be specified.') );
}

for(Provisioning_Request__c pr: [select Id, Name, Kind__c, Identifier__c from Provisioning_Request__c where Identifier__c = :this.deviceId]) {
  ProvisioningRequestIds.add(pr.Name);
 }
}

 public String getStatus() {
 return jbody;    
}

 public MDN__c getMdn() {
 if (this.mdn == null && this.device != null) {
  try {
    this.mdn = this.device.getMdn();
  } catch (Exception ex) {
    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.INFO, 'MDN does not exist in our database.') );        
  }
}

return this.mdn;
}


public Router_custom_object__c getRouter() {
if (this.router == null && this.device != null) {
  try {
    this.router = this.device.getRouter();
  } catch (Exception ex) {
    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.INFO, 'Router does not exist in our database.') );        
  }
}    
return this.router;
}

public Service_plan__c getPlan() {
if (this.plan == null && this.device != null) {
  try {
    this.plan = this.device.getPlan();
  } catch (Exception ex) {
    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.INFO, 'Service plan does not exist in our database.') );        
  }
}    

return this.plan;
}

public PageReference updateDb() {
**if (success && device != null) {**
  device.updateDb();
}

PageReference redir =  ApexPages.currentPage();
if (deviceKind != null)
  redir.getParameters().put(deviceKind, deviceId);
redir.setRedirect(true);
return redir;
}

public PageReference showActivationForm() {
this.activationFormIsVisible = true;
return ApexPages.currentPage();
}

public PageReference activateDevice() {
if (device != null) {
  try {
    this.ProvisioningRequestIds.add(
      prov.changeDevices( new List<Provisioneer.DeviceStatus>{device}, 'activate', chosenServicePlan )
    );
    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Device activation has been requested.') );
  } catch(Exception ex) {
    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage() ) );        
  }
}

return null;
}

public PageReference changeDevicePlan() {
if (device != null && String.isNotBlank(this.chosenServicePlan) ) {
  try {
    this.ProvisioningRequestIds.add(
      prov.changeDevices( new List<Provisioneer.DeviceStatus>{this.device}, 'setPlan', this.chosenServicePlan)
    );
    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Service plan change has been requested.') );
  } catch(Exception ex) {
    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage() ) );        
  }
} else
  ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.Error, 'You must select a rate plan.'));

return null;
}

public PageReference suspendDevice() {
if (device != null) {
  try {
    this.ProvisioningRequestIds.add(
      prov.changeDevices( new List<Provisioneer.DeviceStatus>{device}, 'suspend', null )
    );
    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Device suspension has been requested.') );
  } catch(Exception ex) {
    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage() ) );        
  }
}

return null;
 }  

public PageReference restoreDevice() {
if (device != null) {
  try {
    this.ProvisioningRequestIds.add(
      prov.changeDevices( new List<Provisioneer.DeviceStatus>{device}, 'resume', null )
    );
    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Device restoration has been requested.') );
  } catch(Exception ex) {
    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage() ) );        
  }
}

return null;
}

public PageReference terminateDevice() {
if (device != null) {
  try {
    this.ProvisioningRequestIds.add(
      prov.changeDevices( new List<Provisioneer.DeviceStatus>{device}, 'terminate', null )
    );
    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Device termination has been requested.') );
  } catch(Exception ex) {
    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage() ) );        
  }
}

return null;
 }

 // refresh the page, without doing anything...
  public PageReference refresh() {
   return null;
 }

Line 133: public PageReference updateDb() {

Line 134: if (success && device != null) {

  • 1
    Two things: (a) could you please point out where line 134 is? (b) Do you think you could delete some of the 700+ lines of code that are probably not relevant to your question? – sfdcfox Jun 6 '16 at 19:14
1

In Apex there is no defaulting of a boolean to false; booleans have to be assigned a value before they are referenced otherwise they are null.

I would suggest setting success = false; early in the constructor to make sure it is never null. (Presently its value is only set sometimes in the constructor.) That lets you sick to the natural looking usage later on:

if (success && device != null) {

Alternatively you can defend when success is referenced using the rather ugly constructs of:

if (success != null && success && device != null) {

or:

if (success == true && device != null) {
  • @Bookeraward2 No problem. I've made this mistake more than once... – Keith C Jun 6 '16 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.