+ Start a Discussion
Nilam RoyNilam Roy 

Null Pointer Exception

I have a chatter group where user can put a question and if any user answer the question , requestor can choose the answer as a "BestAnswer". Below class is reponsible for this functionality. Now the issue is bestanswer flag is not coming in the detail page. we have found out the resolution. could someone help me to get it as i am new into SFDC?

for(sobject s : scope){
              jj_sss_Question_And_Answer__c question = (jj_sss_Question_And_Answer__c)s;
            try {                               (4)
                ConnectApi.FeedItem fi = jj_sss_Chatter_Functions.GetFeedItem(question.jj_sss_Feed_Item_Id__c);          (1)
                ConnectApi.QuestionAttachment qa = (ConnectApi.QuestionAttachment)fi.attachment;         (2)
                if (qa.bestAnswer != null) {
                    List<jj_sss_Question_And_Answer__c> answersToQuestion =
                        [select id, jj_sss_Status__c, jj_sss_isMailSent__c,jj_sss_Is_Best_Answer__c, jj_sss_Answer_Posted_By__c,jj_sss_Answer_Posted_By__r.Id,jj_sss_Question_Posted_By__r.Id, jj_sss_Feed_Comment_Id__c
                         from jj_sss_Question_And_Answer__c
                         where jj_sss_Feed_Item_Id__c = :question.jj_sss_Feed_Item_Id__c
                         and jj_sss_Feed_Comment_Id__c != null];
                    jj_sss_Question_And_Answer__c bestAnswer = null;
            } catch(Exception ex) {
                // An Exception occured
                System.debug(logginglevel.ERROR, 'An Exception occured while running the Best Answer job');     (3)
(1)If the feed item is deleted, this will return ‘Null’ and cause an nullpointer exception in (2)
(2)Calling the property .attachment can also return ‘Null’ which is not checked
(3)When logging the error message, the original exception is not mentioned. As such debugging is hard.
(4)The try catch is only around questions, issues with answers will be caught, but stop the processing of other answsers.

seeing 1,2,3,4 can you write the code for me ?
Nilam RoyNilam Roy
Is there anyone who can help me with this ? Thanks in advance
Yogesh KulkarniYogesh Kulkarni
Hi Nilam,

Check if fi is null after line (1) as I can see prolem there also dont catch the exception and see what error you get on the page.

Nilam RoyNilam Roy
Hi @Yogesh
can you write the code for me seeing the points 1,2,3,4. We are getiing a null pointer exception error.