“Attempt to de-reference a null object” on VF page

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












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?










share|improve this question























  • How is your 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.
    – Jayant Das
    4 hours ago











  • Please note, I have edited the question title to reflect your actual issue.
    – Jayant Das
    4 hours ago










  • Thanks for the edit on the title, I wasn't fully understanding what was going on.
    – PHonnold
    3 hours ago
















up vote
1
down vote

favorite












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?










share|improve this question























  • How is your 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.
    – Jayant Das
    4 hours ago











  • Please note, I have edited the question title to reflect your actual issue.
    – Jayant Das
    4 hours ago










  • Thanks for the edit on the title, I wasn't fully understanding what was going on.
    – PHonnold
    3 hours ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











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?










share|improve this question















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?







apex visualforce query






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago

























asked 4 hours ago









PHonnold

153




153











  • How is your 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.
    – Jayant Das
    4 hours ago











  • Please note, I have edited the question title to reflect your actual issue.
    – Jayant Das
    4 hours ago










  • Thanks for the edit on the title, I wasn't fully understanding what was going on.
    – PHonnold
    3 hours ago
















  • How is your 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.
    – Jayant Das
    4 hours ago











  • Please note, I have edited the question title to reflect your actual issue.
    – Jayant Das
    4 hours ago










  • Thanks for the edit on the title, I wasn't fully understanding what was going on.
    – PHonnold
    3 hours ago















How is your 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.
– Jayant Das
4 hours ago





How is your 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.
– Jayant Das
4 hours ago













Please note, I have edited the question title to reflect your actual issue.
– Jayant Das
4 hours ago




Please note, I have edited the question title to reflect your actual issue.
– Jayant Das
4 hours ago












Thanks for the edit on the title, I wasn't fully understanding what was going on.
– PHonnold
3 hours ago




Thanks for the edit on the title, I wasn't fully understanding what was going on.
– PHonnold
3 hours ago










2 Answers
2






active

oldest

votes

















up vote
2
down vote













You're question title is mis-leading. The "too large to display" error is simply because you can only fit so many characters on a single line in the debug logs. Instead of printing the entire array of price books, in your loop add system.debug(string.valueof(a)).



In terms of your actual error my best bet is that pricebook_name appears to be a list. Make sure you initialize the list with pricebook_name = new List<selectOption>(); You can verify this by using system.debug(string.valueOf(pricebook_name)); before your loop.



When you see attempt to deference a null object you should always look for the . character in your code. This character is the deference operator. This means that what ever is to the left of the . character is null. You're code only has a deference in a few places but the only one likely to through an error is the one I mentioned above.






share|improve this answer




















  • So it seems I don't fully understand variable assignment here. I had the following assignment outside the code block that was shown. public List<SelectOption> pricebook_name; I tried the option that was posted above 'pricebook_name = new List<selectOption>();' but got the error message that the pricebook_name variable could not be found. I had to change it to the setup that Jayant Das talked about below 'List<SelectOption> pricebook_name = new List<SelectOption>();' Testing this fully right now, but not sure why the one worked and the other didn't.
    – PHonnold
    3 hours ago











  • @PHonnold - because you are using pricebook_name on the VF page, so you need to have it declared at the class level (outside the method where you are populating it) with a getter method. And that the variable needs to be initialized, so that you can add values in it.
    – Jayant Das
    3 hours ago










  • The difference between pricebook_name = new List<SelectOption(); and List<SelectOption> pricebook_name = new List<SelectOption(); is simply that in the latter you declare and initialize in the same line. In the former it assumes you declare the variable earlier in the code using List<SelectOption> pricebook_name; and then you call pricebook_name = new List<SelectOption>(); on a different line. Ultimately, they accomplish the exact same thing.
    – gNerb
    1 hour ago


















up vote
1
down vote













Your potential issue is on this line for the NullPointerException.



pricebook_name.add( new SelectOption( a.Id, a.Name ) ); 


And it seems that pricebook_name may not have been initialized. That will be the only scenario, when you will get the NPE as you are experiencing.



You should verify if the variable has been initialized before you are adding values in it.



A typical variable declaration and initialization here would look like as below, before you can actually add values in it. Note that you won't get any error during compile time but only run-time as you are experiencing.



List<SelectOption> pricebook_name = new List<SelectOption>();





share|improve this answer




















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "459"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f238421%2fattempt-to-de-reference-a-null-object-on-vf-page%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    You're question title is mis-leading. The "too large to display" error is simply because you can only fit so many characters on a single line in the debug logs. Instead of printing the entire array of price books, in your loop add system.debug(string.valueof(a)).



    In terms of your actual error my best bet is that pricebook_name appears to be a list. Make sure you initialize the list with pricebook_name = new List<selectOption>(); You can verify this by using system.debug(string.valueOf(pricebook_name)); before your loop.



    When you see attempt to deference a null object you should always look for the . character in your code. This character is the deference operator. This means that what ever is to the left of the . character is null. You're code only has a deference in a few places but the only one likely to through an error is the one I mentioned above.






    share|improve this answer




















    • So it seems I don't fully understand variable assignment here. I had the following assignment outside the code block that was shown. public List<SelectOption> pricebook_name; I tried the option that was posted above 'pricebook_name = new List<selectOption>();' but got the error message that the pricebook_name variable could not be found. I had to change it to the setup that Jayant Das talked about below 'List<SelectOption> pricebook_name = new List<SelectOption>();' Testing this fully right now, but not sure why the one worked and the other didn't.
      – PHonnold
      3 hours ago











    • @PHonnold - because you are using pricebook_name on the VF page, so you need to have it declared at the class level (outside the method where you are populating it) with a getter method. And that the variable needs to be initialized, so that you can add values in it.
      – Jayant Das
      3 hours ago










    • The difference between pricebook_name = new List<SelectOption(); and List<SelectOption> pricebook_name = new List<SelectOption(); is simply that in the latter you declare and initialize in the same line. In the former it assumes you declare the variable earlier in the code using List<SelectOption> pricebook_name; and then you call pricebook_name = new List<SelectOption>(); on a different line. Ultimately, they accomplish the exact same thing.
      – gNerb
      1 hour ago















    up vote
    2
    down vote













    You're question title is mis-leading. The "too large to display" error is simply because you can only fit so many characters on a single line in the debug logs. Instead of printing the entire array of price books, in your loop add system.debug(string.valueof(a)).



    In terms of your actual error my best bet is that pricebook_name appears to be a list. Make sure you initialize the list with pricebook_name = new List<selectOption>(); You can verify this by using system.debug(string.valueOf(pricebook_name)); before your loop.



    When you see attempt to deference a null object you should always look for the . character in your code. This character is the deference operator. This means that what ever is to the left of the . character is null. You're code only has a deference in a few places but the only one likely to through an error is the one I mentioned above.






    share|improve this answer




















    • So it seems I don't fully understand variable assignment here. I had the following assignment outside the code block that was shown. public List<SelectOption> pricebook_name; I tried the option that was posted above 'pricebook_name = new List<selectOption>();' but got the error message that the pricebook_name variable could not be found. I had to change it to the setup that Jayant Das talked about below 'List<SelectOption> pricebook_name = new List<SelectOption>();' Testing this fully right now, but not sure why the one worked and the other didn't.
      – PHonnold
      3 hours ago











    • @PHonnold - because you are using pricebook_name on the VF page, so you need to have it declared at the class level (outside the method where you are populating it) with a getter method. And that the variable needs to be initialized, so that you can add values in it.
      – Jayant Das
      3 hours ago










    • The difference between pricebook_name = new List<SelectOption(); and List<SelectOption> pricebook_name = new List<SelectOption(); is simply that in the latter you declare and initialize in the same line. In the former it assumes you declare the variable earlier in the code using List<SelectOption> pricebook_name; and then you call pricebook_name = new List<SelectOption>(); on a different line. Ultimately, they accomplish the exact same thing.
      – gNerb
      1 hour ago













    up vote
    2
    down vote










    up vote
    2
    down vote









    You're question title is mis-leading. The "too large to display" error is simply because you can only fit so many characters on a single line in the debug logs. Instead of printing the entire array of price books, in your loop add system.debug(string.valueof(a)).



    In terms of your actual error my best bet is that pricebook_name appears to be a list. Make sure you initialize the list with pricebook_name = new List<selectOption>(); You can verify this by using system.debug(string.valueOf(pricebook_name)); before your loop.



    When you see attempt to deference a null object you should always look for the . character in your code. This character is the deference operator. This means that what ever is to the left of the . character is null. You're code only has a deference in a few places but the only one likely to through an error is the one I mentioned above.






    share|improve this answer












    You're question title is mis-leading. The "too large to display" error is simply because you can only fit so many characters on a single line in the debug logs. Instead of printing the entire array of price books, in your loop add system.debug(string.valueof(a)).



    In terms of your actual error my best bet is that pricebook_name appears to be a list. Make sure you initialize the list with pricebook_name = new List<selectOption>(); You can verify this by using system.debug(string.valueOf(pricebook_name)); before your loop.



    When you see attempt to deference a null object you should always look for the . character in your code. This character is the deference operator. This means that what ever is to the left of the . character is null. You're code only has a deference in a few places but the only one likely to through an error is the one I mentioned above.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 4 hours ago









    gNerb

    5,450734




    5,450734











    • So it seems I don't fully understand variable assignment here. I had the following assignment outside the code block that was shown. public List<SelectOption> pricebook_name; I tried the option that was posted above 'pricebook_name = new List<selectOption>();' but got the error message that the pricebook_name variable could not be found. I had to change it to the setup that Jayant Das talked about below 'List<SelectOption> pricebook_name = new List<SelectOption>();' Testing this fully right now, but not sure why the one worked and the other didn't.
      – PHonnold
      3 hours ago











    • @PHonnold - because you are using pricebook_name on the VF page, so you need to have it declared at the class level (outside the method where you are populating it) with a getter method. And that the variable needs to be initialized, so that you can add values in it.
      – Jayant Das
      3 hours ago










    • The difference between pricebook_name = new List<SelectOption(); and List<SelectOption> pricebook_name = new List<SelectOption(); is simply that in the latter you declare and initialize in the same line. In the former it assumes you declare the variable earlier in the code using List<SelectOption> pricebook_name; and then you call pricebook_name = new List<SelectOption>(); on a different line. Ultimately, they accomplish the exact same thing.
      – gNerb
      1 hour ago

















    • So it seems I don't fully understand variable assignment here. I had the following assignment outside the code block that was shown. public List<SelectOption> pricebook_name; I tried the option that was posted above 'pricebook_name = new List<selectOption>();' but got the error message that the pricebook_name variable could not be found. I had to change it to the setup that Jayant Das talked about below 'List<SelectOption> pricebook_name = new List<SelectOption>();' Testing this fully right now, but not sure why the one worked and the other didn't.
      – PHonnold
      3 hours ago











    • @PHonnold - because you are using pricebook_name on the VF page, so you need to have it declared at the class level (outside the method where you are populating it) with a getter method. And that the variable needs to be initialized, so that you can add values in it.
      – Jayant Das
      3 hours ago










    • The difference between pricebook_name = new List<SelectOption(); and List<SelectOption> pricebook_name = new List<SelectOption(); is simply that in the latter you declare and initialize in the same line. In the former it assumes you declare the variable earlier in the code using List<SelectOption> pricebook_name; and then you call pricebook_name = new List<SelectOption>(); on a different line. Ultimately, they accomplish the exact same thing.
      – gNerb
      1 hour ago
















    So it seems I don't fully understand variable assignment here. I had the following assignment outside the code block that was shown. public List<SelectOption> pricebook_name; I tried the option that was posted above 'pricebook_name = new List<selectOption>();' but got the error message that the pricebook_name variable could not be found. I had to change it to the setup that Jayant Das talked about below 'List<SelectOption> pricebook_name = new List<SelectOption>();' Testing this fully right now, but not sure why the one worked and the other didn't.
    – PHonnold
    3 hours ago





    So it seems I don't fully understand variable assignment here. I had the following assignment outside the code block that was shown. public List<SelectOption> pricebook_name; I tried the option that was posted above 'pricebook_name = new List<selectOption>();' but got the error message that the pricebook_name variable could not be found. I had to change it to the setup that Jayant Das talked about below 'List<SelectOption> pricebook_name = new List<SelectOption>();' Testing this fully right now, but not sure why the one worked and the other didn't.
    – PHonnold
    3 hours ago













    @PHonnold - because you are using pricebook_name on the VF page, so you need to have it declared at the class level (outside the method where you are populating it) with a getter method. And that the variable needs to be initialized, so that you can add values in it.
    – Jayant Das
    3 hours ago




    @PHonnold - because you are using pricebook_name on the VF page, so you need to have it declared at the class level (outside the method where you are populating it) with a getter method. And that the variable needs to be initialized, so that you can add values in it.
    – Jayant Das
    3 hours ago












    The difference between pricebook_name = new List<SelectOption(); and List<SelectOption> pricebook_name = new List<SelectOption(); is simply that in the latter you declare and initialize in the same line. In the former it assumes you declare the variable earlier in the code using List<SelectOption> pricebook_name; and then you call pricebook_name = new List<SelectOption>(); on a different line. Ultimately, they accomplish the exact same thing.
    – gNerb
    1 hour ago





    The difference between pricebook_name = new List<SelectOption(); and List<SelectOption> pricebook_name = new List<SelectOption(); is simply that in the latter you declare and initialize in the same line. In the former it assumes you declare the variable earlier in the code using List<SelectOption> pricebook_name; and then you call pricebook_name = new List<SelectOption>(); on a different line. Ultimately, they accomplish the exact same thing.
    – gNerb
    1 hour ago













    up vote
    1
    down vote













    Your potential issue is on this line for the NullPointerException.



    pricebook_name.add( new SelectOption( a.Id, a.Name ) ); 


    And it seems that pricebook_name may not have been initialized. That will be the only scenario, when you will get the NPE as you are experiencing.



    You should verify if the variable has been initialized before you are adding values in it.



    A typical variable declaration and initialization here would look like as below, before you can actually add values in it. Note that you won't get any error during compile time but only run-time as you are experiencing.



    List<SelectOption> pricebook_name = new List<SelectOption>();





    share|improve this answer
























      up vote
      1
      down vote













      Your potential issue is on this line for the NullPointerException.



      pricebook_name.add( new SelectOption( a.Id, a.Name ) ); 


      And it seems that pricebook_name may not have been initialized. That will be the only scenario, when you will get the NPE as you are experiencing.



      You should verify if the variable has been initialized before you are adding values in it.



      A typical variable declaration and initialization here would look like as below, before you can actually add values in it. Note that you won't get any error during compile time but only run-time as you are experiencing.



      List<SelectOption> pricebook_name = new List<SelectOption>();





      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        Your potential issue is on this line for the NullPointerException.



        pricebook_name.add( new SelectOption( a.Id, a.Name ) ); 


        And it seems that pricebook_name may not have been initialized. That will be the only scenario, when you will get the NPE as you are experiencing.



        You should verify if the variable has been initialized before you are adding values in it.



        A typical variable declaration and initialization here would look like as below, before you can actually add values in it. Note that you won't get any error during compile time but only run-time as you are experiencing.



        List<SelectOption> pricebook_name = new List<SelectOption>();





        share|improve this answer












        Your potential issue is on this line for the NullPointerException.



        pricebook_name.add( new SelectOption( a.Id, a.Name ) ); 


        And it seems that pricebook_name may not have been initialized. That will be the only scenario, when you will get the NPE as you are experiencing.



        You should verify if the variable has been initialized before you are adding values in it.



        A typical variable declaration and initialization here would look like as below, before you can actually add values in it. Note that you won't get any error during compile time but only run-time as you are experiencing.



        List<SelectOption> pricebook_name = new List<SelectOption>();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 4 hours ago









        Jayant Das

        9,5602522




        9,5602522



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f238421%2fattempt-to-de-reference-a-null-object-on-vf-page%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            Confectionery