function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jyosi jyosiJyosi jyosi 

to insert the records into Case object

I have a visualforce page ,
1)Created a button on Contact Page Layout
onclick of new button i need to pull case feilds once they click save a case record should be created and attached to contact Record

Below is the Visualforce Page
<apex:page standardController="Contact" extensions="GlobalComplaintExt" tabStyle="Case" id="myPage" showHeader="False">
<apex:form id="myForm">
        <apex:pageBlock id="block1" title="Complaint" >
        <apex:outputPanel >
            <apex:pageBlockSection title="Complaint " collapsible="true" id="section1" >
                <apex:outputField value="{!contact.Name}"/>
                <apex:outputField value="{!contact.AccountId}" label="Account Name"/>
               <apex:inputfield value="{!com.Potential_Injury__c}" required="true"/>
               <apex:inputfield value="{!com.Date_Received__c}"/>
               <apex:inputfield value="{!com.Complaint_Source__c}" />
               <apex:inputfield value="{!com.Origin}" required="true"/>
<apex:commandbutton action="{!SaveItem}" value="Start " />
</apex:page>
</apex:form>

Whenever the user clicks SaveItem the cases records should be created.
global class GlobalComplaintExt
{
            
        private List<String> fieldErrors = new List<String>();
        public final Contact ContactName;
        private string contactID = '';
        private string complaintID = '';
        private string productID = '';
        private string prodseg = '';
        private string prodsubseg = '';
        public Integer icounter {get;set;}
        public Contact con {get;set;}
        public Account Acct {get;set;}
        public Case com {get;set;}
        public Product2 pro {get;set;}
        private Questions_and_Answers_Custom__c questions;
        private Questions_and_Answers_Custom__c answers;
        public List<Questions_and_Answers_Custom__c> questionslist;
        public List<Questions_and_Answers_Custom__c> answerslist;
        Case Caseinsert= new Case();
       
  public GlobalComplaintExt(ApexPages.StandardController controller)
   {
     con = [select  Id, Name from Contact where id = :ApexPages.currentPage().getParameters().get('id')];         
     Caseinsert = new Case(ContactId = con.Id);
   }
    
    /*
        Used to Save the values into the Case Object

    */
  public pageReference SaveItem()
   {
     
   /*
      CaseInsert.Potential_Injury__c=com.Potential_Injury__c;
     CaseInsert.Date_Received__c=com.Date_Received__c;
     CaseInsert.Complaint_Source__c=com.Complaint_Source__c;
     CaseInsert.Origin=com.Origin;
     CaseInsert.Segment__c=com.Segment__c;
     CaseInsert.Sub_Segment__c=com.Sub_Segment__c;
     CaseInsert.Product__c=com.Product__c;
     CaseInsert.Associated_Product__c =com.Associated_Product__c;
     CaseInsert.Complaint_Qty__c=com.Complaint_Qty__c;
     CaseInsert.Complaint_Qty_Unit_of_Measure__c=com.Complaint_Qty_Unit_of_Measure__c;
     CaseInsert.Complaint_Lot__c=com.Complaint_Lot__c;
     CaseInsert.Complaint_Lot2__c =com.Complaint_Lot__c;
     CaseInsert.Complaint_Lot3__c =com.Complaint_Lot3__c;
     CaseInsert.Complaint_Lot_Unknown__c=com.Complaint_Lot_Unknown__c;
     CaseInsert.Normal_Usage__c=com.Normal_Usage__c;
     CaseInsert.Actual_Usage__c=com.Actual_Usage__c;
     //cases.Subject=com.Subject;
     CaseInsert.Country__c=com.Country__c;
     CaseInsert.Voice_of_Customer__c=com.Voice_of_Customer__c;
     CaseInsert.Complaint_Code__c=com.Complaint_Code__c;
     CaseInsert.Problem_Damage_Code__c=com.Problem_Damage_Code__c;
     CaseInsert.Samples_to_be_Sent__c=com.Samples_to_be_Sent__c;
     CaseInsert.Replacement_Product__c=com.Replacement_Product__c;
       
     CaseInsert.Replacement_Product_Quantity__c=com.Replacement_Product_Quantity__c;
     CaseInsert.DE_Replacment_Unit_of_Measure__c=com.DE_Replacment_Unit_of_Measure__c;
     CaseInsert.Credit__c=com.Credit__c;
     CaseInsert.Credit_Note_Number__c=com.Credit_Note_Number__c;
     CaseInsert.Replacement_Order__c=com.Replacement_Order__c;
       
     CaseInsert.Replacement_Order_Number__c=com.Replacement_Order_Number__c;
     CaseInsert.Replacement_Lot_Number__c=com.Replacement_Lot_Number__c;
     CaseInsert.Claim_for_Damages__c=com.Claim_for_Damages__c;
     CaseInsert.Type_of_damage__c=com.Type_of_damage__c;
     CaseInsert.DE_Period_of_record_keeping__c=com.DE_Period_of_record_keeping__c;
     insert CaseInsert;
     system.debug('cases---->>>>>>>>>'+CaseInsert);
     PageReference pageRef = new PageReference('/t');
     pageRef.setRedirect(true);
     return pageRef;
*/
       insert com;
       return null;
     
   }

   public pageReference cancelAction()
   {
         return null;
   }
    

}


System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!SaveItem}' in component <apex:commandButton> in page globalcomplaint: Class.GlobalComplaintExt.SaveItem: line 77, column 1
Class.GlobalComplaintExt.SaveItem: line 77, column 1


Can you help me to fix this

Regards,
Jyo
Niket SFNiket SF
Line 14 

 public Case com {get;set;} 

 Memory not initialised. giving Null pointer exception when you try to insert case.
==========================================================
Updated Code:

 public Case com{
    get
    {
        if(com == null)
            com = New Case();
        return com;
    }set;
}
Krishna SambarajuKrishna Sambaraju
Check the line 77 of your code, you might be referencing an object / field that is null.

Regards,
Krishna.
Krishna SambarajuKrishna Sambaraju
Hi jyosi,

The variable com has never been initialized. You can initialize it as Niket SF given above or you can initialize the variable in the constructor as below

public GlobalComplaintExt(ApexPages.StandardController controller)
   {
     con = [select  Id, Name from Contact where id = :ApexPages.currentPage().getParameters().get('id')];         
     Caseinsert = new Case(ContactId = con.Id);
     com = new Case();
   }

Regards,
Krishna.