0

In my Drag and drop Listview i am collecting the dragged and dropped files by:

var objects=Data.GetData(DataFormats.FileDrop, false);

I can also cast this and i get paths of all dragged and dropped files:

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

It works fine, but when i drag and drop "MyComputer" or something from Webbrowser, my program throws nullfrefferenceexception.

My question is what is the exact return value of Get data method below (when i drag and drop few files in one moment) ?:

Data.GetData(DataFormats.FileDrop, false);

I assume i have to check every object and eliminate the null ones (then i can cast an array without Null-objects to string [] and i get proper paths and no NRexceptions during further processing).

This code still throws System.NullRefferenceException:

private void Directories_ListView_DragDrop(object sender, DragEventArgs e)
{
    object[] rawArray=(object[])e.Data.GetData(DataFormats.FileDrop, false);
    foreach (var s in rawArray)\\System.NullRefferenceException occurs..
    {
        try
        {
            if (!s.Equals(null))
            {
                LogList.Items.Add(DateTime.Now + " Item isnt Null");
            }
        }
        catch (Exception)
        {
            LogList.Items.Add(DateTime.Now + " Item is null");
            continue;
        }
   }
johnny 5
  • 19,893
  • 50
  • 121
  • 195
TamaraL96
  • 47
  • 6
  • I think your problem is the expression `s.Equals(null)`, which requires that `s` not be null in order to work. Try `Equals(s, null)` or `s == null` which are `static`, not instance based. If using C# 7, `s is null` will also work and may be more performant. – JamesFaix Aug 02 '18 at 18:23

1 Answers1

0

I'm sure we've already, pretty much answered this for you Yesterday. You will need to check your list items before you do anything with them. You're getting a null back as per the documentation that Abion47 linked for you.

A string is a nullable type so the answer given yesterday still holds. If you didn't like the try catch around the ListViewItem creation you can always do as you've done above check for null first.

if (e.Data.GetData(DataFormats.FileDrop, false) != null)
{
    string[] DroppedDirectories = (string[]) e.Data.GetData(DataFormats.FileDrop,false);
    List<string> directories = new List<string>();
    foreach(string dir in DroppedDirectories)
    {
        if (dir != null)
            directories.Add(dir);
    }

    // Now loop over the directories list which is guaranteed to have valid string items
}

Documentation for GetData is here

It states that it will attempt to convert it to whatever format you require. If it can't then it will return null. Yesterday you wanted to convert to a string array (DataFormat.FileDrop) and it would fail.

Today you're attempting to convert to object and are getting the same error in the same place. You're still attempting to convert it into DataFormats.FileDrop and that's where it's returning null. You're still asking GetData to convert to DataFormats.FileDrop but it can't.

RecycleBin and Desktop are special directories and I assume that DrapDrop cannot handle them so the conversion fails and it returns null.

I've tried:

var ob = e.Data.GetData(typeof(object));

and it still returns null when you include the recycle bin. If you attempt to get the data type of the data e.Data.GetType() you'll find the data is of type:

System.Windows.Forms.DataObject

You can guard against your null crashes as before or by using:

if (e.Data.GetDataPresent(DataFormats.FileDrop)

This checks to see if the data can be formatted into the type you want. But it can't tell you what type of data is actually inside!

Sadly, it looks like including RecycleBin or Desktop will always fail conversion no matter what you do.

You could always check to see if it converts and if it doesn't pop a message to your user saying that they should not attempt to drop recycle bin/desktop:

if (!e.Data.GetDataPresent(DataFormats.FileDrop)
{
    MessageBox("Please don't drop Recycle Bin or Desktop");
    return;
}
Handbag Crab
  • 1,488
  • 2
  • 8
  • 12
  • I almost understand your point, but when u drag and drop few elements (for example 2 folders + my computer) nothing is processed. Its seems like null is returned always by e.Data.GetData method when "mycomputer" elements was d&dropped with other elements. That's why i don't get what is exactly returned by this method and how to test every element it gets. To solve my problem i have to detect every Null element which e.Data.GetData gets, but i have no idead how to perform this. This is the core of this problem. – TamaraL96 Aug 02 '18 at 18:23
  • I see what you're getting at now. I've updated my answer above. I'm afraid the prognosis isn't good. – Handbag Crab Aug 02 '18 at 18:59
  • That's why i don't get this method . It returns null always when one of the elements isn't recognized (like my computer, bin etc.). From the other hand you can cast it to get the System.object array of string array et cetera... In this place i thought i will be able to detect null objects, but its seems to impossible. @Abion47 said a lot of useful things , but this is what was my problem about ;) – TamaraL96 Aug 02 '18 at 19:09
  • I've tried putting a breakpoint on if (e.Data.GetDataPresent) to see if it will show what the type of data is inside but even that doesn't give out any clues. Sorry. – Handbag Crab Aug 02 '18 at 19:10
  • Dont worry, my app will still work. I have few more ideas, but not enough time. I will inform you after finding any solution. Thank you for your contribution though :) – TamaraL96 Aug 02 '18 at 19:12