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
mavsmavs 

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;
   }


}

Best Answer chosen by Admin (Salesforce Developers) 
OldDeadBugOldDeadBug

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

Ron HessRon Hess

you are using two different variables awt and lawt

 

so awt is null when you go to make the update

MATTYBMEMATTYBME

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.

 

 

MATTYBMEMATTYBME
Ron just beat me to it. Glad I was on the right track.
mavsmavs

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;
         }


}

mavsmavs

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

Message Edited by mavs on 03-31-2009 12:13 PM
OldDeadBugOldDeadBug

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. 

 

This was selected as the best answer
sgoremasgorema
hi - just checking to see if you found a fix for this? I am getting a similar error when trying to save from a custom list controller. can you provide your code if you got it to work? thanks!