In a xamarin.forms application, i have used xamarin.plugin.filepicker nuget package to browse and open files so in my application the process is I open the file from the file browser and update values into the file and close it ,when I go back and try to open the newly generated file the app is crashing, here is the code related to it and the type of file is CSV. please help me to sort this issue.And when the app gets crashed its displayed as Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code). with object reference not set to an instance of object alert .
this is the code for browsing files using xamarin.plugin.filepicker.
private Plugin.FilePicker.Abstractions.FileData file = default(Plugin.FilePicker.Abstractions.FileData);
public async void OnBrowse(object o, EventArgs args)
{
try
{
// var file_path =
this.file = await CrossFilePicker.Current.PickFile().ConfigureAwait(true); /*CrossFilePicker.Current.PickFile();//*/
if (this.file == null)
{
// return;
}
else
{
string extensionType = this.file.FileName.Substring(
this.file.FileName.LastIndexOf(".", StringComparison.Ordinal) + 1,
this.file.FileName.Length - this.file.FileName.LastIndexOf(".", StringComparison.Ordinal) - 1).ToLower();
fileName = this.file.FileName;
if (extensionType.Equals("csv"))
{
csv_file.Text = (fileName);
}
else
{
await this.DisplayAlert("Name of the file:" + file.FileName, "File info", "OK");
}
}
if (SettingsPage.loggingEnabled)
{
LogUtilPage.Initialize("/storage/emulated/0");
}
}
catch (Exception e)
{
await DisplayAlert("Alert", "Something went wrong", "OK");
if (SettingsPage.loggingEnabled)
{
LogUtilPage.Log(e.Message);
}
}
}
this is the code for opening the csv
public async void OnProcess(object o, EventArgs args)
{
if (!(string.IsNullOrWhiteSpace(csv_file.Text)))
{
if (App.Current.MainPage is NavigationPage)
{
try
{
_fileCSV = new FileCSV();
{
_fileCSV.FileName = fileName;
_fileCSV.ProcessDate = DateTime.Now;
_fileCSV.Status = "0";
};
_database.AddFiles(_fileCSV);
List<ItemsCSV> items = new List<ItemsCSV>();
string[] lines = File.ReadAllLines(string.Format(@"{0}", this.file.FilePath));
var reader = new StreamReader(this.file.FilePath);
if (lines != null)
{
for (int x = 1; x < lines.Length; x++)
{
string data = lines[x];
var item = ParseDelimitedString(data, ',').ToArray();
_itemsCSV = new ItemsCSV();
{
_itemsCSV.Cycle_Count = item.ElementAtOrDefault(0);
_itemsCSV.Line_Number = item.ElementAtOrDefault(1);
_itemsCSV.Item_Number = item.ElementAtOrDefault(2);
_itemsCSV.Name = item.ElementAtOrDefault(3);
_itemsCSV.Aisle = item.ElementAtOrDefault(5);
_itemsCSV.Bin = item.ElementAtOrDefault(6);
_itemsCSV.Level = item.ElementAtOrDefault(7);
_itemsCSV.Warehouse = item.ElementAtOrDefault(4);
_itemsCSV.Order_Qty = item.ElementAtOrDefault(8);
_itemsCSV.Order_UOM = item.ElementAtOrDefault(9);
_itemsCSV.Consumption_Qty = item.ElementAtOrDefault(10);
_itemsCSV.Consumption_UOM = item.ElementAtOrDefault(11);
_itemsCSV.NewLocation = item.ElementAtOrDefault(5) + "." + item.ElementAtOrDefault(6) + "." + item.ElementAtOrDefault(7);
if (item.ElementAt(8) == string.Empty)
{
_itemsCSV.Status = "";
}
else
{
_itemsCSV.Status = "Counted";
}
_itemsCSV.LastUpdate = DateTime.Now;
_itemsCSV.CSVId = _fileCSV.Id;
};
items.Add(_itemsCSV);
_database.AddItems(_itemsCSV);
}
var result = await DisplayAlert("", "CSV has been processed, please do cycle count", "OK", "Cancel");
if (result == true)
{
var cyclecountPage = new CycleCountPage(items, 1, "MainPage", this.file.FilePath);
await Navigation.PushAsync(cyclecountPage);
}
else
{
}
}
// LogUtilPage.Initialize(Path.GetDirectoryName(this.file.FilePath));
}
catch (Exception e)
{
await DisplayAlert("Exception", "Something went wrong", "OK");
// LogUtilPage.Initialize(this.file.FilePath);
if (SettingsPage.loggingEnabled)
{
LogUtilPage.Log(e.Message);
}
}
}
}
else
{
await DisplayAlert("Alert", "File name is mandatory", "OK");
}
}