0

I'm experimenting with the code below from another post on stackexchange:

All I'm trying to do is display a VF box in Opportunities to instruct Users on what to do at each stage. It uses a custom object, controller and a VF page. The custom object holds 2 fields, one for the stage and another for the instructions. I have made a few changes, but nothing major. For some reason, when I test the VF page I get the following error:

Content cannot be displayed: Attempt to de-reference a null object

I have created new records in the custom object that match the stage names in the opportunity object.

Can anyone help?

Controller Code:

public class OppInstructionsController {

    public Opportunity opp                                              {get;set;}
    public list<Opportunity_Helper__c> myRules                          {get;set;}
    public map<string,string> stage2Instructions                        {get;set;}
    public string StageInstructions                                     {get;set;}      

    public OppInstructionsController(ApexPages.StandardController controller){
        this.opp= (Opportunity)controller.getRecord();
        myRules = [Select Id, StageName__c, Instructions__c From Opportunity_Helper__c]; 
        for(Opportunity_Helper__c rule : myRules){
            stage2Instructions.put(rule.StageName__c, rule.Instructions__c);
        }

        StageInstructions = stage2Instructions.get(this.opp.StageName);                 
    }       
}

VF Page:

<apex:page StandardController="Opportunity" extensions="OppInstructionsController">
<apex:outputText value="{!StageInstructions}"/>
</apex:page>
0

You have not initialized the Map stage2Instructions. You will not be able to add values to to it unless you initialize it and also before accessing a value from the map it is always essential to check if the key is present in the map. You can change your constructor code something like this:

//initialize your map
stage2Instructions = new Map<String, String>();
for(Opportunity_Helper__c rule : myRules){
    stage2Instructions.put(rule.StageName__c, rule.Instructions__c);
}
//check if the key exists
if(stage2Instructions.containsKey(this.opp.StageName)){
    StageInstructions = stage2Instructions.get(this.opp.StageName);
}else {
    StageInstructions = 'setSomeDefaultValue';
}

But in your case you can change your code to execute something like this, which also does pretty much what you're doing without having to use maps.

try{
    stageInstructions = [Select Id, StageName__c, Instructions__c From Opportunity_Helper__c Where StageName__c=:opp.stageName LIMIT 1].Instructions__c;
}
//in case the SOQl has no records returned it throws an exception. 
catch(exception e) {

    stageInstructions = 'setSomeDefaultValue';
}
  • Thanks for the response. Unfortunately, opp.stageName is null. When I debug opp, it only contains currency, record type and Id. Any idea how I can add stage in there? – Jay Jan 30 '15 at 10:40
  • I just replied to the comment in the below answer. Anyways pasting it back here.. Just use that field in the VF page and hide it. Only those fields referenced in the VF pages are available in the .getRecord() method. Check it out here salesforce.com/docs/developer/pages/Content/… – Sam Jan 30 '15 at 10:42
0

Try doing this- Your query might not returning any result OR Map is empty

Your Code line :

StageInstructions = stage2Instructions.get(this.opp.StageName);

Replace with following Code Snippet :

 if(stage2Instructions.size()>0){
       System.Debug('-----My Map is not Empty ---');
        StageInstructions = stage2Instructions.get(this.opp.StageName); 
    }
  • Hi Nilesh, thanks for the help. It looks like this.opp.stagename is not returning any results. When I debug this.opp, all I get is id, currency and record type. Do you know if it's possible to include stageName in those results? Or do I have to write a second query? It would be great if I could avoid firing 2 queries every time someone views an opportunity. – Jay Jan 30 '15 at 10:35
  • Just use that field in the VF page and hide it. Only those fields referenced in the VF pages are available in the .getRecord() method. Check it out here salesforce.com/us/developer/docs/pages/Content/… – Sam Jan 30 '15 at 10:41

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.