You need to sign in to do that
Don't have an account?
Apex: Wizard Problem, successful insert yields "Null reference" error
Hello,
I'm ripping my hair out over this wizard controller. I made this off of the Cookbook's Opportunity controller. Basically, if I comment this out, the participant assessment is created/inserted successfully.
//participantanswer.Participant_Assessment__c = participantassessment.Id; //participantanswer.Possible_Answer__c = possibleanswer1; //insert participantanswer;
Without that block commented, my controller looks like this, and I cannot seem to pull the newly created Id of the participantassessment which was successfully created in the previous block.
public class osmController { // These member variables maintain the state of the wizard. // When users enter data into the wizard, their input is stored // in these variables. Participant_Assessment__c participantassessment; Participant_Answer__c participantanswer; private final Participant__c participant; private final Assessment__c assessment; string possibleanswer1 = null; // The next methods return one of each of the member // variables. If this is the first time the method is called, // it creates an empty record for the variable. public osmController() { participant = [select id, name from Participant__c where id = :ApexPages.currentPage().getParameters().get('id')]; assessment = [select id, name from Assessment__c where Name = :ApexPages.currentPage().getParameters().get('asmt') AND Version__c = :ApexPages.currentPage().getParameters().get('version')]; } public List<selectOption> getanswer1() { List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options //options.add(new selectOption('', '--None--')); //add the first option of '- None -' in case the user doesn't want to select a value or in case no values are returned from query below for (Possible_Answer__c pa1 : [SELECT Id, Answer_Display__c FROM Possible_Answer__c WHERE Dimension__c = 'Income']) { options.add(new selectOption(pa1.Id, pa1.Id)); //for all records found - add them to the picklist options } return options; //return the picklist options } public Participant__c getParticipant() { return participant; } public Assessment__c getAssessment() { return assessment; } public Participant_Assessment__c getParticipantAssessment() { if(participantassessment == null) participantassessment = new Participant_Assessment__c(); return participantassessment; } public Participant_Answer__c getParticipantAnswer() { if(participantanswer == null) participantanswer = new Participant_Answer__c(); return participantanswer; } public String getpossibleanswer1() { return possibleanswer1; } public void setpossibleanswer1(String possibleanswer1) { this.possibleanswer1 = possibleanswer1; } // The next methods control navigation through // the wizard. Each returns a PageReference for one of the pages // in the wizard. Note that the redirect attribute does not need to // be set on the PageReference because the URL does not need to change // when users move from page to page. public PageReference step1() { return Page.osmStep1; } public PageReference step2() { return Page.osmStep2; } public PageReference step3() { return Page.osmStep3; } // This method cancels the wizard, and returns the user to the // Participants tab public PageReference cancel() { PageReference participantPage = new ApexPages.StandardController(participant).view(); participantPage.setRedirect(true); return participantPage; } // This method performs the final save for all objects, and // then navigates the user to the detail page for the new // participant assessment. public PageReference save() { // Create the participant assessment. Before inserting, use the id field // from the active participant and assessment that are passed via custom button to create // the relationship between the participant and assessment and the participant assessment. participantassessment.Participant__c = participant.Id; participantassessment.Assessment__c = assessment.Id; insert participantassessment; // Create the participant answer. Before inserting, use the id field // that's created once the participant assessment is inserted to create // the relationship between the participant assessment and the participant answer. participantanswer.Participant_Assessment__c = participantassessment.Id; //participantanswer.Possible_Answer__c = possibleanswer1; insert participantanswer; // Finally, send the user to the detail page for // the new participant assessment. PageReference participantassessmentPage = new ApexPages.StandardController(participantassessment).view(); participantassessmentPage.setRedirect(true); return participantassessmentPage; } }
Any tips on how to go about fixing/debugging this would be greatly appreciated.
The exact error I get is:
System.NullPointerException: Attempt to de-reference a null object
Class.osmController.save: line 109, column 1
Line 109 is
participantanswer.Participant_Assessment__c = participantassessment.Id
I think the problem is the participantanswer value is null, not an empty ParticipantAnswer__c.
Try:
Thanks for the feedback. I tried your suggestion and am still getting the same error. I think this:
covers null answers either way, no?