I want to return a empty string when the SomethingName
is null. plants.Something.Name != null ? plants.Something.Name : string.Empty
still gets me a null pointer. I have debugged my application to know this is causing a NullPointerException.
When I set data in the database for SomethingName
, I dont get a null pointer exception.
Is there a better way to handle this?
public FlowerMetaData GetLeafByFlowerId(int flowerId, string flowerName)
{
_flowerContext = _contextUtility.GetFlowerContext(flowerName);
var flowerData = (from flowers in _flowerContext.Flowers
where flowers.FlowerId == flowerId
join plants in _flowerContext.Plants on flowers.PlantId equals plants.PlantId
join leafs in _flowerContext.Leafs on flowers.LeafAk equals leafs.LeafAK
select new FlowerMetaData
{
PlantId = plants.PlantId,
PlantName = plants.PlantName,
FlowerName = FlowerName.ToUpper(),
FlowerNumber = leafs.FlowerNumber,
SomethingName = plants.Something.Name != null ? plants.Something.Name : string.Empty,
CreatedId = plants.CreatedId,
}).FirstOrDefault();
return flowerData;
}
StackTrace
"Message": "An error has occurred.",
"ExceptionMessage": "No flower found with id = 37.",
"ExceptionType": "System.NullReferenceException",
"StackTrace": " at Flower.Services.FlowerService.GetLeafByFlowerId(Int32 flowerId, String flowerName)
at Flower.Controllers.Controllers.FlowerController.GetFlower(Int32 id, String client)\r\n at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Sentinel.Services.AuthFilters.AddChallengeOnUnauthorizedResult.<ExecuteAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
}