0

I'm receiving a Null Reference exception when I create a string type variable inside the onUpdateSlabPicture function (see below).

Note "ApplicationManager.Istance.Paths.SlabPictureFile" is not null in every part (Istance, Paths and SlabPictureFile) , I can see them in the watch and I can assign them to variables.

This is the situation:

// This method starts a task and when it's finished, it calls the other method below
private void takeAndCalibratePhoto()
{
    if(!_startExecutionTakeAndCalibratePhoto)
    {
        MiMessage viewMessage = new MiMessage();
        waitOperation(
            (token) =>
            {
                _startExecutionTakeAndCalibratePhoto = true;
                string errorMessage = "";
                if(!_CameraManager.TakeAndCalibratePhoto(viewMessage, token, out errorMessage))
                {
                    StopAction(null);
                }
                return errorMessage;
            },
            (p) =>
            {
                string errorMessage = (string)p;
                if(errorMessage.Length > 0)
                {
                    StopAction(null);
                    ApplicationManager.Istance.ShowInformationMessage(errorMessage);
                }
                else
                {
                    onUpdateSlabPicture();
                }
                _startExecutionTakeAndCalibratePhoto = false;
            },
            viewMessage, true);
    }
}

protected override void onUpdateSlabPicture()
{
    if (_CameraManager.Photo.SlabBitmap != null)
{
        // vvvvvvvv Null reference Exception vvvvvvvvvvvvvvvv
    string fileDaSalvare = ApplicationManager.Istance.Paths.SlabPictureFile;
        // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        if (_Options.SelectionSlabImage)
        {
            List<string> listaNomi = new List<string>();
            ApplicationManager.Istance.ShowQuertyPad("Slab Name".Translate(), System.IO.Path.GetFileNameWithoutExtension(ApplicationManager.Istance.Paths.SlabPictureFile), listaNomi, (name) => { fileDaSalvare = name; });
        }
        _CameraManager.Photo.SlabBitmap.Save(fileDaSalvare);
        _CameraManager.Photo.SlabBitmap.Dispose();
        _CameraManager.Photo.SlabBitmap = null;
    }
}

Why? Moreover, a less important question: why if I write the code in this way:

if (_CameraManager.Photo.SlabBitmap != null)
{
    string fileDaSalvare;

        if (_Options.SelectionSlabImage)
        {
            List<string> listaNomi = new List<string>();
            ApplicationManager.Istance.ShowQuertyPad("Slab Name".Translate(), System.IO.Path.GetFileNameWithoutExtension(ApplicationManager.Istance.Paths.SlabPictureFile), listaNomi, (name) => { fileDaSalvare = name; });
        }
        else
        {
            fileDaSalvare = ApplicationManager.Istance.Paths.SlabPictureFile;
        }
        _CameraManager.Photo.SlabBitmap.Save(fileDaSalvare);
    }
}

the compiler does not recognize the initialization of the string inside the anonymous method?

IssamTP
  • 2,408
  • 1
  • 25
  • 48
  • In the second part, the compiler has no way of knowing if your delegate is being called or not, so it cannot guarantee that by the time it needs to read from the variable it has definitely been given a value. Initialize the variable to some default value to satisfy the compiler. – Lasse V. Karlsen Oct 09 '19 at 10:41
  • In the first part, are you sure it's not `_Options` that is `null` and it's just the compiler highlighting the wrong line? The problem with null reference exception questions here on Stack Overflow is that invariably the only advice we can give is that you need to debug your code to figure out what is null. Once you know *what* is null, a possibly better and more on-topic question is *why* it is null, but "I have this exception, can anyone help" is almost always closed as a duplicate. Can you clarify that you have debugged and what you found? Also, what is the stack trace of the exception? – Lasse V. Karlsen Oct 09 '19 at 10:41
  • Oh, well, I'm still too used to C++. And yes: I'm sure, by the way the program throws Null Ref even if I use: string fileDaSalvare = ""; – IssamTP Oct 09 '19 at 10:43
  • 2
    So then the error is not on that line. Can you debug your code to find out where it actually is? – Lasse V. Karlsen Oct 09 '19 at 10:44
  • @LasseVågsætherKarlsen dunno if in this case can count: to reach this particular part of code I have to jump some instructions (3 or 4 times), can this corrupt the heap in some way? – IssamTP Oct 09 '19 at 10:51
  • @Fredrik thanks, but I know when null excs are thrown. As written in the question, I've checked carefully that ApplicationManager.Istance.Paths.SlabPictureFile is valid and accessible. – IssamTP Oct 09 '19 at 10:54

0 Answers0