2012-10-11 61 views
1

我正在嘗試將數據提交到服務器,並將其存儲在salesforce中。我收到的錯誤是「試圖在服務器上解除引用空對象」。所以我想知道是什麼問題... 下面是示例代碼:嘗試去引用一個空對象:Salesforce

public static List<String> processAgentVisitSurvey(ProcessSurveySubmission.SurveySubmission submission, Map<String, Submission_Answer__c> answers, Person__c person) { 

    // Load the TDR 
    TDR__c tdr = loadTdr(person); 

    if (tdr == null) { 

     //Send an email saying that an unregistered person is trying to act a TDR 

     // Send back the error message 
     return new String[] { '0', 'User with handset Id ' + submission.imei + ' is not a TDR', 'SUPRESSMSG' }; 
    } 

這是錯誤消息的來源。

有一類的重定向這種方法:

private static List<String> additionalProcessing(
     SurveySubmission surveySubmission, 
     Survey__c survey, 
     Person__c interviewer, 
     Id intervieweeId 
) { 
    List<String> returnValues = new List<String>(); 

    Map<String, Submission_Answer__c> answers = parseSubmissionToMap(surveySubmission); 

    // Find the name of the method that this survey hooks into to do its post processing 
    try { 
     if (survey.Post_Processing_Method__c.equalsIgnoreCase('None')) { 
      returnValues.add('0'); 
      returnValues.add('There is no post processing method specified for this survey'); 
      returnValues.add('SUPRESSMSG'); 
     } 
     else if (survey.Post_Processing_Method__c.equals('CKW_Registration')) { 
      return CkwRegistration.processCkwRegistration(answers, interviewer); 
     } 
     else if (survey.Post_Processing_Method__c.equals('CKW_Baseline')) { 
      return CkwRegistration.processCkwBaseline(answers, interviewer); 
     } 
     else if (survey.Post_Processing_Method__c.equals('CKW_Staff_Update')) { 
      return CkwRegistration.processCkwUpdate(answers, interviewer); 
     } 
     else if (survey.Post_Processing_Method__c.equals('Subcounty_Registration')) { 
      return CkwRegistration.processSubcounties(answers, interviewer); 
     } 
     else if (survey.Post_Processing_Method__c.equals('TDR_AGENT_VISIT')) { 
      return TdrHelpers.processAgentVisitSurvey(surveySubmission, answers, interviewer); 
     } 
     else if (survey.Post_Processing_Method__c.equals('UDOM_RAIN_GUAGE')) { 
      return UDoMSurveyProcessing.processDailyRainGauge(surveySubmission, answers, interviewer); 
     } 
     else if (survey.Post_Processing_Method__c.equals('UDOM_RAIN_GUAGE_REG')) { 
      return UDoMSurveyProcessing.registerRainGauge(surveySubmission, answers, interviewer); 
     } 
     else if (survey.Post_Processing_Method__c.equals('MTN_CHANNELS')) { 
      return MtnChannelsHelpers.processChannelsFFPSSurvey(surveySubmission, answers, interviewer); 
     } 
     else if (survey.Post_Processing_Method__c.equals('FHI_GROUP_REGISTRATION')) { 
     return FHISurveysHelpers.processGroupRegistration(surveySubmission, answers, interviewer, survey.Survey_Name__c); 
     } 
     else if (survey.Post_Processing_Method__c.equals('FHI_HOUSEHOLD_REGISTRATION')) { 
      return FHISurveysHelpers.processHouseholdRegistration(surveySubmission, answers, interviewer, survey.Survey_Name__c); 
     } 
//   else if (survey.Post_Processing_Method__c.equals('Colombia_Farmer_Registration')) { 
//    return ColombiaFarmerRegistrationPostProcessor.processSubmission(surveySubmission, answers, interviewer); 
//   } 
     else if (survey.Post_Processing_Method__c.equals('FIELD_OFFICER_SUPPORT')) { 
      return FieldOfficeHelpers.processFoSurvey(surveySubmission, answers, interviewer); 
     } 
//   else if (survey.Post_Processing_Method__c.equals('DATA_VALIDATOR_SPOT_CHECK')) { 
//    return DataValidatorHelpers.processSpotCheck(surveySubmission, answers, interviewer); 
//   } 
//   else if (survey.Post_Processing_Method__c.equals('DATA_VALIDATOR_BACK_CHECK')) { 
//    return DataValidatorHelpers.processBackCheck(surveySubmission, answers, interviewer); 
//   } 
     else if (survey.Post_Processing_Method__c.equals('EQUIPMENT_TRACKING')) { 
      return EquipmentTrackingHelpers.processFieldOfficerSubmission(surveySubmission, answers, interviewer); 
     } 
    } 
    catch (Exception e) { 
     returnValues.add('0'); 
     returnValues.add(e.getMessage()); 
     returnValues.add('An error occured. Please contact support'); 
    } 
    return returnValues; 
} 

我認爲這是很好...

請幫忙怎麼我似乎沒有看到任何問題 謝謝。我希望代碼提供就夠了。

+0

請寫一行錯誤發生的地方。 現在,它只是你使用未初始化的變量。這個變量是什麼?爲什麼......不知道錯誤發生在哪裏很難告訴你。 –

+0

如果你只是說SomeObject.SomeField__c.someStringMethod();你會遇到大量的空引用錯誤。這是因爲它們並不總是有值,並且將爲空。我寫了一個名爲'getSObjectField()'的包裝方法,並將其放入服務類中。本質上它是這樣做的:'(someField == null)? '':someField;'如果值爲空,則返回一個空字符串,否則返回值。這有助於在直接引用sobject字段時避免這些空錯誤! –

回答

0

通常當我遇到那個錯誤時,我的第一個直覺就是尋找任何查詢。在APEX中,當您將一個空值(預期與否)返回到單個項目(如Person__c person = [query that returns objects};)中時,會引發錯誤。

的解決方案是確保數據被返回到一個具體的sObject如...

List<Person__c> persons = [Here is a query or method call];

然後你會檢查與persons.size()這種服從Salesforce的Bulkify一切辦法,他們執行以及一個列表更強大的後端。

對不起,我無法提供更多的支持,錯誤在您的代碼示例沒有行號或調試日誌不是很明顯。

祝你好運!

0

請注意:如果您試圖引用類的非實例化屬性,也會出現此錯誤。

示例:如果聲明某個類的List屬性,但從未實例化該屬性,然後嘗試添加到該列表中,則會出現此錯誤。

相關問題