+ Start a Discussion
Sudhir_MeruSudhir_Meru 

Not able to Store Lookup Value to Custom Object from Visualforce Page to Controller

Hi, 

 Created a controler as mentioned below

Class
public with sharing class storelookup{

   public Asset GetReseller{get;set;}
  
   public void processSelected() {
     
        SUDHIR__c  TempAssetList = new  SUDHIR__c();
      
       TempAssetList.test__c = GetReseller.AccountId;
      
       Insert TempAssetList;
   
   }

}


Visual Force Page
<apex:page Controller="storelookup">

   <apex:form >
   <apex:pageBlock >
     <apex:inputField value="{!GetReseller.AccountId}"/>
     <apex:pageBlockButtons >
                <apex:commandButton value="Save Records" action="{!processSelected}"/>
            </apex:pageBlockButtons>
            </apex:pageBlock>
   </apex:form>
</apex:page>


I am getting below error message please suggest me how to fix

Visualforce Error
Help for this Page

System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!processSelected}' in component <apex:commandButton> in page storelookup

Class.storelookup.processSelected: line 9, column 1
Best Answer chosen by Sudhir_Meru
ra1ra1
Hi Sudhir,

You are getting NPE due to "GetReseller" is not initilize with any value.

You might have to create new instance of Assets or fetch existing Assets from database. Below is sample constructor for your reference.

public storelookup(){
     GetReseller = new Asset();
       
        // GetReseller = [select id, AccountId from Asset where ...]
    }

Hope this will solve your problem.

All Answers

ra1ra1
Hi Sudhir,

You are getting NPE due to "GetReseller" is not initilize with any value.

You might have to create new instance of Assets or fetch existing Assets from database. Below is sample constructor for your reference.

public storelookup(){
     GetReseller = new Asset();
       
        // GetReseller = [select id, AccountId from Asset where ...]
    }

Hope this will solve your problem.

This was selected as the best answer
SFDC_DevloperSFDC_Devloper
Hi Sudhir,

try below code..

public with sharing class storelookup{

   public Asset GetReseller{get;set;}
  
   public void processSelected() {
        
        GetReseller=new Asset();

        GetReseller=[SELECT AccountId from Asset limit 10];
		
        SUDHIR__c  TempAssetList = new  SUDHIR__c();
      
           TempAssetList.test__c = GetReseller.AccountId;
      
              Insert TempAssetList;
   
   }

}

[If it helps, mark it as "Best Answer"]

Thanks,
Rockzz