尝试保存时出现视觉力错误

时间:2013-06-11 12:04:29

标签: apex-code

    <apex:page controller="MyController" tabStyle="Account"  showChat="false">
        <apex:form >
            <apex:pageBlock title="Congratulations {!$User.FirstName}">
                You belong to Account Name: <apex:inputField value="{!account.name}"/>
               <apex:commandButton action="{!save}" value="save"/></apex:pageblock>
        </apex:form>
    </apex:page>



    /* Apex class*/

    public class MyController {

        private final Account account;

        public MyController() {
            account = [SELECT Id, Name, Site FROM Account 
                       WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
        }

        public Account getAccount() {
            return account;
        }

        public PageReference save() {
            update account;
            return null;
        }
    }




Unable to insert account name.any solution to get rid from below error:

System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!save}' in component <apex:page> in page fresh14

任何人都可以提出最佳解决方案。 主要问题是公共PageReference save()代码的“插入”选项

任何人都可以使用插入代码

来消化

插入和更新代码的组合。

1 个答案:

答案 0 :(得分:3)

 public Account account {get;set;}       
 public MyController() {
        string accId =  ApexPages.currentPage().getParameters().get('id');
          if(accId  != null)           
           account = [SELECT Id, Name, Site FROM Account 
                       WHERE Id = :accId  ];
         else account = new Account ();
        }


        public PageReference save() {
            upsert account;
            return null;
        }