【问题标题】:System.NullPointerException: Attempt to de-reference a null objectSystem.NullPointerException:尝试取消引用空对象
【发布时间】:2014-10-17 11:56:21
【问题描述】:

请帮助我编写代码,我是 Java/SF 新手,我不理解整个代码。

下面的代码应该返回 18 岁以下、18 到 30 岁及以上的人的三个值。

我想学习编码,我评论所有行,如果有错误,请纠正我。

目前我得到错误:

System.NullPointerException:尝试取消引用空对象

Class.testfor1_c.getEZRen:第 18 行,第 1 列 Class.testfor1_c.:第 4 行,第 1 列

public class testfor1_c {

public testfor1_c(ApexPages.StandardController stdcontroller) { 
    getEZRen();
}

public Integer Count_u18 {get; set;} // variable for people < 18
public Integer Count_1830 {get; set;} // variable for people >=18 AND <30
public Integer Count_g30 {get; set;} // variable for people >30

public void getEZRen() {

List<Einzelrisiko__c> EZRList = [SELECT Alter__c FROM Einzelrisiko__c]; // create List EZRList with the information Alter__c of all people

FOR (Einzelrisiko__c EZR : EZRList) {  // Loop thru the all people

    IF(EZR.Alter__c < 18) { Count_u18++; } // if Alter__c < 18 variable Count_u18 increase
    IF(EZR.Alter__c >= 18 && EZR.Alter__c <=30) { Count_1830++; } // if Alter__c >=18 AND <=30 variable Count_1830 increase
    IF(EZR.Alter__c > 30) { Count_g30++; } // if Alter__c > 30 variable Count_g30 increase

}

}

}

<apex:page standardcontroller="account" extensions="testfor1_c">

Anzahl {!Count_u18} // show the value of people <18
Anzahl {!Count_1830} // show the value of people >=18 AND <=30
Anzahl {!Count_g30} // show the value of people >30

</apex:page>

提前致谢, peX

【问题讨论】:

    标签: java class controller salesforce apex


    【解决方案1】:

    IF(EZR.Alter__c >= 18 && EZR.Alter__c

    在这一行你有一个空值。在 if 块中进行比较之前必须使用 check。

    【讨论】:

    • 好的,“检查”是什么意思?我是 Java 和 Salesforce 的新手,拜托。
    • 意味着 EZR.Alter__c 在某些时候为空,您必须检查它是否为空。就像你可以使用 if(EZR.Alter__c!=null) 如果出现空值怎么办。
    • taroworks.zendesk.com/hc/en-us/articles/… -------通过此链接并在调试模式下运行您的代码一次。
    【解决方案2】:

    你只需要initialize the Integer Properties

    public class testfor1_c {
    
          public testfor1_c(ApexPages.StandardController stdcontroller) { 
              Count_u18 = 0;
              Count_1830 = 0;
              Count_g30 = 0;
              getEZRen();
          }
    
          public Integer Count_u18 {get; set;} // variable for people < 18
          public Integer Count_1830 {get; set;} // variable for people >=18 AND <30
          public Integer Count_g30 {get; set;} // variable for people >30
    
          public void getEZRen() {
    
             List<Einzelrisiko__c> EZRList = [SELECT Alter__c FROM Einzelrisiko__c]; // create List EZRList with the information Alter__c of all people
    
             for(Einzelrisiko__c EZR : EZRList) {  // Loop thru the all people
    
                   IF(EZR.Alter__c < 18) { Count_u18++; } // if Alter__c < 18 variable Count_u18 increase
                   IF(EZR.Alter__c >= 18 && EZR.Alter__c <=30) { Count_1830++; } // if Alter__c >=18 AND <=30 variable Count_1830 increase
                  IF(EZR.Alter__c > 30) { Count_g30++; } // if Alter__c > 30 variable Count_g30 increase
             }
          }
    }
    

    希望对你有帮助

    【讨论】:

    • 我仍然收到错误,请看下面我的帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多