+ Start a Discussion
kumarkkumark 

Error while clicking custom button:System.NullPointerException: Attempt to dereference a null object

1.Create quote button from opportunity page (detail page) is giving the below error when clicked.
  This custom button is calling URL /apex/ManageQuote/ apex code but its giving error

Error
--System.NullPointerException: Attempt to dereference a null object line 31 column 1 which is under public PageReference createQuote() of ManageQuoteController code.

 

can let us know what is the issue?Thanks in advance for help

 

 


Code snippet:
---ManageQuoteController code
:
public with sharing class ManageQuoteController {
    //Opportunity Reference
    public Opportunity opportunityRef {get; set;}
    //Quote Creation Error
    public Boolean quoteCreationError {get; set;}
    //Quote created indicator
    public Boolean quoteCreated {get; set;}
    //Error Message
    public String errorMessage {get; set;}
    //Quote URL
    public String quoteUrl {get; set;}
    //Default Controller
    public ManageQuoteController() {
        quoteCreated = false;
        quoteCreationError = false;
        Id opptyId = ApexPages.currentPage().getParameters().get('opportunity.id');
        try{
        opportunityRef = [select Id, Name from Opportunity where Id = :opptyId];
        }
        catch (System.QueryException e) {
          System.debug('caught ya!');
        }
    }
   
    public PageReference goback() {
        return new Apexpages.Standardcontroller(opportunityRef).view();
    }
   
    public PageReference createQuote() {
        Opportunity oppty = [select Id, Name, StageName, CloseDate, Owner.Name, AccountId, Account.Name,
            Account.Sic, Account.Industry, Account.BillingStreet, Account.BillingCity, Account.BillingState,
            Account.BillingPostalCode, Account.BillingCountry from Opportunity where Id = :opportunityRef.Id];
        OpportunityContactRole ocr = [select Contact.FirstName, Contact.LastName, Contact.EMail, Contact.Phone, Contact.Fax
            from OpportunityContactRole where opportunityId = :opportunityRef.Id and IsPrimary = true];
        QuoteResponse response = QuoteServiceDelegate.createQuote(oppty, ocr.Contact);
        if (response.stausCode != 200) {
            quoteCreationError = true;
            errorMessage = response.message;
        } else {
            quoteCreated = true;
            quoteUrl = response.quoteUrl;
        }
        return null;
    }
}



---Manage Quote code

<apex:page name="ManageQuote" controller="ManageQuoteController" id="thisPage" tabStyle="Opportunity">
        <script type="text/javascript">
                function init() {
                        if ('{!quoteCreationError}' == 'false' && '{!quoteCreated}' == 'false')
                                createQuote();
                }
                window.onload = init;
    </script>
        <apex:form >
                <apex:actionFunction name="createQuote" action="{!createQuote}"  rerender="outputBlock, quoteBlock" status="createStatus" />
                <apex:sectionHeader title="Manage Quote" subtitle="{!opportunityRef.Name}"/>
                <apex:outputPanel id="outputBlock">
                        <apex:actionStatus id="createStatus">
                                <apex:facet name="start">
                                        <apex:pageBlock >
                                                <apex:outputText >Creating quote. Please wait...</apex:outputText>
                                        </apex:pageBlock>
                                </apex:facet>
                        </apex:actionStatus>
                </apex:outputPanel>
                <apex:outputPanel id="quoteBlock">
                        <apex:pageBlock id="errorBlock" title="Error" rendered="{!quoteCreationError}">
                                <apex:pageBlockButtons location="bottom">
                                        <apex:commandButton value="Go Back" action="{!goback}" />
                                </apex:pageBlockButtons>
                                <apex:pageBlockSection columns="1">
                                        <apex:outputText value="{!errorMessage}"/>
                                </apex:pageBlockSection>
                        </apex:pageBlock>
                        <apex:iframe src="{!quoteUrl}" scrolling="true" id="quoteFrame" rendered="{!quoteCreated}"/>
                </apex:outputPanel>
        </apex:form>
</apex:page>

Best Answer chosen by Admin (Salesforce Developers) 
jd123jd123

/apex/ManageQuote?opportunityid={!Opportunity.Id}

 

  Id opptyId = ApexPages.currentPage().getParameters().get('opportunityid');

 

try with this code in button and class.Just remove the opportunityi.d' and replace with opportunityid' i have face the problem i think it will resolve otherwise please let me know

All Answers

mast0rmast0r

Im not sure that ths is ok: ApexPages.currentPage().getParameters().get('opportunity.id'); Have you checked that ID?

kumarkkumark

This is the exact error.Can you tell what i s wrong in the code or missing

 

Visualforce Error


System.NullPointerException: Attempt to de-reference a null object

Error is in expression '{!createQuote}' in component <apex:page> in page managequote


Class.ManageQuoteController.createQuote: line 33, column 1

jd123jd123

HI 

Can you post your button url then i can help out you.

kumarkkumark

here is the link of custom button /apex/ManageQuote?opportunity.id={!Opportunity.Id}

jd123jd123

/apex/ManageQuote?opportunityid={!Opportunity.Id}

 

  Id opptyId = ApexPages.currentPage().getParameters().get('opportunityid');

 

try with this code in button and class.Just remove the opportunityi.d' and replace with opportunityid' i have face the problem i think it will resolve otherwise please let me know

This was selected as the best answer
kumarkkumark

First thanks for the update and quick response.

 

1.

/apex/ManageQuote?opportunityid={!Opportunity.Id}

 

  Id opptyId = ApexPages.currentPage().getParameters().get('opportunityid');

 

try with this code in button and class.Just remove the opportunityi.d' and replace with opportunityid' i have face the problem i think it will resolve otherwise please let me know

 

Does this mean

 

1./apex/ManageQuote?opportunity.id={!Opportunity.Id} this needs to be changed in custom button url source as ?

     Id opptyId = ApexPages.currentPage().getParameters().get('opportunityid');

 

2.did not get this part?

Just remove the opportunityi.d' and replace with opportunityid' i have face the problem i think it will resolve otherwise please let me know

 

 

can you explain a bit more

jd123jd123

Hi Kumar,

 

I tried with your code it is working fine. Today I spent some time Just I copied your code and paste in my environment it worlking. Have you seen in the URL the Opportunity id is coming or not after clicking the button.

 

try in  Developer console I think no need.  I can give my access you can check it out o.w I can check out. Let me know which one is fine for you.

 

Thanks.

kumarkkumark

Yes opportunity ID is there as below in the url when button is clicked but then it gives error

 

/apex/ManageQuote?opportunity.id=006P0000004SPnC

 

System.NullPointerException: Attempt to de-reference a null object

Error is in expression '{!createQuote}' in component <apex:page> in page managequote

 

So can you help wht needs to be changed to make it error free

 

Class.ManageQuoteController.createQuote: line 33, column 1

jd123jd123

I think this line your getting error

 

 QuoteResponse response = QuoteServiceDelegate.createQuote(oppty, ocr.Contact);

 

But in the Query 

 

  OpportunityContactRole ocr = [select Contact.FirstName, Contact.LastName, Contact.EMail, Contact.Phone, Contact.Fax
            from OpportunityContactRole where opportunityId = :opportunityRef.Id and IsPrimary = true];

 

in the query you are not retrieving Contact  what you want to pass Contact or Contact.FirstName

kumarkkumark

Thanks creating contact role under opportunity resolved that error

 

now it is giving this new error

 

This is the url generated:/apex/ManageQuote?opportunity.id=006P0000004SPnC

 

Manage Quote Test
Error 
Attempt to de-reference a null object