You need to sign in to do that
Don't have an account?

Updating List using the custom controller
Hi -
I am trying to update the pick list values. When i click on save i am ending up with the below error.
System.NullPointerException: Attempt to de-reference a null object
Class.MyController.save: line 13, column 14 External entry point
Can someone please advise me how to update the picklist values in the custom object. This is my first app using VF. Searched the doucumentation but no luck...please help.... Here is my code
I know i have to write some logic in the pagereference save..but not getting any idea...
<apex:page controller="MyController" tabStyle="Award_Tracking__c">
<apex:form >
<apex:pageBlock mode="edit">
<apex:pageBlockTable value="{!Award_Tracking}" var="at">
<apex:column headervalue="Award/Recognition">
<apex:outputText value="{!at.Name_of_Award__c}"></apex:outputText>
</apex:column>
<apex:column headervalue="Reward Options">
<apex:inputField value="{!at.Reward_Option__c}" required="true"/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
public class MyController
{
Award_Tracking__c awt;
public List<Award_Tracking__c> getAward_Tracking()
{
Award_Tracking__c[] lawt=[select Id,Name_of_Award__c,Reward_Option__c from Award_Tracking__c where Award_date__c<=TODAY and Award_expiration_date__c>=TODAY and SFDC_ID__c=:UserInfo.getUserId()];
return lawt;
}
public PageReference save() {
update awt;
PageReference AwardShippingPage = new PageReference('/Apex/AwardShipping');
AwardShippingPage.setRedirect(true);
return AwardShippingPage;
}
}
Just taking a guess here as I'm still a newbie myself.
I belive the error message is essentially telling you that the 'lawt' array is null, which means your Query isn't providing any results, and thus you are trying to update an empty array.
So, you may need to reevaluate your query to see if it can ever return a value, and adjust it if necessary. For example, try writing the query so that it will always return one or two results:
lawt = [Select id, Name, etc... from Award_Tracking__c LIMIT 2];
If that still doesn't work, then its time for adding system.debug statements and checking debug logs to see what's going on.
If the query is correct, and is able to return values if they exist, then you may wish to use something like:
if (!lawt.isEmpty)
update lawt;
which will prevent the update call in the case of a null array.
All Answers
you are using two different variables awt and lawt
so awt is null when you go to make the update
I could be wrong here but a couple of things stand out.
First in your controller you refernce "awt" for Award_Tracking__c but else were you reference "lawt". I think it needs to be uniform. if you start using awt then stick with that.
Also, in your VF page you use:
<apex:pageBlockTable value="{!Award_Tracking}" var="at">
try using var="awt" instead. I don't think that matters too much but keeps things uniform.
Now all of that to say I am a Apex novice so I could be wrong.
Thankyou for all the responses. Now i changed my code to be uniform. when i am saving still i get the same error. Please advise...why is it not saving the values
System.NullPointerException: Attempt to de-reference a null object
Class.MyController.save: line 13, column 14 External entry point
<apex:page controller="MyController" tabStyle="Award_Tracking__c">
<apex:form >
<apex:pageBlock mode="edit">
<apex:pageBlockTable value="{!Award_Tracking}" var="lawt">
<apex:column headervalue="Award/Recognition">
<apex:outputText value="{!lawt.Name_of_Award__c}"></apex:outputText>
</apex:column>
<apex:column headervalue="Reward Options">
<apex:inputField value="{!lawt.Reward_Option__c}" required="true"/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton value="save" action="{!save}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
public class MyController
{
List<Award_Tracking__c> lawt;
public List<Award_Tracking__c> getAward_Tracking()
{
Award_Tracking__c[] lawt=[select Id,Name_of_Award__c,Reward_Option__c from Award_Tracking__c where Award_date__c<=TODAY and Award_expiration_date__c>=TODAY and SFDC_ID__c=:UserInfo.getUserId()];
return lawt;
}
public PageReference save() {
update lawt;
PageReference AwardShippingPage = new PageReference('/Apex/AwardShipping');
AwardShippingPage.setRedirect(true);
return AwardShippingPage;
}
}
Please help. I do not know where to search.
I have tried many ways .still i am getting the same error. What am i doing wrog here...PLEASE ADVISE...
I also referenced the example in the below link. This is what exactly what i am trying to achive. But when i change the pick list values and click on save i am getting System.NullPointerException: Attempt to de-reference a null object error. Please advise... How to write the update statement in the save action.
http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#StartTopic=Content%2Fpages_controller_custom.htm|SkinName=webhelp
Topic: (Editing a table of data in a page) in the Visualforce documentation
Just taking a guess here as I'm still a newbie myself.
I belive the error message is essentially telling you that the 'lawt' array is null, which means your Query isn't providing any results, and thus you are trying to update an empty array.
So, you may need to reevaluate your query to see if it can ever return a value, and adjust it if necessary. For example, try writing the query so that it will always return one or two results:
lawt = [Select id, Name, etc... from Award_Tracking__c LIMIT 2];
If that still doesn't work, then its time for adding system.debug statements and checking debug logs to see what's going on.
If the query is correct, and is able to return values if they exist, then you may wish to use something like:
if (!lawt.isEmpty)
update lawt;
which will prevent the update call in the case of a null array.