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
Ram Shiva KumarRam Shiva Kumar 

Fetching Records

Hi my requirement is, i want to fetdch the recors from Account object based on the name enter in the VFpage, (when i enter the name ram in VFpage  ,then total recordswith the Name ram have to be fetched ) for that i have written the belwo code, in this codewhen enter the name in the vfpage ima getting the error like bleow
"Attempt to de-reference a null object
Error is in expression '{!values}' in component <apex:commandButton> in page chennai: Class.chennai.values: line 18, column 1 "

andmy code is as follows,

vf code:

<apex:page controller="chennai">

<apex:form>

<apex:outputText>EnterName</apex:outputText>

<apex:inputField value="{!a.Name}"/>

<apex:commandButton value="Enter"  action="{!values}" reRender="one"/>


<apex:pageBlock id="one">
<apex:pageBlockSection >

<apex:pageBlockTable value="{!at}" var="b">

<apex:column value="{!b.Name}"/>


</apex:pageBlockTable>

</apex:pageBlockSection>
</apex:pageBlock>




  </apex:form>
</apex:page>


Apex Code:

Public class chennai
{

Public chennai()
{
List<Account> at=new List<Account>();

Account a=new Account();
}

Public List<Account> at{get;set;}

Public Account a{get;set;}

Public void values()
{

List<Account> at=[select Name from Account where Name=:a.Name];


}

}

Please suggest me

Regards,
siva.


 
Best Answer chosen by Ram Shiva Kumar
Virendra ChouhanVirendra Chouhan
Hi Ram,

That error occur due to null value.

What is {!a.name} in your VF page? to bind inputfield you can use controller variable which have {get; set;} property
or if you have standard variable then you can use Objectname.field name but in your case you are using custom controller so instead of {!a.name} use a string variable to hold account name.
<apex:inputField value="{!accountName}"/>

and in your controller:
public String accountName {get; set;}
also change in your values method:
Public void values()
{
//best practise says always check null first.
If(accountName != null && accountName !='')
List<Account> at=[select Name from Account where Name=:accountName];

}

 

All Answers

Virendra ChouhanVirendra Chouhan
Hi Ram,

That error occur due to null value.

What is {!a.name} in your VF page? to bind inputfield you can use controller variable which have {get; set;} property
or if you have standard variable then you can use Objectname.field name but in your case you are using custom controller so instead of {!a.name} use a string variable to hold account name.
<apex:inputField value="{!accountName}"/>

and in your controller:
public String accountName {get; set;}
also change in your values method:
Public void values()
{
//best practise says always check null first.
If(accountName != null && accountName !='')
List<Account> at=[select Name from Account where Name=:accountName];

}

 
This was selected as the best answer
PRIYAN NEERUDUPRIYAN NEERUDU
<apex:page controller="chennai">

<apex:form >

<apex:outputText >EnterName</apex:outputText>

<apex:inputText value="{!searchTxt}" />

<apex:commandButton value="Enter"  action="{!values}" reRender="one"/>


<apex:pageBlock id="one">

<apex:pageBlockSection >

<apex:pageBlockTable value="{!at}" var="b">

<apex:column value="{!b.Name}"/>


</apex:pageBlockTable>

</apex:pageBlockSection>
</apex:pageBlock>




  </apex:form>
</apex:page>
===============================================================================


Public class chennai
{

   
public  string searchTxt {get; set;}

public List<Account> at{get; set;}

Public void values()

{

if ( searchTxt != null && searchTxt!= '') {
system.debug('searchTxt '+searchTxt );
 at=[SELECT Id,name,phone FROM Account WHERE Name LIKE: '%'+searchTxt+'%' ];

}

}

}


hope this code is helpfull.......