1

I am receiving an error and my code is highlighting this function to be faulty

public string GetConnectionString(string name)
{
    return ConfigurationManager.ConnectionStrings[name].ConnectionString;
}

the exact error is:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

System.Configuration.ConnectionStringSettingsCollection.this[string].get returned null.

I'm trying to access the "product" data on the api by going api/product, and this error pops up for me. This is the error on the web page

<ExceptionMessage>
Object reference not set to an instance of an object.
</ExceptionMessage>

The whole error message on the webpage is this

<StackTrace>
at RMPwebapi.Library.Internal.DataAccess.SqlDataAccess.GetConnectionString(String name) in C:\Users\Gamer\Desktop\The mighty project\retail-manager-project\RMPwebapi.Library\Internal\DataAccess\SqlDataAccess.cs:line 17 at RMPwebapi.Library.Internal.DataAccess.SqlDataAccess.LoadData[T,U](String storedProcedure, U parameters, String connectionStringName) in C:\Users\Gamer\Desktop\The mighty project\retail-manager-project\RMPwebapi.Library\Internal\DataAccess\SqlDataAccess.cs:line 23 at RMPwebapi.Library.DataAccess.ProductData.GetProducts() in C:\Users\Gamer\Desktop\The mighty project\retail-manager-project\RMPwebapi.Library\DataAccess\ProductData.cs:line 16 at RMPwebapi.Controllers.ProductController.Get() in C:\Users\Gamer\Desktop\The mighty project\retail-manager-project\RMPwebapi\Controllers\ProductController.cs:line 19 at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.<GetExecutor>b__2(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) 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.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()
</StackTrace>

I tried to check the difference classes and identify where I could be going wrong because I suspect I'm passing on an object that is null. At the moment I'm analysing this function in my ProductData.cs file:

public List<ProductModel> GetProducts()
{
    SqlDataAccess sql = new SqlDataAccess();
    var output = sql.LoadData<ProductModel, dynamic>("dbo.spProduct_GetAll",
         new { }, "RMPdatabase");
    return output;
}

I'm also investigating this function within my ProductController.cs file:

public List<ProductModel > Get()
{
    ProductData data = new ProductData();

    return data.GetProducts();      
}

What can I do guys, please help. I would like to say I am a beginner so I won't understand everything

CEH
  • 5,701
  • 2
  • 16
  • 40
meniman98
  • 195
  • 1
  • 13
  • 1
    The best way to debug these is to actually debug :) Start debugging in Visual Studio. It will break on the line with the exception, and then you can look for variables that are `null` that you didn't expect to be. – Gabriel Luci Nov 19 '19 at 20:12
  • The exception was thrown on line 17 of SqlDataAccess.cs. So look there. – Gabriel Luci Nov 19 '19 at 20:13

0 Answers0