0

Hi trying to find the cause of the error message found in the code below, still new to learning C# so any help is much appreciated :)

private static void DisplayTransaction(Transaction transaction)
    {
        string type = transaction.getType();
        string date = transaction.getDate().ToString();
        string id = transaction.getItem().getID().ToString();
        string itemname = transaction.getItem().getItemName();

        if(type == "add")
        {
            string price = transaction.getPrice().ToString();
            Console.WriteLine("\t{0, -19} {1, -6} {2, -3} {3, -10} {4, -10} {5, -5}",
                date,
                type,
                id,
                itemname,
                "",
                price);
        }
        else
        {
            string name = transaction.getEmployee();
            Console.WriteLine("\t{0, -19} {1, -6} {2, -3} {3, -10} {4, -10} {5, -5}",
                date,
                type,
                id,
                itemname,
                name,
                "");
        }
    }

This line keeps throwing "System.NullReferenceException: 'Object reference not set to an instance of an object.'

Software_Assignment.Transaction.getItem(...) returned null."

string id = transaction.getItem().getID().ToString();

1 Answers1

0

Debug the program in Visual Studio and step on/onto the problematic line by stepping into the calls to see why they are returning an unexpected null. Fix the issue or put in coding logic to handle a null return.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122