`Object reference not set to an instance of an object` in Azure Functions Queue Trigger

krishna572 876 Reputation points
2022-05-03T11:15:49.783+00:00

Scenario:

How to use the Queue trigger to get the message from the queue and add that message details to the entity of Azure Table service within the storage account?

Exception:

tYfCa0K.png

Code: .NET 3.1 Azure Functions - Queue Trigger

Order.cs

csharp
namespace AzStorageQTriggerFunction
{
    public class Order
    {
        // This will map to the Category property of the data
        public string PartitionKey { get; set; }
        // This will map to the Order ID property of the data
        public string RowKey { get; set; }
        public int Quantity { get; set; }
        public decimal UnitPrice { get; set; }
    }
}

QueueMessage.cs

csharp
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;

namespace AzStorageQTriggerFunction
{
    public static class QueueMessage
    {
        [FunctionName("GetMessages")]
        [return: Table("Orders", Connection = "storage-connectionstring")]
        public static Order Run([QueueTrigger("appqueue", Connection = "storage-connectionstring")] JObject myQueueItem, ILogger log)
        {
            Order _order = new Order()
            {
                PartitionKey = myQueueItem["Category"].ToString(),
                RowKey = myQueueItem["OrderID"].ToString(),
                Quantity = Convert.ToInt32(myQueueItem["Quantity"]),
                UnitPrice = Convert.ToDecimal(myQueueItem["UnitPrice"])
            };

            log.LogInformation("Order written to table");

            return _order;
        }
    }
}

Add Message to the Queue:

JSON
{"Order ID":"01","Category":"Mobiles","Quantity":100,"UnitPrice":9.99}

Function running the queue trigger after passing message to the queue and getting exception while sending it to the Azure Table within the same storage account.

Could any of the community experts help me to resolve this exception and what is the reason of causing this?

Azure Table Storage
Azure Table Storage
An Azure service that stores structured NoSQL data in the cloud.
163 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,643 questions
Azure Queue Storage
Azure Queue Storage
An Azure service that provides messaging queues in the cloud.
100 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,629 questions
{count} votes

Accepted answer
  1. VenkateshDodda-MSFT 19,641 Reputation points Microsoft Employee
    2022-05-03T13:46:43.103+00:00

    @NFSCoder-9821 Thanks for reaching out. Looks like the new message that you are adding in appqueue has the attribute of Order ID but you are passing it as OrderID in the QueueMessage.cs that is reason you are getting System.NullReferenceExpection.
    I would suggest to replace line 18 in your QueueMessage.cs.

    old code:

    RowKey = myQueueItem["OrderID"].ToString(),
    

    New code:

    RowKey = myQueueItem["Order ID"].ToString(),
    

    I have tested this it is working fine for me and i would just you to test it from your end. Feel free to reach back to me if you have any further questions.


0 additional answers

Sort by: Most helpful