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?