You need to sign in to do that
Don't have an account?

Attempt to de-reference a null object Error is in expression '{!go}' in component <apex:commandButton> in page createcase: Class.assign_1_Controller.go
I have Visualforce page with below code
<apex:page controller="assign_1_Controller">
<apex:pageBlock >
<apex:form >
<apex:pageBlockSection columns="1">
<apex:inputField value="{!newCase.ContactId}" label="Search Contact">
</apex:inputField>
<apex:commandButton value="Go" title="Go" action="{!go}"/>
<!-- <apex:commandButton value="Add New Contact" title="Add New Contact" action="{!addNewContact}"/> -->
</apex:pageBlockSection>
</apex:form>
</apex:pageBlock>
</apex:page>
and
Controller
public Contact contactbyLookup{get;set;}
public PageReference go(){
PageReference p = new PageReference('/apex/Casetabsearchassignment');
if(newCase.ContactId != null){
contactByLookup = [select id,
Name,
Business_Vertical__c
from Contact
where id =: newCase.ContactId];
}
return p;
}
<apex:page controller="assign_1_Controller">
<apex:pageBlock >
<apex:form >
<apex:pageBlockSection columns="1">
<apex:inputField value="{!newCase.ContactId}" label="Search Contact">
</apex:inputField>
<apex:commandButton value="Go" title="Go" action="{!go}"/>
<!-- <apex:commandButton value="Add New Contact" title="Add New Contact" action="{!addNewContact}"/> -->
</apex:pageBlockSection>
</apex:form>
</apex:pageBlock>
</apex:page>
and
Controller
public Contact contactbyLookup{get;set;}
public PageReference go(){
PageReference p = new PageReference('/apex/Casetabsearchassignment');
if(newCase.ContactId != null){
contactByLookup = [select id,
Name,
Business_Vertical__c
from Contact
where id =: newCase.ContactId];
}
return p;
}
In your SOQL you have where condition, Where trying to get newCase.ContactId. But your newCase is having NULL value.
Replace your IF condition like:
if(newCase != null && newCase.ContactId != null)
It will avoid your Exception.
Let me kknow if it will work for you.
Mark ur Ans to help others
Thanks
Niraj
All Answers
From where you are getting record in newCase. Please confirm that this variable have value beacause you are getting Attempt to de-reference a null object Error which may be due to this null variable.
In your SOQL you have where condition, Where trying to get newCase.ContactId. But your newCase is having NULL value.
Replace your IF condition like:
if(newCase != null && newCase.ContactId != null)
It will avoid your Exception.
Let me kknow if it will work for you.
Mark ur Ans to help others
Thanks
Niraj
How ever the value was not coming so I initialize the variable like newCase = new Case();