0

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");
        }
    }

attached is the output from debug

D V
  • 211
  • 6
  • 13
  • Which line causes the crash? What is the exception that gets thrown? – Jason Jun 04 '18 at 16:32
  • it says this 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). and object reference not set to an instance of object – D V Jun 04 '18 at 16:34
  • https://stackoverflow.com/questions/46634776/your-app-has-entered-a-break-state-but-there-is-no-code-to-show-because-all-thr?noredirect=1&lq=1 – Jason Jun 04 '18 at 16:35
  • and, you can still use the debugger to step through the code and identify the line causing the exception, and/or use a try catch to narrow it down – Jason Jun 04 '18 at 16:36
  • please find my updated post – D V Jun 04 '18 at 17:09
  • please read my comments, apparently you're ignoring my suggestions – Jason Jun 04 '18 at 17:14

0 Answers0