function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Molson94Molson94 

Doh! | Attempt to de-reference a null object

Hey Everyone,

Banging my head against the wall as i cannot figure out why this exception is being thrown.
I am getting the following error:

Attempt to de-reference a null object
Error is in expression '{!LookupTitleID}' in component <apex:commandButton> in page espcreate: Class.ESPController1.LookupTitleID: line 80, column 1

I am querying Vendor based on param V at the beginning when the page loads, so Vendor should never be null when "LookupTitleID" fires.

Any thoughts?
Thanks!

Here is the code:
Line 80 is: newEsp.Vendor__c = Vendor.get(0).id;
 
public class ESPController1 {

     //url parameters
    public String v = ApexPages.currentPage().getParameters().get('v');
    public String esp = ApexPages.currentPage().getParameters().get('esp');
    
    //lists
    public List<Vendor__c> Vendor = new list<vendor__c>([SELECT id FROM Vendor__c WHERE id = :v LIMIT 1]);
    public List<Title__c> Title = new List<Title__c> ();
    public List<ESP__c> ESPList = new List<ESP__C> ([SELECT id FROM ESP__c WHERE id = :esp LIMIT 1]);
    
    //sObjects
    public ESP__c newEsp;
    
    //Create Inputs
    public String TitleID { get; set; }
    public string martyid;
    
    //Key Page References
    public PageReference booter = new PageReference('http://www.google.com'); 
    
    /////Footer Links/////
    public String getV() {
        return v;
    }
    //////////////////////
    
    /////Nav Page Refs/////
    
    public PageReference goCreate() {
        PageReference goCreate = Page.espcreate;
        goCreate.getParameters().put('v',v);
        goCreate.setRedirect(true);
        return goCreate;
    }
    
    public PageReference goDash() {
        PageReference goDash = Page.espdashboard;
        goDash.getParameters().put('v',v);
        goDash.setRedirect(true);
        return goDash;
    }
    
    ////////////////////////
    
    public PageReference LookupVendor() {//function to check the url contains the correct vendor id, set as action for all pages to enable a check when the page is loaded!!!
        if (String.isEmpty(v)) {
            booter.setRedirect(true);
            return booter;
            
        }
        
        if (vendor.size() != 1) {
            booter.setRedirect(true);
            return booter;
            
        }
        return null;
    }
    
    
    
    /////ESP Create Page///// 
    
    public PageReference LookupTitleID(){ //check that the supplied title ID works, if it does, create a new esp, and move to next step
        
        if (String.isEmpty(TitleID) || !TitleId.isNumeric()){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Title ID is a required input. Please enter a valid Title ID.'));
            return null;
        }
        
        if(TitleId!=null){
            Title = [SELECT Id, Marty_Title_Id__c FROM Title__c WHERE Marty_Title_ID__c = :TitleId LIMIT 1];
            if (Title.size() == 1) { //valid title id, create the esp, insert, and move to the full form. 
                newEsp.Vendor__c = Vendor.get(0).id;
                newEsp.Title__c = Title.get(0).id;
                
                try {
                    insert(newEsp);
                } catch(DmlException e) {
                    System.debug('The following exception has occurred: ' + e.getMessage());
                }
                
                PageReference redirect = Page.esp;
                redirect.getParameters().put('v',vendor.get(0).Id);
                redirect.getParameters().put('esp',newEsp.id);
                redirect.setRedirect(true);
                return redirect;
            }   
        }else{
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Not a valid Title ID. Please enter a valid Title ID.'));
            return null;
        }
        return null;
    }
}



 
Best Answer chosen by Molson94
Andy BoettcherAndy Boettcher
Your "Vendor" list has a zero-length, so it's not able to pull index 0 from it.  Without a rewrite of your class - you can add another if/else in there to check the size of that list.

All Answers

Andy BoettcherAndy Boettcher
Your "Vendor" list has a zero-length, so it's not able to pull index 0 from it.  Without a rewrite of your class - you can add another if/else in there to check the size of that list.
This was selected as the best answer
Molson94Molson94
Thanks Andy!
In my effort to simplify my queries i forgot to pull in another column off the table. i added name and now it works just fine.

When you mention a re-write, are you seeing some other inefficiencies that would be worth fixing? Im all for hearing best practices.

Thanks!
Andy BoettcherAndy Boettcher
How are you calling this class?
Molson94Molson94
Being called from a visualforce page. when the page loads it checks for certain url parameters. The first page asks for user for an id and then moves to the next piece of the form.
Andy BoettcherAndy Boettcher
You don't seem to have a constructor in your page - are you calling this class via <apex:page extension> or <apex:page controller>?
Molson94Molson94
<apex:page controller>, with action={!Lookupvendor}
Andy BoettcherAndy Boettcher
So by no means am I attacking what you did *smile* - as I'm always a great proponent of "if it works, don't fix it", but in the spirit of readability / reusability, here is a stab at the class:  (keep in mind I don't have these objects, so I haven't compile-validated the code)
 
public class ESPController1 {
        
    //Key Page References
    public PageReference booter = new PageReference('http://www.google.com'); 
    
    ///////////////////////////
    // Constructors
    ///////////////////////////
    public ESPController1() {

        // Set URL Parameters
        v = ApexPages.currentPage().getParameters().get('v');
        esp = ApexPages.currentPage().getParameters().get('esp');
    }

    ///////////////////////////
    // GET/SET
    ///////////////////////////

    /******************
        I'm always a proponent of using specific and predictable GET/SETs instead of statically declaring
        this stuff at the top of the class.  Makes this class easier to re-use and improves readibility IMHO.
    ******************/

    public String TitleID {get; set;}
    public string martyid {get; set;}

    public ESP__C newEsp {
        get {
            if(newEsp == null) {
                return new ESP__c();
            }
            return newEsp;
        }
        set { newEsp = value; }
    }

    public String v {
        get { 
            if(v == null) {
                return null;
            }
            return v;
        set { v = value; }
    }

    public String esp {
        get {
            if(esp == null) {
                return null;
            }
            return esp; 
        }
        set { esp = value; }
    }

    public List<Vendor__c> Vendor {
        get {
            if(v == null) {
                return new List<Vendor__c>();
            }
            Vendor = new list<vendor__c>([SELECT id FROM Vendor__c WHERE id = :v LIMIT 1]);
            return Vendor;
        }
        set { Vendor = value; }
    }

    public List<Title__c> Title {
        get {
            if(Title == null) {
                return new List<Title__c>();
            }
            return Title;
        }
        set { Title = value; }
    }

    public List<ESP__c> ESPList {
        get {
            if(esp == null) {
                return new List<ESP__c>();
            }
            ESPList = new list<ESP__c>([SELECT id FROM ESP__c WHERE id = :esp LIMIT 1]);
            return ESPList;
        }
        set { ESPList = value; }
    }


    ///////////////////////////
    // Action Methods
    ///////////////////////////
    
    public PageReference goCreate() {
        PageReference goCreate = Page.espcreate; // GREAT USE OF DYNAMIC PAGE REFERENCE!
        goCreate.getParameters().put('v',v);
        goCreate.setRedirect(true);
        return goCreate;
    }
    
    public PageReference goDash() {
        PageReference goDash = Page.espdashboard; // GREAT USE OF DYNAMIC PAGE REFERENCE!
        goDash.getParameters().put('v',v);
        goDash.setRedirect(true);
        return goDash;
    }
    
    public PageReference LookupVendor() {//function to check the url contains the correct vendor id, set as action for all pages to enable a check when the page is loaded!!!
        
        // Combined both ifs here into one...
        if (String.isEmpty(v) || vendor.size() != 1) {
            booter.setRedirect(true);
            return booter;
        }

        // All Else Fails...
        return null;
    }
    
    public PageReference LookupTitleID(){ //check that the supplied title ID works, if it does, create a new esp, and move to next step
        
        if (String.isEmpty(TitleID) || !TitleId.isNumeric()){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Title ID is a required input. Please enter a valid Title ID.'));
            return null;
        }
        
        if(TitleId!=null){
            Title = [SELECT Id, Marty_Title_Id__c FROM Title__c WHERE Marty_Title_ID__c = :TitleId LIMIT 1];
            if (Title.size() == 1) { //valid title id, create the esp, insert, and move to the full form. 

                newEsp.Vendor__c = Vendor.get(0).id;
                newEsp.Title__c = Title.get(0).id;
                
                try {
                    insert(newEsp);
                } catch(DmlException e) {
                    System.debug('The following exception has occurred: ' + e.getMessage());
                }
                
                PageReference redirect = Page.esp;
                redirect.getParameters().put('v',vendor.get(0).Id);
                redirect.getParameters().put('esp',newEsp.id);
                redirect.setRedirect(true);
                return redirect;
            }   
        }else{
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Not a valid Title ID. Please enter a valid Title ID.'));
            return null;
        }
        return null;
    }
}

 
Molson94Molson94
Hey andy,

Sorry for the late reply. I like how you structured that class and made note of it for future projects. Certainly reads a lot better.
For this use case though in order to get the action methods to fire correctly i need to put the parameter statements outside of the constructor. Are you able to comment on why you put them inside the constructor and what the best practices are for using the constructor. Ive typically stayed away from it in most cases, but that could very well be because of inexperience.

Thanks!