0

I have an array where the user can fill it up with objects. Objects are different sodas. There is a function in this simulator where the user has the option to find a specific soda. I don't want it to be case-sensitive so im using the method ToLower(). Everything works fine until one of the spots in the array is empty. I get a null reference error.

I've been trying to solve this issue by using a if statement but then the variable is unavailable in other statements.

How can i solve my issue???

Thanks in advance!

do
{
    name = Console.ReadLine();
    nameL = name.ToLower();

    if (name == "h" || name == "H")
        break;

    for (int i = 0; i < sodas.Length; i++)
    {
        sodasL = sodas[i].Name.ToLower();

        if (name == "h" || name == "H")
        {
            Run();
            break;
        }
        else if (sodas[i] == null)
            Console.WriteLine("Det är tomt på indexet: {0}!", i + 1);//translation: its empty at index..
        else if (sodasL != nameL)
            Console.WriteLine("Drycken hittades inte på indexet: {0}.", i + 1);//translation: Could not find soda at index...
        else if (sodasL == nameL)
        {
            Console.WriteLine("Drycken {0} finns på indexet: {1}.\n", sodas[i].Name, i + 1);//translation: found soda at index...

            Console.WriteLine("Vill du ta bort drycken?");//translation do you want to remove soda?
            Console.WriteLine("[J]a");//yes
            Console.WriteLine("[N]ej");//no
            Console.WriteLine("[G] för nästa {0}.", sodas[i].Name);
            string inmatat = Console.ReadLine();
            if (inmatat == "j" || inmatat == "J")
            {
                amount_bottles--;
                sodas[i] = null;
                Console.WriteLine("Drycken har tagits bort! Sök efter en annan dryck eller [H] för Huvudmenyn");//soda has been removed
                break;
            }
            else if (inmatat == "n" || inmatat == "N")
            {
                Console.WriteLine("Drycken är kvar! Sök efter en annan dryck eller [H] för Huvudmenyn.");//soda has not been removed
                break;
            }
        }
    }
} while (name != "h" || name != "H");
fhnaseer
  • 7,159
  • 16
  • 60
  • 112
AllanJ
  • 147
  • 1
  • 15
  • In your for loop check if sodas[i] is null before using it and if so call continue. – Ralf Mar 27 '19 at 10:14
  • Since array's are immutable therefore use List to remove/add as per your requirements. The best solution is to redesign your code. Do something like this: `List sodaList= new List(sodas);`. This would convert your array to a List which is mutable. – Rahul Sharma Mar 27 '19 at 10:16
  • Ive got one else IF sodas[i] is null already, can I use it twice? And also the assignment only allows arrays and not lists., – AllanJ Mar 27 '19 at 10:54
  • @user11131093 Don't set your array to `null`. Set to something with which you can compare. `sodas[i] = "Removed";` and then check for it `else if (sodas[i] == "Removed"){//your code}`. Comparing your array to null means that you are checking that your array is not initialized. In this case, you need to check for it before you start looping over your array. – Rahul Sharma Mar 27 '19 at 11:20

2 Answers2

0

According to your code, you add objects to sodas. Let's say that your object looks like this class:

public class Container
{
    public string Name;
}

When you add a new object without initialisation of the Name field, like:

sodas[0] = new Container();

The field Name is null. So, in this case, when you call sodasL = sodas[i].Name.ToLower(), it looks like:

string name = sodas[0].Name; // name = null!!!
sodasL = name.ToLower(); // You try to call the method of an object, but the reference is null.

Here is a couple of solutions:

  1. You need to init Name by default (it may be an empty string).
public class Container
{
    public string Name = "";
}
  1. You may check Name before the ToLower call.
string name = sodas[i].Name;
if (name != null)
{
    sodasL = name.ToLower();
    // ...
}
Anton
  • 560
  • 6
  • 21
  • 1
    .Add(); does not work for Arrays. –  Mar 27 '19 at 12:14
  • 1
    Thanks, you are right. I've fixed that. – Anton Mar 27 '19 at 12:15
  • The 2nd option is exactly what I tried. And it couldn't reach the variable sodasL in other statement. I guess it's because it only exists locally? – AllanJ Mar 27 '19 at 12:43
  • What is "other statement"? Is `sodaL` a field in your class? Is it a local variable in this method? Could you add more details in your question? Where do you declare `sodaL`? – Anton Mar 27 '19 at 13:17
  • @user11131093 You should check `if (sodas[i] == null)` before `sodasL = sodas[i].Name.ToLower();`. – Anton Mar 27 '19 at 13:29
  • Solved! Thank you. Last trick worked. – AllanJ Mar 27 '19 at 18:12
0

You can fix your issue with removing these lines:

nameL = name.ToLower();

and

sodasL = sodas[i].Name.ToLower();

And using String.Equals for a case insensitive comparison:

//...
if (sodas[i] == null)
    Console.WriteLine("Det är tomt på indexet: {0}!", i + 1)
else if (!String.Equals(sodas[i], name, StringComparison.OrdinalIgnoreCase))
    Console.WriteLine("Drycken hittades inte på indexet: {0}.", i + 1);
else //Another if is unnecessary
{
    //...
}
//...
Sefe
  • 13,731
  • 5
  • 42
  • 55