You need to sign in to do that
Don't have an account?
Rohan Telang
Plz help me in following Test class as I am getting Error as "System.NullPointerException: Attempt to de-reference a null object"
Hi Developers,
I am getting error in Test class. Kindly help. I am getting error as "System.NullPointerException: Attempt to de-reference a null object".
Stack Trace- Class.EGS_ProductForWebTeam.sendProductData: line 140, column 1
Class.EGS_ProductForWebTeamTestClass.createTestRecord: line 50, column 1
Class.EGS_ProductForWebTeamTestClass.testProduct: line 4, column 1
Below is the Rest service Class and Test Class.
Main Class (REST API)-
@RestResource(urlMapping='/ProductForWebTeam/*')
global class EGS_ProductForWebTeam {
public class ProductData {
public String sf_product_id;
public String name;
public String type;
public Decimal regular_price;
public String description;
public String short_description;
public List<Categories> categories;
public List<Attribute> attributes;
public List<Variation> variations;
}
public class Categories {
public Integer id;
public String name;
}
public class Attributes_Z {
public Integer id;
public String option;
}
public class Attribute {
public Integer id ;
public Boolean variation;
public Boolean visible;
public List<String> options;
}
/* public class Option{
public Integer id;
public String month;
}*/
public class Variation {
public Decimal regular_price;
public List<Attributes_Z> attributes;
}
@httpGet
global static void sendProductData() {
try {
List<ProductData> productWrapperList = new List<ProductData>();
Map<Id, Product2> productMap = new Map<Id, Product2>([SELECT Id, Name, Description, Product_Category__c, Product_Subcategory__c FROM Product2 WHERE Is_sent_to_web__c = false LIMIT 1]);
System.debug(productMap);
Pricebook2 priceBook = [SELECT Id FROM Pricebook2 WHERE Id = '01s0900000DSd7HAAT'];
List<PriceBookEntry> priceBookEntryList = [SELECT Product2Id, UnitPrice
FROM PriceBookEntry
WHERE pricebook2id = :priceBook.Id AND Product2Id =: productMap.keySet()];
Map<Id, PriceBookEntry> MapOfProductToPBE = new Map<Id, PriceBookEntry>();
for(PriceBookEntry pbe : priceBookEntryList)
{
MapOfProductToPBE.put(pbe.Product2Id, pbe);
}
System.debug(MapOfProductToPBE);
List<Product2> productListToBeUpdated = new List<Product2>();
for(Product2 product : productMap.values()) {
ProductData productData = new ProductData();
productData.sf_product_id = product.Id;
productData.name = product.Name;
productData.type = 'variable';
// if(MapOfProductToPBE.get(product.id).Product2id == product.id && MapOfProductToPBE.get(product.id).UnitPrice != null){
productData.regular_price = 100;
// }else{
// productData.regular_price = 0;
// }
productData.description = product.Description;
List<categories> categoryList = new List<categories>();
Categories c = new Categories();
if(product.Product_Category__c == 'Bundle') {
c.id = 60;
c.name = product.Product_Category__c;
} else if(product.Product_Category__c == 'Additional module') {
c.id = 61;
c.name = product.Product_Category__c;
} else if(product.Product_Category__c == 'Stand Alone') {
c.id = 62;
c.name = product.Product_Category__c;
}
categoryList.add(c);
productData.categories = categoryList;
//Code for attribute
List<Attribute> attributeList = new List<Attribute>();
Attribute varAttr = New Attribute();
varAttr.id = 2;
varAttr.variation = true;
varAttr.visible = true;
List<String> OptionList = new List<String>();
OptionList.add('3 Months');
OptionList.add('6 Months');
OptionList.add('1 Year');
varAttr.options = OptionList;
attributeList.add(varAttr);
productData.attributes = attributeList;
List<variation> VariationList = new List<variation>();
for(String i : OptionList)
{
variation v = new variation();
v.regular_price = 100;
List<Attributes_Z> AttrList = new List<Attributes_Z>();
Attributes_Z attrInstance = new Attributes_Z();
attrInstance.id = 2;
attrInstance.option = i;
AttrList.add(attrInstance);
v.attributes = AttrList;
VariationList.add(v);
}
productData.variations = VariationList;
product.Is_sent_to_web__c = true;
productListToBeUpdated.add(product);
productWrapperList.add(productData);
}
// update productListToBeUpdated;
System.debug(JSON.serialize(productWrapperList));
RestResponse res = RestContext.response;
res.statusCode = 200;
res.responseBody = Blob.valueOf(JSON.serialize(productWrapperList));
} catch(Exception e) {
RestResponse res = RestContext.response;
res.statusCode = 404;
res.responseBody = Blob.valueOf('Something went wrong! Error Message : '+e.getMessage()+' '+'line number '+e.getLineNumber()+ ' '+e.getCause());
}
}
}
Test Class-
@isTest
private class EGS_ProductForWebTeamTestClass {
@isTest static void testProduct() {
Product2 varProd= createTestRecord();
// Set up a test request
RestRequest request = new RestRequest();
request.requestUri = 'https://egssolutions--developer.lightning.force.com/services/apexrest/ProductForWebTeam/';
// request.requestUri = '/services/apexrest/ProductForWebTeam/';
request.httpMethod = 'GET';
RestContext.request = request;
// Call the method to test
}
static Product2 createTestRecord() {
// Create test record
Product2 varP = new Product2(
Name = 'Product 1',
Description='Test Description',
Product_Category__c='Bundle',
Product_Subcategory__c='Light',
Is_sent_to_web__c=false);
insert varP;
system.debug('Product '+varP);
Pricebook2 priceBook = new Pricebook2(
Name = 'End User');
insert priceBook;
system.debug('PriceBook '+priceBook);
PriceBookEntry standardPriceBookEntry = new PriceBookEntry(
Product2Id = varP.Id,
pricebook2id = Test.getStandardPricebookId(),
UnitPrice = 2000);
insert standardPriceBookEntry;
system.debug('StandardPriceBookEntry '+standardPriceBookEntry);
PriceBookEntry priceBookEntry = new PriceBookEntry(
UnitPrice = 6000,
pricebook2id=priceBook.id,
Product2Id = varP.Id);
insert priceBookEntry;
system.debug('PriceBookEntry '+priceBookEntry);
test.startTest();
EGS_ProductForWebTeam.sendProductData();
test.stopTest();
return varP;
}
}
I am getting error in Test class. Kindly help. I am getting error as "System.NullPointerException: Attempt to de-reference a null object".
Stack Trace- Class.EGS_ProductForWebTeam.sendProductData: line 140, column 1
Class.EGS_ProductForWebTeamTestClass.createTestRecord: line 50, column 1
Class.EGS_ProductForWebTeamTestClass.testProduct: line 4, column 1
Below is the Rest service Class and Test Class.
Main Class (REST API)-
@RestResource(urlMapping='/ProductForWebTeam/*')
global class EGS_ProductForWebTeam {
public class ProductData {
public String sf_product_id;
public String name;
public String type;
public Decimal regular_price;
public String description;
public String short_description;
public List<Categories> categories;
public List<Attribute> attributes;
public List<Variation> variations;
}
public class Categories {
public Integer id;
public String name;
}
public class Attributes_Z {
public Integer id;
public String option;
}
public class Attribute {
public Integer id ;
public Boolean variation;
public Boolean visible;
public List<String> options;
}
/* public class Option{
public Integer id;
public String month;
}*/
public class Variation {
public Decimal regular_price;
public List<Attributes_Z> attributes;
}
@httpGet
global static void sendProductData() {
try {
List<ProductData> productWrapperList = new List<ProductData>();
Map<Id, Product2> productMap = new Map<Id, Product2>([SELECT Id, Name, Description, Product_Category__c, Product_Subcategory__c FROM Product2 WHERE Is_sent_to_web__c = false LIMIT 1]);
System.debug(productMap);
Pricebook2 priceBook = [SELECT Id FROM Pricebook2 WHERE Id = '01s0900000DSd7HAAT'];
List<PriceBookEntry> priceBookEntryList = [SELECT Product2Id, UnitPrice
FROM PriceBookEntry
WHERE pricebook2id = :priceBook.Id AND Product2Id =: productMap.keySet()];
Map<Id, PriceBookEntry> MapOfProductToPBE = new Map<Id, PriceBookEntry>();
for(PriceBookEntry pbe : priceBookEntryList)
{
MapOfProductToPBE.put(pbe.Product2Id, pbe);
}
System.debug(MapOfProductToPBE);
List<Product2> productListToBeUpdated = new List<Product2>();
for(Product2 product : productMap.values()) {
ProductData productData = new ProductData();
productData.sf_product_id = product.Id;
productData.name = product.Name;
productData.type = 'variable';
// if(MapOfProductToPBE.get(product.id).Product2id == product.id && MapOfProductToPBE.get(product.id).UnitPrice != null){
productData.regular_price = 100;
// }else{
// productData.regular_price = 0;
// }
productData.description = product.Description;
List<categories> categoryList = new List<categories>();
Categories c = new Categories();
if(product.Product_Category__c == 'Bundle') {
c.id = 60;
c.name = product.Product_Category__c;
} else if(product.Product_Category__c == 'Additional module') {
c.id = 61;
c.name = product.Product_Category__c;
} else if(product.Product_Category__c == 'Stand Alone') {
c.id = 62;
c.name = product.Product_Category__c;
}
categoryList.add(c);
productData.categories = categoryList;
//Code for attribute
List<Attribute> attributeList = new List<Attribute>();
Attribute varAttr = New Attribute();
varAttr.id = 2;
varAttr.variation = true;
varAttr.visible = true;
List<String> OptionList = new List<String>();
OptionList.add('3 Months');
OptionList.add('6 Months');
OptionList.add('1 Year');
varAttr.options = OptionList;
attributeList.add(varAttr);
productData.attributes = attributeList;
List<variation> VariationList = new List<variation>();
for(String i : OptionList)
{
variation v = new variation();
v.regular_price = 100;
List<Attributes_Z> AttrList = new List<Attributes_Z>();
Attributes_Z attrInstance = new Attributes_Z();
attrInstance.id = 2;
attrInstance.option = i;
AttrList.add(attrInstance);
v.attributes = AttrList;
VariationList.add(v);
}
productData.variations = VariationList;
product.Is_sent_to_web__c = true;
productListToBeUpdated.add(product);
productWrapperList.add(productData);
}
// update productListToBeUpdated;
System.debug(JSON.serialize(productWrapperList));
RestResponse res = RestContext.response;
res.statusCode = 200;
res.responseBody = Blob.valueOf(JSON.serialize(productWrapperList));
} catch(Exception e) {
RestResponse res = RestContext.response;
res.statusCode = 404;
res.responseBody = Blob.valueOf('Something went wrong! Error Message : '+e.getMessage()+' '+'line number '+e.getLineNumber()+ ' '+e.getCause());
}
}
}
Test Class-
@isTest
private class EGS_ProductForWebTeamTestClass {
@isTest static void testProduct() {
Product2 varProd= createTestRecord();
// Set up a test request
RestRequest request = new RestRequest();
request.requestUri = 'https://egssolutions--developer.lightning.force.com/services/apexrest/ProductForWebTeam/';
// request.requestUri = '/services/apexrest/ProductForWebTeam/';
request.httpMethod = 'GET';
RestContext.request = request;
// Call the method to test
}
static Product2 createTestRecord() {
// Create test record
Product2 varP = new Product2(
Name = 'Product 1',
Description='Test Description',
Product_Category__c='Bundle',
Product_Subcategory__c='Light',
Is_sent_to_web__c=false);
insert varP;
system.debug('Product '+varP);
Pricebook2 priceBook = new Pricebook2(
Name = 'End User');
insert priceBook;
system.debug('PriceBook '+priceBook);
PriceBookEntry standardPriceBookEntry = new PriceBookEntry(
Product2Id = varP.Id,
pricebook2id = Test.getStandardPricebookId(),
UnitPrice = 2000);
insert standardPriceBookEntry;
system.debug('StandardPriceBookEntry '+standardPriceBookEntry);
PriceBookEntry priceBookEntry = new PriceBookEntry(
UnitPrice = 6000,
pricebook2id=priceBook.id,
Product2Id = varP.Id);
insert priceBookEntry;
system.debug('PriceBookEntry '+priceBookEntry);
test.startTest();
EGS_ProductForWebTeam.sendProductData();
test.stopTest();
return varP;
}
}
Greetings!
This error has occurred because your map returns a null value where the entered key does not exist.
On that null object. You are trying to de refer that to a variable.
If you find your Solution then mark this as the best answer.
Thank you!
Regards,
Suraj Tripathi
Please use below code:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Errors- System.NullPointerException: Attempt to de-reference a null object.
Stack Trace-
Class.EGS_ProductForWebTeam.sendProductData: line 140, column 1
Class.EGS_ProductForWebTeamTestClass.createTestRecord: line 51, column 1
Class.EGS_ProductForWebTeamTestClass.testProduct: line 4, column 1
Can you please share you full main class code because what to check on line :140,