You need to sign in to do that
Don't have an account?
Molson94
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;
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; } }
All Answers
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!
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!