
Everything works fine during a normal callout, and now I'm trying to build a test class for it.However, i get the following error when running the test class, I get this error: null argument for JSONGenerator.writeStringField().This error occurs on the line gen.writeStringField('fileNumber', fileNumber); I put a debug on the variable 'fileNumber' and a null check, shows that ==null is false.public static boolean UpdateCIACRR(id RateId, String Stage, String Status, String PA, String DocId, String comments, id contact, Boolean isFuture) {
string rateCreated;
string fileNumber;
string comment = (comments == null) ? '' : comments;
string PriceAuthority = (PA == null) ? '' : PA;
if (DocId == null) {
fileNumber = '';
rateCreated = 'No';
} else {
fileNumber = DocId;
rateCreated = 'Yes';
}
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('rateId', rateId);
gen.writeStringField('rateCreated', rateCreated);
gen.writeStringField('priceAuthority', priceAuthority);
System.debug('null?' + fileNumber);
System.debug(fileNumber==null);
gen.writeStringField('fileNumber', fileNumber);
gen.writeStringField('comments', comment);
gen.writeStringField('status', status);
gen.writeEndObject();
}
Here's the code in the test class:
Even if the value DocId__c is null, it should have been replace by the '' empty string. Any idea what could be the cause?rate__c TestRate = [SELECT id, Stage__c, Status__c, Price_Authority__c, DocId__c, comments__c, contact__c FROM rate__c LIMIT 1];
boolean result = UpdateCIA_CRR.UpdateCIACRR(TestRate.id, TestRate.Stage__c,TestRate.Status__c,TestRate.Price_Authority__c,TestRate.DocId__c,TestRate.comments__c,TestRate.contact__c,true);
System.assertEquals(false, result);
Btw, i ran the following method in the Anononymes window and it returns fine.
UpdateCIA_CRR.UpdateCIACRR(rateid, '1', '1', null, '123','testing',contactid, true);
Hi,Review below link which can help you.https://salesforce.stackexchange.com/questions/231062/null-argument-for-jsongenerator-writestringfieldThanks,