Using ProcessAdmission controller extension i am trying to read data entered on the vf page to insert a new record into Student__C custom object. but not getting data instead i received "Null pointer exception" at bold lines.

Visualforce page:

<apex:pageBlockSection title="New Student Admission">

            <apex:outputLabel id="inputtxta" value="First Name" for="StdFName"/>
            <apex:inputText id="StdFName" value="{!StdFName}"/>
            <apex:outputLabel id="inputtxtb" value="Last Name" for="StdLName"/>
            <apex:inputText id="StdLName" value="{!StdLName}"/>
            <apex:outputLabel id="inputtxtc" value="Father's Name " for="StdFatherName"/>
            <apex:inputText id="StdFatherName" value="{!StdFatherName}"/>
            <apex:outputLabel id="selectLista" value="Gender" for="StdGender"/>
            <apex:selectList id="StdGender" value="{!gendervalue}" size="1">
                <apex:selectOption itemValue="Male" itemLabel="male" />
                <apex:selectOption itemValue="Female" itemLabel="female"/>
                <apex:selectOption itemValue="Transgender" itemLabel="TansGender"/>
    <apex:outputLabel id="buttonid" value="button" for="validate"/>
          <apex:commandButton id="validate" value="Submit" action="{!submit}"/>


Apex class:

 public class ProcessAdmission {

    private ApexPages.StandardController controller;
    public Student__c std;
    public String StdFName{get; set;}
    public String StdLName{get; set;}

    public String StdFatherName{get; set;}
    public String gendervalue{get; set;}
  //  public list<student__c> dataRecords{get; set;}
    public Student__c datarecords;

   public ProcessAdmission(ApexPages.StandardController con)
        /*dataRecords = [select id, Student_First_Name__c, student_Last_Name__c, father_Name__c, gender__C 
        from student__c
         WHERE Id = :ApexPages.currentPage().getParameters().get('id')];*/
        **datarecords = (Student__c)controller.getRecord();**

       /* datarecords = (Student__c)controller.getRecord();     


   /* public Student__C getStd()
        return std;
    public void submit()
       /* std.Student_First_Name__c = stdFirstName;
        std.Student_Last_Name__c = stdlastName;
        std.Father_Name__c = stdfatherName;
        std.gender__c = gender;
      /* StdFName = ApexPages.currentPage().getParameters().get('StdFName');
        StdLName = ApexPages.currentPage().getParameters().get('StdLName');
        StdFatherName = ApexPages.currentPage().getParameters().get('StdFatherName');
        gendervalue = ApexPages.currentPage().getParameters().get('gendervalue');*/
        **system.debug('the value is '+stdFname);**
        std.Student_First_Name__c = stdFName;
        std.Student_Last_Name__c = stdLName;
        std.Father_Name__c = stdfatherName;
        std.gender__c = gendervalue;
        student__C singleRec;   
        /*for(student__c std : dataRecords)
            singleRec = 
        insert dataRecords;

2 Answers 2


Tough to say for sure where you're getting the error (there is no bold formatting in a block of code), but I'm guessing it's because you're referencing your controller variable in the constructor without ever setting it. Adding the following as the first line in your constructor should do the trick:

this.controller = con;

if you have get and set methods against string you do not need to do getParameters().get('StdFName'); kind of stuff. getter method will get data from VF page anyways few things :

1) You can use the object and its field directly on the page without using string placeholders 2) you need to instantiate ("new") the object Student__c in constructor and then insert it. In your case you do not have an instance , so null pointer.

code can be simple like :

public student__c std {get;set;}

public constructor() {
    std = new student__c();

public void submit() {
    insert std

and on page

<apex:inputfield value="{!std.field__c}"/>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .