0

I am seeing

System.NullPointerException: Argument cannot be null

in my test class

Below is the controller method that i am trying to cover :

public void Synchsongmethod()

{
    system.debug('synch song method is called');
    List<Cue_Sheet_Sequence__c> recordToVerify=new List<Cue_Sheet_Sequence__c>();
    List<Cue_Sheet_Assignment__c> recordToVerify1=new List<Cue_Sheet_Assignment__c>();
    List<Cue_Sheet_Assignment__c> recordToVerify2=new List<Cue_Sheet_Assignment__c>();
    List<SongCpaJunction__c >songcpalist=new List<SongCpaJunction__c>();
    List<Cue_Sheet_Assignment__c>finallist=new List<Cue_Sheet_assignment__c>();
    set<id> songid= new Set<id>();
    Id composerId, publisherId,ArtistId;

    Id seqIdsynch =Id.ValueOf(sid); // <= getting the error here

    system.debug('seqIdsynch'+sid);
    Cue_Sheet_Sequence__c newRecord =[Select id,song__c,Flagged_for_Delete__c,RapidCue_Delete_Timestamp__c, Verified__c,(Select Id, Flagged_for_Delete__c, Copyright_Holder__c From Cue_Sheet_Sequence_References__r ) from Cue_Sheet_Sequence__c where id =:seqIdsynch and Sequence_Order_No__c!=0];
    system.debug('newRecord '+newRecord );

    // for(Cue_Sheet_Sequence__c record:newRecord){
    newRecord.IsSynch__c=true;
    songid.add(newRecord.song__c);
            recordToVerify.add(newrecord);
    //}
    update recordToVerify;

I am seeing error at this line :

Id seqIdsynch =Id.ValueOf(sid);

Below is my test class:

Static void createData(){
    DataBaseForTest.DataBaseMethod();
    songData =DataBaseForTest.songData ;
    scJunList =DataBaseForTest.scJunList;
    songSeq =DataBaseForTest.songSeq ;
    cpaObjList = DataBaseForTest.cpaObjList;
    songSeq= DataBaseForTest.songSeq;
    cueSheet=DataBaseForTest.caseObj;
    assignList=DataBaseForTest.assignList;
    seg=DataBaseForTest.seg;
    List<String> fR=new List<string>();
    fR.add('a');

    sList.add(cueSheet.Episode__r.show__r);

    seqList.add(songSeq);
    segList.add(seg);

}
static  void updateData(){
    createData();

    Id sid=songseq.id;
    List<Cue_Sheet_Sequence__c> seqList2 =[Select id,song__c,Flagged_for_Delete__c,RapidCue_Delete_Timestamp__c, Verified__c,(Select Id, Flagged_for_Delete__c, Copyright_Holder__c From Cue_Sheet_Sequence_References__r ) from Cue_Sheet_Sequence__c where id =:sid and Sequence_Order_No__c!=0];
    List<Cue_Sheet_Sequence__c> seqListNew1= new List<Cue_Sheet_Sequence__c>();
    List<Cue_Sheet_Assignment__c> assignList2 = new List<Cue_Sheet_Assignment__c>();
    Id cpaAssignRecordTypeId0 = Schema.SObjectType.Cue_Sheet_Assignment__c.getRecordTypeInfosByName().get('Composer').getRecordTypeId();    
    Id cpaAssignRecordTypeId2 = Schema.SObjectType.Cue_Sheet_Assignment__c.getRecordTypeInfosByName().get('Publisher').getRecordTypeId();    

    for(Cue_Sheet_Sequence__c sq:seqList2){
        assignList2.add(new Cue_Sheet_Assignment__c(Flagged_for_Delete__c= false,RecordTypeId=cpaAssignRecordTypeId ,first_name__c='test name',last_NAme__C='last42',society__c='a', Cue_Sheet_Sequence__c=sq.id, Percentage__c = 50.0) );
        assignList2.add(new Cue_Sheet_Assignment__c(Flagged_for_Delete__c= false,RecordTypeId=cpaAssignRecordTypeId1 ,first_name__c='test name',last_NAme__C='last41',society__c='a', Cue_Sheet_Sequence__c=sq.id, Percentage__c = 50.0) );
        sq.Cue_Usage__c='BI';
        sq.No_of_Occurances__c=3;
        sq.Duration__c='12:12';
        sq.verified__c=True;
    }

    insert assignList2;
    update seqList2;


}

static testMethod void InsertMethod4(){

    updateData();
    Test.setCurrentPageReference(new PageReference('Page.CueSheetEdit')); 
    System.currentPageReference().getParameters().put('id', cueSheet.id);
    System.currentPageReference().getParameters().put('Add', '1');
    System.currentPageReference().getParameters().put('seqId', songSeq.id);
    System.currentPageReference().getParameters().put('verifyStatus', 'Verify');
    System.currentPageReference().getParameters().put('seqIdVerify', '0');



    CueSheetEditController obj1=  new CueSheetEditController (new 
          ApexPages.StandardController(cueSheet));
    obj1.verifySong();
    obj1.approveUpdateSheet();
    obj1.scheduleRapidcue();
    obj1.Synchsongmethod();
}

Can anyone please point out where am i going wrong?

  • At the line where you are getting the error the variable sid is null, therefore the error is that this argument is not valid for the method Id.valueOf that tries to convert the parameter to a valid Salesforce Id. Your issue is that the Id is null, probably because the record is not inserted on the database or you didn't retrieve its value after inserting it. – Renato Oliveira Oct 22 '17 at 12:24
  • Thank you for your response , I tried to pass the value of sid to the value function but it is always giving me the null error , I am not sure if i am doing the right way , Can you please take a look , – Natasha Oct 22 '17 at 12:51
  • 2
    If sid is a property of your controller, then you need to find where that id is created by the database (when a record is inserted). If it is not, then that is your issue. Please post your entire controller code so we can take a look. – Renato Oliveira Oct 22 '17 at 13:04
  • Sid is a propert of the controller and below is the code where the insert of the record happens that creates sid : Its in databasefortest class :songSeq = new Cue_Sheet_Sequence__c(Cue_Sheet_Segment__c = seg.id,Hit_Time__c='0:0:0',Duration__c='00:00',Out_Time__c='0:0:0',Sequence_Order_No__c=1,Song_Title_Entry__c='Test Song', Song__c = Null,Verified__c=False); insert songSeq; – Natasha Oct 22 '17 at 13:16
  • 3
    Well, the error is pretty straightforward. Renato Oliviera has already covered the issue. Your sid variable in your controller is null. You should edit your question to include the code that declares sid and the code that sets that variable. You mentioned that you don't know how to ensure that sid gets a value, well...neither do we unless you share the code that assigns some value to that variable. – Derek F Oct 22 '17 at 15:38
0

Try this way:

Id seqIdSynch = ( Id )sid;

or also check before casting it that sid is not null:

if( sid == null ){ return; }

if this is a private controller property, make it testVisible using:

@testVisible

and be sure to set it in your test method.

if this is being pulled from a field on an object - be sure it is also not null before trying to cast it.

Hope that helps. I don't see all of the code to make a solid answer - just some basic assumptions by what i can see.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.