function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BigThorBigThor 

Attempt to de-reference a null object

Hello,

 

I have the next error in VF

 

Attempt to de-reference a null object

Error is in expression '{!iniQuestionsResponsesLB}' in component <apex:page> in page deal_questions_and_responses_lb

An unexpected error has occurred. Your development organization has been notified.     

 

My class is:

 

/**
* Victor
 */
@isTest
public without sharing class QuestionsResponsesLB {
    
    List<Deal_Screening_Question_Response__c> questions;
    List<Deal_Screening_Questions__c> origiQuestions;
    List<Deal_Screening_Question_Response__c> conRespuestas;
    public String sId {get; set;}
    public Boolean inEdit {get; set;}
    
    /*
    *
    */
    public List<Deal_Screening_Question_Response__c> getQuestions(){
        PageReference p = ApexPages.currentPage();
        sId = p.getParameters().get('id');
        
        Opportunity myOppo = [SELECT Deal_Category__c FROM Opportunity WHERE Id = :sId];
        
        questions = [SELECT Category__c,Concept__c,Country__c,language__c,Name,Publish__c,Question_Number__c,Question__c,Sub_Category_2__c,Sub_Category__c FROM Deal_Screening_Question_Response__c WHERE Category__c = :myOppo.Deal_Category__c ORDER BY Question_Number__c];
        return questions;
    }
    
    /*
    *
    */
    public void iniQuestionsResponsesLB(){
        PageReference p = ApexPages.currentPage();
        sId = p.getParameters().get('id');
        
        conRespuestas = [SELECT Category__c,Concept__c,Country__c,language__c,Name,Publish__c,Question_Number__c,Question__c,Sub_Category_2__c,Sub_Category__c FROM Deal_Screening_Question_Response__c WHERE Opportunity__c = :sId];
        if(questions.size() > 0){ //Tenemos respuestas por lo que las mostramos
        	inEdit = true;
        }
        else{//Caso en el que no tenemos respuestas
        	String cat;
        	String idioma;
        	
        	Opportunity myOppo = [SELECT Deal_Category__c FROM Opportunity WHERE Id = :sId];
        	origiQuestions = [SELECT Category__c,Concept__c,Country__c,language__c,LB__c,Name,Publish__c,Question_Number__c,Question_Type__c,Question__c,Sub_Category_2__c,Sub_Category__c FROM Deal_Screening_Questions__c WHERE Category__c = :myOppo.Deal_Category__c ORDER BY Question_Number__c];
        	
        	if(origiQuestions.size() > 0){//Si para esta categoria tenemos preguntas entonces las gruardamos en la tabla preguntas/respuestas
        		//Creamos una lista de preguntas/respuestas
        		List<Deal_Screening_Question_Response__c> forInsert = new List<Deal_Screening_Question_Response__c>();
        		//Por cada una de las preguntas crearemos su pregunta respuesta
        		for(Deal_Screening_Questions__c q : origiQuestions){
        			forInsert.add(new Deal_Screening_Question_Response__c(Question_Number__c = q.Question_Number__c,Category__c = q.Category__c,Sub_Category__c = q.Sub_Category__c,Question__c = q.Question__c,Opportunity__c = sId,language__c = q.language__c,Publish__c = q.Publish__c));
        			if(forInsert.size() > 0){
        				insert forInsert;
        			}
        		}
        	}
        	else{
        		inEdit = false;
        	}
        }
    }
    
    
    /*
    *
    */
    public PageReference CloseUp(){
        PageReference pr = New PageReference('/'+ sId);
        pr.setRedirect(true);
        return pr;
    }
}

 My page is

 

<apex:page controller="QuestionsResponsesLB" tabstyle="Opportunity" action="{!iniQuestionsResponsesLB}" showHeader="true" sidebar="true">
    <apex:pageBlock title="Questions">
    <apex:form >
        <apex:pageBlockSection columns="1" rendered="true">
            <apex:pageblocktable value="{!Questions}" var="question" id="list">
                <apex:column headerValue="Preguntillas">
                    <apex:outputText value="{!question.Question__c}"/>
                </apex:column>
                <apex:column headerValue="Respuestas">
                    <apex:inputText size="100"/>
                </apex:column>
            </apex:pageblocktable>
        </apex:pageBlockSection>
        <apex:pageBlockSection >
            <apex:commandButton value="Cancel" action="{!closeUp}"/>
        </apex:pageBlockSection>
    </apex:form>
    </apex:pageBlock>
</apex:page>

  When I remove action={!iniQuestionsResponsesLB} the page work well

 

Whats is happening ? Any idea of the error ?

 

Thanks for all

 

Best Answer chosen by Admin (Salesforce Developers) 
Jake GmerekJake Gmerek

One of your Querys in the iniQuestionsResponsesLB method is not returning data and when you reference the results nothing is there and it is throwing an error.  You can look in the debug log and see which query it is.

All Answers

Jake GmerekJake Gmerek

One of your Querys in the iniQuestionsResponsesLB method is not returning data and when you reference the results nothing is there and it is throwing an error.  You can look in the debug log and see which query it is.

This was selected as the best answer
BigThorBigThor

Hi Jake,

 

thanks for you answerd, I modify the line if(questions.size  > 0) by if(questions != null) and functions works

 

When I have another problem, in the same code, in the line insert forInsert I have this message error

 

Insert failed. First exception on row 0 with id a0IJ0000001Msg6MAC; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Error is in expression '{!iniQuestionsResponsesLB}' in component <apex:page> in page deal_questions_and_responses_lb

 An unexpected error has occurred. Your development organization has been notified.

 


In the debug log:

 

 

12:02:58.149 (149699000)|USER_DEBUG|[51]|DEBUG|forInsert: (Deal_Screening_Question_Response__c:{Sub_Category__c=null, Question__c=¿A quien va dirigido el espectáculo?, Category__c=Entertainment, Opportunity__c=006J0000004NPKzIAO, Publish__c=false, Question_Number__c=1, language__c=Spanish})
12:02:58.149 (149710000)|SYSTEM_METHOD_EXIT|[51]|System.debug(ANY)
12:02:58.149 (149724000)|SYSTEM_METHOD_ENTRY|[52]|LIST<Deal_Screening_Question_Response__c>.size()
12:02:58.149 (149756000)|SYSTEM_METHOD_EXIT|[52]|LIST<Deal_Screening_Question_Response__c>.size()
12:02:58.149 (149821000)|DML_BEGIN|[53]|Op:Insert|Type:Deal_Screening_Question_Response__c|Rows:1
12:02:58.206 (206868000)|DML_END|[53]
12:02:58.206 (206956000)|SYSTEM_METHOD_ENTRY|[49]|system.ListIterator.hasNext()
12:02:58.206 (206985000)|SYSTEM_METHOD_EXIT|[49]|system.ListIterator.hasNext()
12:02:58.207 (207168000)|METHOD_ENTRY|[50]|01pJ0000000Dbes|QuestionsResponsesLB.__sfdc_sId()
12:02:58.207 (207193000)|METHOD_EXIT|[50]|01pJ0000000Dbes|QuestionsResponsesLB.__sfdc_sId()
12:02:58.207 (207308000)|SYSTEM_METHOD_ENTRY|[50]|LIST<Deal_Screening_Question_Response__c>.add(Object)
12:02:58.207 (207336000)|SYSTEM_METHOD_EXIT|[50]|LIST<Deal_Screening_Question_Response__c>.add(Object)
12:02:58.207 (207358000)|SYSTEM_METHOD_ENTRY|[51]|String.valueOf(Object)
12:02:58.207 (207470000)|SYSTEM_METHOD_EXIT|[51]|String.valueOf(Object)
12:02:58.207 (207487000)|SYSTEM_METHOD_ENTRY|[51]|System.debug(ANY)
12:02:58.207 (207495000)|USER_DEBUG|[51]|DEBUG|forInsert: (Deal_Screening_Question_Response__c:{Sub_Category__c=null, Question__c=¿A quien va dirigido el espectáculo?, Category__c=Entertainment, Opportunity__c=006J0000004NPKzIAO, Id=a0IJ0000001Msg6MAC, Publish__c=false, Question_Number__c=1, language__c=Spanish}, Deal_Screening_Question_Response__c:{Sub_Category__c=null, Question__c=Añadir link informativo, Category__c=Entertainment, Opportunity__c=006J0000004NPKzIAO, Publish__c=false, Question_Number__c=1, language__c=Spanish})
12:02:58.207 (207511000)|SYSTEM_METHOD_EXIT|[51]|System.debug(ANY)
12:02:58.207 (207523000)|SYSTEM_METHOD_ENTRY|[52]|LIST<Deal_Screening_Question_Response__c>.size()
12:02:58.207 (207534000)|SYSTEM_METHOD_EXIT|[52]|LIST<Deal_Screening_Question_Response__c>.size()
12:02:58.207 (207573000)|DML_BEGIN|[53]|Op:Insert|Type:Deal_Screening_Question_Response__c|Rows:2
12:02:58.237 (237266000)|DML_END|[53]
12:02:58.237 (237400000)|VF_PAGE_MESSAGE|cannot specify Id in an insert call
12:02:58.237 (237538000)|EXCEPTION_THROWN|[53]|System.DmlException: Insert failed. First exception on row 0 with id a0IJ0000001Msg6MAC; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
12:02:58.240 (240078000)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0 with id a0IJ0000001Msg6MAC; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Class.QuestionsResponsesLB.iniQuestionsResponsesLB: line 53, column 1
12:02:58.240 (240095000)|CODE_UNIT_FINISHED|QuestionsResponsesLB invoke(iniQuestionsResponsesLB)

 

 but in the insert I don't specify the Id for each Deal_Screening_Question_Response__c, I look some examples and all don't specify field Id

 

Sorry for all questions, this is my second class and my first VF page and I'm learning

 

Thanks for all !!

 

BigThorBigThor

Hi all,

 

I found the error, I have the sentence insert in sentence for, the code correct is

 

for(Deal_Screening_Questions__c q : origiQuestions){
        			forInsert.add(new Deal_Screening_Question_Response__c(Question_Number__c = q.Question_Number__c,Category__c = q.Category__c,Sub_Category__c = q.Sub_Category__c,Question__c = q.Question__c,Opportunity__c = sId,language__c = q.language__c,Publish__c = q.Publish__c));
        			System.debug('forInsert: '+ forInsert);
        		}
        		if(forInsert.size() > 0){
        			insert forInsert;
        		}

 Thanks all !!! :)