You need to sign in to do that
Don't have an account?
keerthana chowdhary
test class 4 System.NullPointerException: Attempt to de-reference a null object
public class fileAttach{
Public Attachment myfile{get;set;}
Public String VMfileName{get;set;}
public String ContactIds{get;set;}
public Contact con{get;set;}
public List<Contact> contacts{get;set;}
Public Attachment getmyfile()
{
myfile = new Attachment();
return myfile;
}
public fileAttach(ApexPages.StandardController controller) {
ContactIds= ApexPages.currentPage().getParameters().get('array');
}
Public Pagereference Savedoc()
{
if(ContactIds!= null || ContactIds!= ''){
List<String> strConIds= ContactIds.split(',');
for(Id conId:strConIds){
Attachment a = new Attachment(parentId =conId,name=myfile.name, body = myfile.body);
insert a;
}
}
return null;
}
}
test class...
@isTest(seealldata=true)
public class testfileattach
{
static testmethod void testfileattach()
{
Contact con=new Contact(LastName='Test Name',payroll__c='rakhul');
insert con;
Pagereference ref=page.conatactsaved;
ref.getParameters().put('id', String.valueOf(con.id));//for page referenc
Test.setcurrentpage(ref);
ApexPages.StandardController cont = new ApexPages.StandardController(con);
fileAttach fill=new fileAttach(cont);
fill.VMfileName='';
Attachment att=new Attachment();
att=fill.getmyfile();
fill.Savedoc();
}
}
test class passed with 50 %code coverage
but whenever i was calling pagerefence savedoc method getting error that System.NullPointerException: Attempt to de-reference a null object
stack trace
Class.fileAttach.Savedoc: line 24, column 1
Class.testfileattach.testfileattach: line 16, column 1
Public Attachment myfile{get;set;}
Public String VMfileName{get;set;}
public String ContactIds{get;set;}
public Contact con{get;set;}
public List<Contact> contacts{get;set;}
Public Attachment getmyfile()
{
myfile = new Attachment();
return myfile;
}
public fileAttach(ApexPages.StandardController controller) {
ContactIds= ApexPages.currentPage().getParameters().get('array');
}
Public Pagereference Savedoc()
{
if(ContactIds!= null || ContactIds!= ''){
List<String> strConIds= ContactIds.split(',');
for(Id conId:strConIds){
Attachment a = new Attachment(parentId =conId,name=myfile.name, body = myfile.body);
insert a;
}
}
return null;
}
}
test class...
@isTest(seealldata=true)
public class testfileattach
{
static testmethod void testfileattach()
{
Contact con=new Contact(LastName='Test Name',payroll__c='rakhul');
insert con;
Pagereference ref=page.conatactsaved;
ref.getParameters().put('id', String.valueOf(con.id));//for page referenc
Test.setcurrentpage(ref);
ApexPages.StandardController cont = new ApexPages.StandardController(con);
fileAttach fill=new fileAttach(cont);
fill.VMfileName='';
Attachment att=new Attachment();
att=fill.getmyfile();
fill.Savedoc();
}
}
test class passed with 50 %code coverage
but whenever i was calling pagerefence savedoc method getting error that System.NullPointerException: Attempt to de-reference a null object
stack trace
Class.fileAttach.Savedoc: line 24, column 1
Class.testfileattach.testfileattach: line 16, column 1
strConIds = ContactIds.split(',');
try this
First thing compiler will not allow you to save the class.
there are some syntax errors
List<String> strConIds is type "string" but in the for loop you are using "ID".
for(Id conId:strConIds) it should be for(string conId:strConIds){
In the Test Class you are not providing any values ContactIds which is actually a null.
Just give any string dummy contact ids to the "ContactIds"
I think this should work
Let me know if you have any problem.
Thanks,
pRAMODH.
for(string conId:strConIds){
Attachment a = new Attachment(parentId =conId,name=myfile.name, body = myfile.body);
insert a
these lines are not covering please any help;
ContactIds = con.id;
and always use list when you make any DML statement or you may runout the governor limits.
let me know if you have any issues
Thanks,
pRAMODH.
when you wrire test class or inserting any records without having mandatory fields it will through that message error message.
use something like this or refer to those links
Blob b = Blob.valueOf('Test Data');
Attachment attachment = new Attachment();
attachment.ParentId = parentId;
attachment.Name = 'Test Attachment for Parent';
attachment.Body = b;
http://www.fishofprey.com/2011/10/inserting-attachment-in-apex-test.html
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_attachment.htm
let me know if you have any issues.
Thanks and good night.
I like
http://blog.jeffdouglas.com
http://www.oyecode.com/
sfdc99
http://www.jitendrazaa.com/blog/
if you have any questions drop me an email @ pramodh.thammishetty@gmail.com.
Thanks,
pRAMODH.
Thanks,
Pramodh