Home > Enterprise >  Having trouble fixing null reference errors
Having trouble fixing null reference errors

Time:01-22

Im very new to C# and I am trying to complete my assignment but I came across some errors that I don't understand how to fix or even what they are implying.

using System;
public class Assignment1B
{
    public static void Main(string[] args)
    {
        string item_one, itemtwo;
        int itemone_quantity, itemtwo_quantity;
        float itemone_cost, itemtwo_cost, itemone_total, itemtwo_total;

        // Asking first item
        Console.Write("What are you buying? ");
        item_one = Console.ReadLine();
        Console.Write("How many? ");
        itemone_quantity = int.Parse(Console.ReadLine());
        Console.Write("What do they cost? ");
        itemone_cost = float.Parse(Console.ReadLine());
        Console.WriteLine();

        // Asking second item
        Console.Write("What else are you buying? ");
        itemtwo = Console.ReadLine();
        Console.Write("How many? ");
        itemtwo_quantity = int.Parse(Console.ReadLine());
        Console.Write("What do they cost? ");
        itemtwo_cost = float.Parse(Console.ReadLine());
        Console.WriteLine();

        // Math
        itemone_total = itemone_quantity * (float)itemone_cost;
        itemtwo_total = itemtwo_quantity * (float)itemtwo_cost;

        // Listing everything
        Console.WriteLine("Your list:");
        Console.WriteLine("--------");
        Console.WriteLine("{0} ({1})", item_one, itemone_quantity);
        Console.WriteLine("{0} ({1})", itemone_cost, itemone_total);
        Console.WriteLine();
        Console.WriteLine("{0} ({1})", itemtwo, itemtwo_quantity);
        Console.Write("{0} ({1})", itemtwo_cost, itemtwo_total);
    }
}

The assignment wants me to asks you for the name, quantity, and price of two grocery store items. Then display them as a list.

What I have tried: I have tried looking up the error codes individually to try and see if I can find an example to get a better understanding of why I am getting the error but I haven't found anything that deeply explains everything. Sorry I am still learning and I am open to any advice or tips.

Errors that I have:

item_one = Console.ReadLine(); Warning CS8600: Converting null literal or possible null value to non-nullable type
itemone_quantity = int.Parse(Console.ReadLine()); Warning CS8604: Possible null reference argument for parameter 's' in 'int int.Parse(string s)'.
itemone_cost = float.Parse(Console.ReadLine()); Warning CS8604: Possible null reference argument for parameter 's' in 'float float.Parse(string s)'.
itemtwo = Console.ReadLine(); Warning CS8600: Converting null literal or possible null value to non-nullable type.
itemtwo_quantity = int.Parse(Console.ReadLine()); Warning CS8604: Possible null reference argument for parameter 's' in 'int int.Parse(string s)'.
itemtwo_cost = float.Parse(Console.ReadLine()); Warning CS8604: Possible null reference argument for parameter 's' in 'float float.Parse(string s)'.

CodePudding user response:

You should change the variable names to be more descriptive and related to grocery items, such as itemOneName, itemOneQuantity, itemOnePrice, itemTwoName, itemTwoQuantity, and itemTwoPrice.

In the prompts for item one, use the appropriate variable names instead of "What are you buying?" "How many?", and "What do they cost?"

Similarly, for item two, use the appropriate variable names instead of "What else are you buying?" "How many?", and "What do they cost?".

try changing the output section, change the format to display the list of items in a clearer way, for example:

Console.WriteLine("Your list:");
Console.WriteLine("--------");
Console.WriteLine("Item: {0}, Quantity: {1}, Price: {2}, Total: {3}", itemOneName, itemOneQuantity, itemOnePrice, itemOneQuantity * itemOnePrice);
Console.WriteLine("Item: {0}, Quantity: {1}, Price: {2}, Total: {3}", itemTwoName, itemTwoQuantity, itemTwoPrice, itemTwoQuantity * itemTwoPrice);

something like this in the end?

using System;
public class Assignment1B
{
    public static void Main(string[] args)
    {
        string itemOneName, itemTwoName;
        int itemOneQuantity, itemTwoQuantity;
        float itemOnePrice, itemTwoPrice, itemOneTotal, itemTwoTotal;

        // Asking first item
        Console.Write("What is the name of the first item? ");
        itemOneName = Console.ReadLine();
        Console.Write("How many of {0} do you want? ", itemOneName);
        itemOneQuantity = int.Parse(Console.ReadLine());
        Console.Write("What is the price of {0}? ", itemOneName);
        itemOnePrice = float.Parse(Console.ReadLine());
        Console.WriteLine();

        // Asking second item
        Console.Write("What is the name of the second item? ");
        itemTwoName = Console.ReadLine();
        Console.Write("How many of {0} do you want? ", itemTwoName);
        itemTwoQuantity = int.Parse(Console.ReadLine());
        Console.Write("What is the price of {0}? ", itemTwoName);
        itemTwoPrice = float.Parse(Console.ReadLine());
        Console.WriteLine();

        // Math
        itemOneTotal = itemOneQuantity * itemOnePrice;
        itemTwoTotal = itemTwoQuantity * itemTwoPrice;

        // Listing everything
        Console.WriteLine("Your list:");
        Console.WriteLine("--------");
        Console.WriteLine("Item: {0}, Quantity: {1}, Price: {2}, Total: {3}", itemOneName, itemOneQuantity, itemOnePrice, itemOneTotal);
        Console.WriteLine("Item: {0}, Quantity: {1}, Price: {2}, Total: {3}", itemTwoName, itemTwoQuantity, itemTwoPrice, itemTwoTotal);
    }
}

CodePudding user response:

A possible null reference means that you are pointing (using/relying) to something that may or may not exist.

You could try to learn how to use the TryParse method or dive into exception handling to validate the user inputs, but these may be a little too much if you are just starting with programming.

  •  Tags:  
  • c#
  • Related