+ Start a Discussion
rohit singh 92rohit singh 92 

i am getting this error "System.NullPointerException: Attempt to de-reference a null object" at line no 79 .. can anyone please help me in this ?

@IsTest(SeeAllData=true)
public class FileUploaderTest
{
  @isTest static void testVerifySingleUser()
  {
     FileUploader fu = new FileUploader();
     fu.firstname = 'elias';
     fu.lastname = 'yanni';
     fu.useraddress = '1013 Young Cir';
     fu.usercity = 'Corona';
     fu.userstate = 'CA';
     fu.userpostalcode = '92881';
     test.startTest();
         Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
         HttpResponse res = CalloutClass.getInfoFromExternalService();
         test.stopTest();
        // Verify response received contains fake values
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = res.getBody();
        String expectedValue = '{"foo":"bar"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, res.getStatusCode());
     
  }
  static testMethod void testVerifyAccurateAppendApi()
  {
     test.startTest();
      Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
      HttpResponse res = CalloutClass.getInfoFromExternalService();
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = res.getBody();
        String expectedValue = '{"foo":"bar"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, res.getStatusCode());

     test.stopTest();

  }
  static testmethod void testfileupload(){
        Test.startTest();
        PageReference pageRef = Page.helloworldpage;
        Test.setCurrentPage(pageRef);
        Account ac=new Account();ac.Name='rakuten12345';
        insert ac;
        String resourceName = 'Csvfiles';
        Document document;

        
        
        Document lstDocument = [Select id,name,Body from Document where Name ='csvContacts' ];
        Blob content= lstDocument.Body;
        String myCSVFile = lstDocument.Body.toString();
        System.debug('myCSVFile = ' + myCSVFile);
        FileUploader file=new FileUploader();
        file.contentFile = content;
        file.ReadFile(); 
        file.getuploadedAccounts();
        //file.nameFiles=content.toString();
        String nameFiles= content.toString();
        String[] filelines = nameFiles.split('\n');
        
        for(Integer i=1;i<filelines.size();i++)
        {
        String[] inputvalues = new String[]{};
        inputvalues = filelines[i].split(',');
        
                        Contact a = new Contact();
                        a.FirstName  = 'elias';
                        a.LastName = 'yanni';
                        
                        a.MailingStreet = '301 street';
                        a.MailingCity = 'Corona';
                        a.MailingState = 'CA';
                        a.MailingCountry = 'US';
                        a.MailingPostalCode = 'B12123';
                        
                        accstoupload.add(a);                
             try{
                insert accstoupload;
                System.assertEquals(1,accstoupload.size());
                Test.stopTest();
            }
            catch (Exception e)
            {
                ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template');
                ApexPages.addMessage(errormsg);
            }
        }
  }
        
        
  
  static testMethod void testgetuploadedAccounts()
  {
        test.startTest();
         FileUploader fu = new FileUploader();
         fu.ReadFile();
        test.stopTest();          
  }

}
Sunil MadanaSunil Madana
Hello Rohit, You are adding contacts in Line number 68 to a List (accstoupload) which is not declared. Once the "accstoupload" list is declared, the error should be gone. Thanks.
Abhijeet Anand 6Abhijeet Anand 6
Create and instantiate a List in your method testfileupload as follows:

List<Contact> accstoupload = new List<Contact>();