I have an endpoint:
http://localhost:5235/ProductsEF/GetSubstances/ApapExtra
where I get the result, for example /ApapExtra
:
{
"productId": 4,
"productName": "ApapExtra",
"substance1": "Paracetamolum",
"substance2": "Coffeinum",
"substance3": null,
"substance4": null
}
And I have another endpoint:
http://localhost:5235/SubstancesEF/GetSubstanceDataByName/Paracetamolum
where I get the data like, for example /Paracetamolum
:
{
"substanceId": 4,
"name": "Paracetamolum",
"effect": "Analgesic"
}
I want to combine them in one method. I want to create a method:
[HttpGet("GetSubstances/{ProductName}")]
public Product GetSubstancesFromProduct(string ProductName)
{
Product product = _productRepository.GetProductDataByName(ProductName);
if (product == null)
{
throw new Exception("Could not find the product!");
}
List<Substance> substanceInfos = new List<Substance>();
// string[] substanceNames = new string[] { product.Substance1, product.Substance2, product.Substance3, product.Substance4 };
Console.WriteLine($"ProductName: {product.ProductName}");
return product;
}
This line:
Product product = _productRepository.GetProductDataByName(ProductName);
leads to first endpoint. The logic I want to implement:
- Send request to this endpoint with drug name
- Drug is being unfolded into substances.
- For all the substances, the request is sent to get the info about the substance
- The list of info about substances is returned.
So I sent request /ApapExtra
and I want to get:
{
"substanceId": 4,
"name": "Paracetamolum",
"effect": "Analgesic"
}
{
"substanceId": 4,
"name": "Coffeinum",
"effect": "Analgesic"
}
because it consists of these ingredients.
The problem is with:
string[] substanceNames = new string[] { product.Substance1, product.Substance2, product.Substance3, product.Substance4 };
because I get:
Possible null reference assignment.CS8601
even though I have in models ?
next to the field names.
How to fix this problem? Or how to solve it in a better way?
1