+ Start a Discussion
keerthana chowdharykeerthana 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
Parth ThakkarParth Thakkar
List<String> strConIds = new  List<String>();
strConIds  = ContactIds.split(',');
try this
keerthana chowdharykeerthana chowdhary
i tried it facing same error
Pramodh KumarPramodh Kumar

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.
keerthana chowdharykeerthana chowdhary
     List<String> strConIds= ContactIds.split(',');
            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;
Pramodh KumarPramodh Kumar
before calling the Savedoc method. add the 

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.
keerthana chowdharykeerthana chowdhary
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name, Body]: [Name, Body]
keerthana chowdharykeerthana chowdhary
yeah i got it 90 % code coverage
 
Pramodh KumarPramodh Kumar
you are missing some  manadatory fields. 

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.
keerthana chowdharykeerthana chowdhary
Let me know the best blogs to google the content pRAMODH
Pramodh KumarPramodh Kumar
There are so many blogs in the internet.

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.
 
Pramodh KumarPramodh Kumar
Please mark as a "Best Answer" if your issue is resolved.


Thanks,
Pramodh