+ Start a Discussion

NullPointerException: Attempt to de-reference a null object

I am trying to set create a new clone button only cloning certain fields on the opportunity:

I am recieving a de-reference a null object error.


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

Class.OpportunityCloneWithItemsController: line 32, column 81 External entry point 


Here is the apex class.


public with sharing class OpportunityCloneWithItemsController {
    public OpportunityCloneWithItemsController(ApexPages.StandardController controller) {


        // load the current record
      //  Opportunity o = (Opportunity)controller.getRecord();
    // method called from the VF's action attribute to clone the Opportunity
    public PageReference clone;{
         // setup the save point for rollback
         Savepoint sp = Database.setSavepoint();
         Opportunity o;
         Opportunity newO;

              //copy the Opportunity - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             o = [select  id, Name, Estimated_Cost__c, Margin__c, Competitor_Primary__c, 
             Competitor_Secondary__c, Account_Shipping_Location__c, Amount, INCO_Terms__c,
             Related_Location__c, Language_to_prepare_docs_in__c, Campaignid, Equipment__c,
             Parent__c, Equip_Services_Options_to_be_quoted__c, Lead_Time_Ex_Works__c, Op_Market_Position__c,
             Product_1__c, Product1_condition__c, Product1_Line_Capacity__c, Product1_Bulk_density__c,
             Product1_defect_description__c, Product_2__c, Product_2_Condition__c, Product_2_Line_Capacity__c,
             Product_2_Bulk_Density__c, Product_2_Defect_Description__c, Description, PS__c, Upgrade__c, Farmco__c,
             Service_Contract__c, Potato__c, Processed_Fruit_Veg__c, AIS__c, Project_System_Eng__c, Freshline__c,
             Pharma__c, Misc_Other__c, AccountId from Opportunity o Where id = :o.id ];
             newO = o.clone(false);
             insert newO;

             // copy over the line items - ONLY INCLUDE THE FIELDS YOU WANT TO CLONReturn must be called from a method or trigger bodyE
             List<OpportunityLineItem> items = new List<OpportunityLineItem>();
             for (OpportunityLineItem oli : [Select Id, ListPrice, Cost_Each__c, Quantity From OpportunityLineItem ol where OpportunityLineItem.id = :o.id]) {
                  OpportunityLineItem newoli = oli.clone(false);
                //  newO.id = newO.id;
             insert items;

   //     return new PageReference('/'+newO.id+'/e?retURL=%2F'+newO.id);




How do I solve this problem?

Thank you

Best Answer chosen by Admin (Salesforce Developers) 



The problem seems to be that whatever query that you are firing in the code is returning no rows.

and still without checking for null you are trying to "clone" the object, this is where the code is throwing the "Null pointer exception".


What you can do to check your query is to run the "Query" in Apex Explorer and check if its returning any rows.