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
Rafal Galazkiewicz 4Rafal Galazkiewicz 4 

Attempt to de-reference a null object

I'm running int0 this error - attempt to de-reference a null object.
My scenario - VFP published as force.com site with a class to save a record first from entered fields and add attachmnet there as well if user attached that:

public class savemcd {
    public savemcd(ApexPages.StandardController controller)
    {
    }
    Public Attachment myfile;
    Public Attachment getmyfile()
    {
        myfile = new Attachment();
        return myfile;
    }

    Public Misconduct__c mymscd;
    Public Misconduct__c getmymscd()
    {
        mymscd = new Misconduct__c();
        return mymscd;
    }
  
 
    Public Pagereference Savedoc()
    {
    if (mymscd.name != null){
       Misconduct__c mcd = new Misconduct__c(Submitter__c=mymscd.Submitter__c, Phone_number__c=mymscd.Phone_number__c, Email__c=mymscd.Email__c, Description__c=mymscd.Description__c );
       insert mcd;
       string result = mcd.id;
          
           if (myfile.name != null)
           {
           Attachment a = new Attachment(parentId = result, name=myfile.name, body = myfile.body);
           /* insert the attachment */
           insert a;
           }
        }
        return NULL;
    } 
}


What do I do wrong?
Ramesh KallooriRamesh Kalloori
change the below .
Public Pagereference Savedoc()
    {
    if (getmymscd().name != null){
       Misconduct__c mcd = new Misconduct__c(Submitter__c=mymscd.Submitter__c, Phone_number__c=mymscd.Phone_number__c, Email__c=mymscd.Email__c, Description__c=mymscd.Description__c );
       insert mcd;
       string result = mcd.id;
          
           if (getmyfile().name != null)
           {
           Attachment a = new Attachment(parentId = result, name=myfile.name, body = myfile.body);
           /* insert the attachment */
           insert a;
           }
        }
        return NULL;
    } 
}
thanks,
RAmesh
Rafal Galazkiewicz 4Rafal Galazkiewicz 4
Hello,
Thank you for you comment. Indeed it helps, error is not there anymore but it doesn't save a record (attachment neither). When I push Submit button it only clears attachment selection field, other fields stay untouched and even validation rule on phone number field doesn't fire when I put there letters.

My VFP is following. Thank you in advance.

<apex:page standardController="Misconduct__c" extensions="savemcd">

    <apex:form >
          <apex:pageBlock title="Contact details">
              <apex:pageMessages />
              <apex:pageBlockSection columns="1">
                  <apex:inputField value="{!Misconduct__c.Submitter__c}"/>
                  <apex:inputField value="{!Misconduct__c.Phone_number__c}"/>
                  <apex:inputField value="{!Misconduct__c.Email__c}"/>
                  <td> <apex:inputField value="{!Misconduct__c.Description__c}" style="width: 500px; height: 80px" /> </td>
                  <apex:inputfile value="{!myfile.body}" filename="{!myfile.Name}" />
                   <apex:commandbutton value="Submit" action="{!Savedoc}"/>
                  </apex:pageBlockSection>
          </apex:pageBlock>
  </apex:form>
   
</apex:page>