You need to sign in to do that
Don't have an account?
Attempting to de-reference null object
Hi,
I am getting an error attemptiong to de-reference null object.
Can anyone please guide?
List<Sub_Proj__c> approvals = [select Id FROM
Sub_Proj__c WHERE ID = :projectDetail.Id];
if(approvals.isEmpty())
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.
Severity.ERROR,'Approvals are not present');
ApexPages.addMessage(myMsg);
return null;
}
else
{
if(Projectdeatil.name!=null){
// do rest work
}
}
return returnPage;
}
Hi again
Anil - that will work, but isnt it a little wasteful on your mem allocation?
All Answers
Hi Rajashri.
List<Sub_Proj__c> approvals = [select Id FROM
Sub_Proj__c WHERE ID = :projectDetail.Id];
if(approvals.isEmpty())
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.
Severity.ERROR,'Approvals are not present');
ApexPages.addMessage(myMsg);
return null;
}
else
{
if(Projectdeatil.name!=null){
// do rest work
}
}
return returnPage;
}
why you are writing two return statements with in a single method, if you are writing it will give comiple time error only i.e it won't save.
Hi Rajashri
projectDetail is probably null and you are trying to refference ias ID.
regarding the return statement - Mahi that is not true, because it is inside an IF statement. He might want to exit the meet hod in case he hits that IF.
Thanks for the reply!!
Below is my code.
public PageReference editprj() {
PageReference returnPage = null;
List<Sub_Proj__c> appr = [select Id,Name FROM
Sub_Proj___c WHERE ID = :Detail.Id];
if(appr.isEmpty())
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Approvals are not present');
ApexPages.addMessage(myMsg);
return null;
}
else
{
do rest code here
}
return returnPage;
}
And your getting an error on
List<Sub_Proj__c> appr = [select Id,Name FROM
Sub_Proj___c WHERE ID = :Detail.Id];
?
Than "Detail" is probably null. You should check if its null before referencing its ID, and decide what you want to do if its null.
i'm not sure what it is because its not a local variable of the method.
Sorry, I didn't' see properly, I thought that in else also having the return statement
So the last line is won't be reachable.
Any way thanks for your post
Attempting to de-reference null object error comes when the object is null / not initialized. in this case it seems Detail variable is null.
ublic PageReference editprj()
{
PageReference returnPage = null;
List<Sub_Proj__c> appr=new List<Sub_Proj__c>();
appr = [select Id,Name FROM Sub_Proj___c WHERE ID = :Detail.Id];
if(appr.isEmpty())
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Approvals are not present');
ApexPages.addMessage(myMsg);
return null;
}
else
{
do rest code here
}
return returnPage;
}
IF it solve your issue mark it as solution
Thanks
Hi again
Anil - that will work, but isnt it a little wasteful on your mem allocation?
yes,obviously
we have to first check Detail sobject that whether it is null or not.if it is not null than we can use Detail.ID
Thanks
Hey,Thank you guys..My Object reference was wrong and i corrected it, it works fine now....