0

I am using Drag & Drop ListView on files (directories or any kind of files). When i drag&drop My Computer or Bin from Desktop NullReferenceException happens.

All i want is to skip this element (display some text in log textbox or smthing). I have no idea how to achieve that. I don't know what kind of object is MyComputer element from Desktop.

Here's my code after cutting useless to this subject logic:

private void Directories_ListView_DragDrop(object sender, DragEventArgs e)
    {

        string[] DroppedDirectories = (string[]) e.Data.GetData(DataFormats.FileDrop,false); //array of dd files paths
        try
        {
            foreach (var directory in DroppedDirectories) // here Exception  occurs this loop process all dragged and drop files 
            {
                ListViewItem Dir = new ListViewItem(Path.GetFileNameWithoutExtension(directory));
                //Place for some checking Logic
                if (GetDir.IsDirectory(directory))
                {
                   (.....);// Doing some things
                }
                else
                    LogList.Items.Add(DateTime.Now + ": Entry " + Dir.Text + " is not a directory");

            }
        }
        catch(Exception) // it only stops app from crashing, 
        {
            LogList.Items.Add(DateTime.Now + "One of elements was Mycomputer or Bin, process stopped ");
        }
    }
TamaraL96
  • 47
  • 6
  • WinForms? WPF? UWP? Something else? The "desktop" tag is pretty vague. – Abion47 Aug 01 '18 at 20:24
  • Corrected that. Your link is not related to the subject anyway. – TamaraL96 Aug 01 '18 at 20:29
  • How is your listview configured? I'm trying to implement your code but I'm finding that event with AllowDrop = true and handling DragDrop. When I drag the Recycle bin from my desktop it shows the not allowed symbol and won't drop it. – Handbag Crab Aug 01 '18 at 20:40
  • @HandbagCrab You need the DragEnter event. – LarsTech Aug 01 '18 at 20:42
  • @TamaraL96 You said you are getting a NullReferenceException. The linked question contains a comprehensive walkthrough on how to debug those kinds of exceptions, hence the duplicate. – Abion47 Aug 01 '18 at 20:44
  • @TamaraL96 For example, the line throwing the exception is `foreach (var directory in DroppedDirectories)`. The only variable getting referenced in that line is `DroppedDirectories`, so that must be `null`, which means the line _assigning_ that variable must be returning `null`. So now we've arrived at the _real_ issue, which is that `e.Data.GetData(DataFormats.FileDrop,false)` is returning `null`. – Abion47 Aug 01 '18 at 20:47
  • @TamaraL96 Consulting the [documentation](https://msdn.microsoft.com/en-us/library/5x40k0y6(v=vs.110).aspx) for that function, it says it returns `null` if there is no data in the event that can be converted to the specified format (in this case, `DataFormats.FileDrop`). Now we have the _real real_ issue that the drag-drop event doesn't contain the data you are expecting to be getting. (This can be checked beforehand by using the `GetDataPresent` method instead of relying on a `null` value or thrown exception.) – Abion47 Aug 01 '18 at 20:50
  • @TamaraL96 This is why posting a question about a NullReferenceException is generally not going to get the kind or quality of answers you are hoping for. NREs are just symptoms of what the _real_ problem is. If you get an NRE, follow the steps in the linked question. When you discover what the actual problem is, post a question about _that_ (assuming following the steps doesn't lead you to the solution on its own). – Abion47 Aug 01 '18 at 20:54
  • @HandbagCrab In Listview i enabled dragEnter and DragDrop events. DragEnter method contains only: e.Effect = DragDropEffects.All; – TamaraL96 Aug 01 '18 at 21:12
  • @LarsTech, Ah yes. If I move the above code into DragEnter I get the error as soon as I drag over. It is the string[] DroppedEntries that is failing as I've put debug.WriteLine(DroppedEntries == null ? "Yes" : "No") and it's printing Yes. So the assumption of what is in e.GetData() needs to be verified before you attempt to assign it to DroppedEntries as Abion47 says. – Handbag Crab Aug 01 '18 at 21:13

1 Answers1

0

Thanks to LarsTech and Abion47 your solution is to wrap your drag drop code in this condition:

if (e.Data.GetData(DataFormats.FileDrop, false) != null)
{
    string[] DroppedDirectories = (string[]) e.Data.GetData(DataFormats.FileDrop,false);

    foreach(string directory in DroppedDirectories)
    {
        try
        {
            ListViewItem Dir = new ListViewItem(Path.GetFileNameWithoutExtension(directory));
            //Rest of code
        }
        catch(Exception ex)
        {
            LogList.Items.Add(DateTime.Now + ": Entry " + directory + " is not a directory");
        }
    }
}
Handbag Crab
  • 1,488
  • 2
  • 8
  • 12
  • Thanks for help to everyone :) @Abion47 Thank you for performing solution process. – TamaraL96 Aug 01 '18 at 21:22
  • Code works fine, but if you drag and drop few files and one of them is Bin or Mycomputer, the other ones are still not processed. I will try to figure it out tommorow. – TamaraL96 Aug 01 '18 at 21:41
  • You'll need to check each "directory" before you process it - or just wrap the contents of the foreach loop in a try catch, that way you only catch the item that fails and can then process the next. I've updated the answer. – Handbag Crab Aug 01 '18 at 21:48
  • I'll will test your improved code :) My conception is to get rid of foreach loop and replacing it by for loop (i – TamaraL96 Aug 01 '18 at 22:16