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
MohanaGopalMohanaGopal 

Null value Error in getter method

Hi...
    In my visual force page I am using two get methods one is binding with inputfield and another one is inputtext...
 
If my get method query returns 0 record then it shows
 
System.QueryException: List has no rows for assignment to SObject

Class.Control1.getPerSav: line 11, column 7
External entry point
 
How to avoid it...
 
 I saw a sample code for selectlist avoiding this error.. I have implemented it in following code..
 
Any one give sample code for avoiding those error in  inputfield and inputtext...
 
Apex class 
 
public class Control1
{
private Calculated_Measures__c calculatedControl;

List<SelectOption> componentList = null;

public List<SelectOption> getComponents()
{
 componentList = new List<SelectOption>();
 New_List__c[] p;
 componentList.add(new selectoption('',''));
 p=[select Id,Name from New_List__c where Measure__c='Control'];
 if (p.size() >-1)
  {
         for(integer i = 0; i < p.size(); i++)
         {
              componentList.add(new selectoption(p[i].Id,p[i].Name));
         }
  }   
  
        return componentList;
}

public Control1(ApexPages.StandardController stdController)
{
this.calculatedControl = (Calculated_Measures__c)stdController.getRecord();
}
}
 
 
hisrinuhisrinu
Hi MohanGopal,


you can declare the list outside like this.

List<SelectOption> componentList = new List<SelectOption>{};


or   You can declare that list inside

List<SelectOption> componentList;

Ron HessRon Hess
try this

 New_List__c[] p = new New_List__c[] ();

that way if the query is empty you won't be accessing undefined variable when you do
p.size()


MohanaGopalMohanaGopal

Thanks

  Its work properly..