System.NullPointerException: попытка разыменовать нулевой объект

пожалуйста, помогите мне с моим кодом, я новичок в 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


person peXeq    schedule 17.10.2014    source источник


Ответы (2)


IF(EZR.Alter__c >= 18 && EZR.Alter__c ‹=30) { Count_1830++; }

В этой строке у вас есть нулевое значение. вы должны использовать проверку перед сравнением в блоке if.

person Kandy    schedule 17.10.2014
comment
Хорошо, что вы имеете в виду под чеком? Я новичок в Java и Salesforce, пожалуйста. - person peXeq; 17.10.2014
comment
Означает, что EZR.Alter__c имеет значение null, в какой-то момент вы должны проверить его на значение null. например, вы можете использовать if(EZR.Alter__c!=null) для выполнения действий, если появится нулевое значение. - person Kandy; 18.10.2014
comment
taroworks.zendesk.com/hc/en-us/articles/ ------- Перейдите по этой ссылке и запустите свой код один раз в режиме отладки. - person Kandy; 21.10.2014

Вам просто нужно 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
         }
      }
}

Надеюсь, это поможет вам

person NullReferenceException    schedule 18.10.2014
comment
Я все еще получаю сообщение об ошибке, пожалуйста, посмотрите ниже на мой пост ниже. - person peXeq; 20.10.2014