0

I have created following table from DynamoDB service in Amazon Web Services:

enter image description here

which I want to display on an ASP.NET application. The problem is that it returns a NullReferenceException, and I can't event debug it. I have created a ViewModel as follows:

  public class DynamoDB
  {
    public int userNO { get; set; }

    public string firstName { get; set; }

    public string lastName { get; set; }                
  }

and an extra class to access the DynamoDB table from AWS:

public class DynamoContext 
{
    Credentials.Credentials credentials = new Credentials.Credentials();

    public IEnumerable<DynamoDB> GetProducts()
    {
        IEnumerable<DynamoDB> products = null;
        RegionEndpoint region = RegionEndpoint.GetBySystemName("eu-central-1");
        AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials.AccessKey, credentials.SecretAccessKey, region);

        DynamoDBContext context = new DynamoDBContext(client);
        products = context.Scan<DynamoDB>();

        return products;
    } 
}

and a controller which has to access this IEnumerable and convert it to a List of objects.

public class DynamoController : Controller
{
    //
    DynamoContext dc = new DynamoContext();
    [HttpGet]
    public ActionResult Index()
    {
        List<DynamoDB> objList = dc.GetProducts().ToList();
        return View("users", objList);
    }
}

and finally my View which has to display the table on the Index page:

@{
ViewBag.Title = "Home Page";
}

@model List<FinalApplication.Models.DynamoDB>

<div class="jumbotron">
    <h1>Home Monitoring Application</h1>
</div>

<table>

    @foreach (FinalApplication.Models.DynamoDB objUser in Model)
    {
        <tr>
            <td>@objUser.userNO.ToString()</td>
            <td>@objUser.firstName</td>
            <td>@objUser.lastName</td>
        </tr>
    }
</table>

When running the application in Google Chrome, it returns following exception:

enter image description here

I have even set some breakpoints in the DynamoContext class, to check whether it gets any data from AWS, but the Exception occurs even before they are triggered. How can I check where the issue is ?.

Black Frog
  • 11,168
  • 1
  • 31
  • 64
Osman Esen
  • 1,668
  • 2
  • 14
  • 41
  • [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Soner Gönül Dec 09 '14 at 13:52
  • I have almost set breakpoints everywhere in all classes except the View. The exception is thrown before any of them are triggered. – Osman Esen Dec 09 '14 at 14:12
  • It's most likely on this line: `@objUser.userNO.ToString()` calling `ToString()` on null userNO field. – Chris L Dec 09 '14 at 14:14
  • The application does not go so far. I have even set a breakpoint on the line at "{" next to the @foreach statement in the view, and the breakpoint is not even triggered, so the error occurs at the foreach statement. – Osman Esen Dec 09 '14 at 14:55
  • I can also see from debugging that my Model is null, when the breakpoint at the foreach statement is hidden. – Osman Esen Dec 10 '14 at 00:57

2 Answers2

0

Add IEnumerable on the top of your view, above than viewbag

@model IEnumerable<FinalApplication.Models.DynamoDB>

and remove that line @model List<FinalApplication.Models.DynamoDB>

-1

in view try to use IEnumerable instead of List

  • 1
    The controller is sending a generic list to the view. Changing it to an IEnumerable would not make a difference. – mason Dec 09 '14 at 14:30