I am creating a product selection process via a custom apex controller and visualforce page.
When the page is rendered it gives the error message "Attempt to de-reference a null object".
The end of the error log says the problem is with a query that is being attempted. Backtracking in the log for the data that should be used for the query the following error is found.
11:55:33.0 (9330713)|VARIABLE_ASSIGNMENT|[7]|priceBooks|"List of size 6 too large to display"|0x43d8a527
The line that is causing the null object error is the query in the following block of code, attempting to get a selection of price books from the database.
public queryProduct( ) {
//Getting list of price books to show user.
List<Pricebook2> priceBooks = [select id, name from PriceBook2 where isActive=True];
System.debug( 'Price books ' + priceBooks);
for( Pricebook2 a : priceBooks ) {
pricebook_name.add( new SelectOption( a.Id, a.Name ) );
}
}
The code is being used on the visualforce page in the following way.
<apex:pageBlockSection title="Pricebook selection">
<apex:form >
<apex:outputLabel value="Which Pricebook? :"/>
<apex:selectList value="{!selected_Pricebook}" size="1">
<apex:selectOptions value="{!pricebook_name}"/>
</apex:selectList>
<br /><br />
<apex:commandButton value="Search records" action="{!search}" rerender="out"/>
<apex:commandButton value="Clear records" action="{!clear}"/>
</apex:form>
</apex:pageBlockSection>
User will select one and then get a list of all products in that book, to then ultimately select a product, to be used later in a process that is being developed.
I don't understand why this bit of code isn't working. The System.debug message in the log reflects that the records in questions are found and being displayed, at least in the log. Why is the code crashing out like this?
Editing to add more detail So I thought I was starting to understand better, but guess not. Here are the full code pages I am working with.
Apex Class
public class queryProduct {
List<SelectOption> pricebook_name {get;set;}
public string selected_Pricebook {get;set;}
public queryProduct( ) {
List<selectOption> pricebook_name;
//pricebook_name= new List<SelectOption>();
//Getting list of price books to show user.
List<Pricebook2> priceBooks = [select id, name from PriceBook2 where isActive=True];
//System.debug( 'Price books ' + priceBooks);
system.debug(string.valueOf(pricebook_name));
for( Pricebook2 a : priceBooks ) {
pricebook_name.add( new SelectOption( a.Id, a.Name ) );
}
}
public void search(){
List<SelectOption> options;
List<PricebookEntry> productList = [select Name, Product2Id from PriceBookEntry where Pricebook2Id=:selected_Pricebook];
for( PricebookEntry aa : productList ) {
options.add(new SelectOption( aa.Product2Id, aa.Name));
}
}
public void clear(){
//pricebook_name.clear();
}
}
VF Page
<apex:page Controller="queryProduct">
<apex:pageBlockSection title="Pricebook selection">
<apex:form >
<apex:outputLabel value="Which Pricebook? :"/>
<apex:selectList value="{!selected_Pricebook}" size="1">
<apex:selectOptions value="{!pricebook_name}"/>
</apex:selectList>
<br /><br />
<apex:commandButton value="Search records" action="{!search}" rerender="out"/>
<apex:commandButton value="Clear records" action="{!clear}"/>
</apex:form>
</apex:pageBlockSection>
I am now getting the error Unknown property 'queryProduct.pricebook_name' when trying to save the VF page. I ''think'' I have the getter set and the var initialized, but I am doing something wrong here still it seems. What am I not understanding here?
pricebook_name
declared? It seems that you haven't initialized the variable and are trying to add the select options in it directly which is causing a null pointer exception.