You need to sign in to do that
Don't have an account?
Preethi Natarajan
system.NullpointerException:Attempt to de-reference a null object
Hi, I am new to SFDC and i am trying to write a test class .
I am getting system.NullpointerException:Attempt to de-reference a null object. But the Apex class executes successfully.
PFB the details
VF Page:
<apex:page sidebar="false" standardController="CandidateDetails__c" extensions="Thankspageclass" cache="false" expires="0">
<script>
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
history.pushState(null, null, document.URL);
});
</script>
<apex:pageblock >
<div align="center" style="background-color:orange;height:500px;padding-top:200px;font-size:large">
<b>
Dear {!lst[0].Name__c}, Thanks for your participation.
</b>
<br></br>
<br></br>
<b>You will be assessed and contacted shortly.</b>
</div>
</apex:pageblock>
</apex:page>
Apex Class:
public class Thankspageclass {
public String candidateEmail=ApexPages.currentPage().getCookies().get('candidateMail').getValue();
public List<CandidateDetails__c> lst{get;set;}
public Thankspageclass(ApexPages.StandardController controller) {
lst=[SELECT Name__c FROM CandidateDetails__c where Email__c=: candidateEmail];
}
}
Test Class:
@isTest
public class ThankspageclassTest {
public static testmethod void test1(){
CandidateDetails__c cand = new CandidateDetails__c();
cand.Areas_of_Assessments__c ='sales';
cand.Email__c= 'Banu@gmail.com';
cand.Name__c='Banu';
cand.Number_Of_Questions__c= 5;
cand.Participant_Id__c='INF0009';
cand.Phone__c='9876543210';
insert cand;
PageReference myVfPage = Page.ThanksPage;
Test.setCurrentPageReference(myVfPage);
Thankspageclass TP = new Thankspageclass(new ApexPages.StandardController (cand));
TP.candidateEmail = cand.Email__c;
}
}
Please let me know where i am going wrong.
I am getting system.NullpointerException:Attempt to de-reference a null object. But the Apex class executes successfully.
PFB the details
VF Page:
<apex:page sidebar="false" standardController="CandidateDetails__c" extensions="Thankspageclass" cache="false" expires="0">
<script>
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
history.pushState(null, null, document.URL);
});
</script>
<apex:pageblock >
<div align="center" style="background-color:orange;height:500px;padding-top:200px;font-size:large">
<b>
Dear {!lst[0].Name__c}, Thanks for your participation.
</b>
<br></br>
<br></br>
<b>You will be assessed and contacted shortly.</b>
</div>
</apex:pageblock>
</apex:page>
Apex Class:
public class Thankspageclass {
public String candidateEmail=ApexPages.currentPage().getCookies().get('candidateMail').getValue();
public List<CandidateDetails__c> lst{get;set;}
public Thankspageclass(ApexPages.StandardController controller) {
lst=[SELECT Name__c FROM CandidateDetails__c where Email__c=: candidateEmail];
}
}
Test Class:
@isTest
public class ThankspageclassTest {
public static testmethod void test1(){
CandidateDetails__c cand = new CandidateDetails__c();
cand.Areas_of_Assessments__c ='sales';
cand.Email__c= 'Banu@gmail.com';
cand.Name__c='Banu';
cand.Number_Of_Questions__c= 5;
cand.Participant_Id__c='INF0009';
cand.Phone__c='9876543210';
insert cand;
PageReference myVfPage = Page.ThanksPage;
Test.setCurrentPageReference(myVfPage);
Thankspageclass TP = new Thankspageclass(new ApexPages.StandardController (cand));
TP.candidateEmail = cand.Email__c;
}
}
Please let me know where i am going wrong.
Right way of doing it is to initialize the array in Constructor.
Or, you can do it this way in your test class.
Replace the following code.. with the following.
All Answers
Are you able to access value on VF page if not put this LOC. and check your code is able to read the value from VF Page and then run your SOQL in the quesry editor because may be you are returning empty list to the VF Page.
if it make sens please let me know
CandidateEmail is getting value from Cookie candidateMail
Sorry. I missed part of the code.
Here is the code. Add these lines after the following line of code in your Test Class.
These lines will set the cookie for the test call.
It worked. Thanks a lot.
I am getting same error in another test class.Please find the details below.
VF Page:
<apex:page sidebar="false" standardController="CandidateDetails__c" extensions="CandidateEntry1" docType="html-5.0">
<script>
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
history.pushState(null, null, document.URL);
});
</script>
<apex:form >
<apex:pageBlock title="Candidate Details" >
<apex:PageMessages />
<apex:pageblockSection >
<apex:inputField value="{!candidate.Name__c}" label="Candidate's Name" required="true"/>
<apex:input type="text" value="{!participantId}" label="Participant_Id" required="true"/>
<apex:input type="email" label="Email" value="{!email}" required="true"/>
<apex:inputField value="{!candidate.Phone__c}"/>
<apex:inputField value="{!candidate.Test_date__c}"/>
</apex:pageblockSection>
<apex:pageBlockSection title="Assessment Area">
<!--<apex:input type="number" label="No of Questions" value="{!noofquestions}" required="true"/>-->
<apex:selectList size="" value="{!noofquestions}" label="No of Questions " required="true">
<!--<apex:selectoption itemLabel="--None--" itemValue="--None--"></apex:selectoption>-->
<apex:selectoption itemLabel="20" itemValue="20"></apex:selectoption>
<apex:selectoption itemLabel="05" itemValue="05"></apex:selectoption>
<apex:selectoption itemLabel="10" itemValue="10"></apex:selectoption>
<apex:selectoption itemLabel="15" itemValue="15"></apex:selectoption>
<apex:selectoption itemLabel="25" itemValue="25"></apex:selectoption>
<apex:selectoption itemLabel="30" itemValue="30"></apex:selectoption>
</apex:selectList>
<apex:selectList id="area1" size="" value="{!area1}" multiselect="true" label="Area of Assessment" required="true">
<!--<apex:selectoption itemLabel="--None--" itemValue="--None--"></apex:selectoption>-->
<apex:selectoption itemLabel="Sales" itemValue="Sales"></apex:selectoption>
<apex:selectoption itemLabel="Service" itemValue="Service"></apex:selectoption>
<apex:selectoption itemLabel="Marketing" itemValue="Marketing"></apex:selectoption>
<apex:selectoption itemLabel="Force.com Platform" itemValue="Force.com Platform"></apex:selectoption>
</apex:selectList>
</apex:pageBlockSection>
<apex:pageblockButtons >
<apex:commandButton value="Save" action="{!Save11}" />
</apex:pageblockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Class:
public class CandidateEntry1 {
public List<string> area1{get;set;}
public Integer count=0;
public Integer noofquestions{set;get;}
public String email{get;set;}
public CandidateDetails__c candidate{get;set;}
public Pagereference page1=null;
public Pagereference start=null;
public String participantId{get;set;}
public SET<String> Pid=new Set<string>();
public CandidateEntry1(ApexPages.StandardController controller){
candidate=new CandidateDetails__c();
}
//This method is used to save the record values which are given as input by the recruiter
public pageReference save11()
{
for(String s:area1)
{
if(candidate.Areas_of_Assessments__c==null)
{
candidate.Areas_of_Assessments__c=s+';';
}
else
{
candidate.Areas_of_Assessments__c+=s+';';
}
}
if (candidate.Areas_of_Assessments__c.endsWith(';')) {
candidate.Areas_of_Assessments__c= candidate.Areas_of_Assessments__c.substring(0, candidate.Areas_of_Assessments__c.length() - 1);
}
candidate.Number_Of_Questions__c=noofquestions;
candidate.Email__c=email;
candidate.Participant_Id__c =participantId;
if(area1.size()==1)
{
count=[SELECT count() from QuestionBank__c where Cloud_Type__c=:area1[0]];
}
else
{
for(String str:area1)
{
Integer val=[SELECT count() from QuestionBank__c where Cloud_Type__c=:str];
count+=val;
}
}
for(candidateDetails__c c:[select Participant_Id__c from CandidateDetails__c])
{
pId.add(c.Participant_Id__c );
}
if(pId.contains(participantId)) //Validation for Participant Id
{
ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, 'Participant Id already exist !!!' );
ApexPages.addMessage(msg);
}
else if(candidate.Test_date__c<date.Today()) //Validation for Test Date
{
ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, 'Test Date cannot be in past' );
ApexPages.addMessage(msg);
}
else if(noofquestions>count) //Validation for Number of Questions
{
ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, 'No of Questions field exceeded the available questions' );
ApexPages.addMessage(msg);
}
else
{
insert candidate;
Cookie areaOfScopes= new Cookie('areaOfScopes',candidate.Areas_of_Assessments__c,null,315569260,false);
Cookie noOfQuestion= new Cookie('noOfQuestion',noofquestions+'',null,315569260,false);
Cookie candidateMailID= new Cookie('candidateMailID',email,null,315569260,false);
start=new PageReference('/apex/startTest');
start.setCookies(new Cookie[]{areaOfScopes,noOfQuestion,candidateMailID});
start.setRedirect(true);
}
return start;
}
}
Test Class:
@isTest
public class CandidateEntry1Test
{
public static testmethod void test1(){
CandidateDetails__c CD = new CandidateDetails__c();
CD.Areas_of_Assessments__c ='Sales;Marketing';
CD.Email__c = 'pavithra@gmail.com';
CD.Name__c ='pavithra';
CD.Number_Of_Questions__c =5;
CD.Participant_Id__c ='INF00001';
CD.Phone__c = '2389123456';
insert CD;
CandidateEntry1 CE = new CandidateEntry1(new ApexPages.StandardController(CD));
CE.area1[0] ='Sales';
CE.area1[1] ='Marketing';
CE.noofquestions =5;
CE.email ='pavan@gmail.com';
CE.participantId ='INF00002';
CE.save11();
}
}
Please let me know where i am going wrong.
Where exactly are you seeing error in the new class you have posted?
I can see that you have multiple IF ELSE conditions in your APEX class.
You may need multiple test records to be created to meet all the conditions in code coverage.
And, please point me out the line of code and the actual error.
I am getting system.NullpointerException:Attempt to de-reference a null object error in theline CE.area1[0] ='Sales'; of test class
You can haven't initialized your Array.
Add it to your constructor.
Your CandidateEntry1 constructor will look like this now.
And, the following two lines in your test class should be written differently.
Replace with this... Let me know how it goes.
When you declared any public attributes (properties) of complex data type (Object type or Array, List, etc.) in your class, you need to initialize them in your Constructor.
Look at the above constructor code in your APEX class and the one I have provided in my previous response. You will understand the difference.
Right way of doing it is to initialize the array in Constructor.
Or, you can do it this way in your test class.
Replace the following code.. with the following.
It worked. Thanks for the hint.