array comparison(T) delegate NullReferenceException

koko

I'm having a problem with the delegate not delegating... I have an object called Tweet that has a string text and an int score. I want to sort the array of tweet objects(twtArray) in order of the score. This is the code I have:

Array.Sort(twtArray, delegate(Tweet twt1, Tweet twt2)
        {
            return twt1.score.CompareTo(twt2.score); //(twt1.score - twt2.score)
        });

and it throws:

System.NullReferenceException: Object reference not set to an instance of an object. at System.Array.FunctorComparer`1.Compare(T x, T y)

Whilst debugging, I noticed that the first comparison works but in the second comparison, twt2 is null. And it can't be null because I definitely have 8 elements in the array. I've tried reversing twt1 and twt2 as well but makes no difference. I also tried making my own comparison method in the Tweet class but again, same thing. Any help would be appreciated!

Also, I dont think this is a duplicate of this question: List.Sort in C#: comparer being called with null object because i tried all the possible solutions from this but it's not working. i've also searched a lot on google :(

Tim Schmelter

Even if you have a Tweet[] with 8 elements some can be null:

Tweet[] twtArray = new Tweet[8]; // all instances are null

You: The Tweet[] is of size 20 and I can see that there are 8 Tweet objects in there (with correct text and score values) in the first line of my code.

So the array's size is 20 but only 8 are initialized? (see above) Array.Sort needs to compare all with all others.

You could prevent it in this way:

Array.Sort(twtArray, delegate(Tweet twt1, Tweet twt2)
{
    if(twt1 == null && twt2 == null) return 0;
    if(twt1 == null) return -1;
    if(twt2 == null) return  1;
    return twt1.score.CompareTo(twt2.score); 
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Nullreferenceexception after initializing an array

From Dev

NullReferenceException in array of objects

From Dev

NullReferenceException using TextBox array

From Dev

NullReferenceException when accessing array

From Dev

NullReferenceException on Class array reference

From Dev

Update array in Extension Delegate

From Dev

Delegate Array in Excel VBA

From Dev

Clean array by using delegate

From Dev

Sort array by delegate

From Dev

Update array in Extension Delegate

From Dev

Global Structure array gives a NullReferenceException

From Dev

Find delegate in a swift Array of delegates

From Dev

How to delegate to an array of objects in JavaScript

From Dev

NullReferenceException was unhandled in datarow array C#

From Dev

Getting NullReferenceException when trying to add a value to an array

From Dev

NullReferenceException in C# (using array of lists)

From Dev

NullReferenceException unhandled when reached null element in array

From Dev

Create delegate of array get method fails

From Dev

Marshalling dynamic array to C# delegate (callback)

From Dev

c# delegate array with string & double

From Dev

Getting 'NullReferenceException' when putting int in Array of variety Object

From Dev

Getting 'NullReferenceException' when putting int in Array of variety Object

From Dev

c# System.NullReferenceException while assign the value to Enum array

From Dev

How to delegate click event to children of jQuery element array?

From Dev

How to use delegate with lambda expression to sum up the odd number in the array

From Dev

Class Array usage generates the error "NullReferenceException: Object reference not set to an instance of an object"

From Dev

NullReferenceException with Owin

From Dev

NullReferenceException was unhandeled

From Dev

Dapper NullReferenceException

Related Related

  1. 1

    Nullreferenceexception after initializing an array

  2. 2

    NullReferenceException in array of objects

  3. 3

    NullReferenceException using TextBox array

  4. 4

    NullReferenceException when accessing array

  5. 5

    NullReferenceException on Class array reference

  6. 6

    Update array in Extension Delegate

  7. 7

    Delegate Array in Excel VBA

  8. 8

    Clean array by using delegate

  9. 9

    Sort array by delegate

  10. 10

    Update array in Extension Delegate

  11. 11

    Global Structure array gives a NullReferenceException

  12. 12

    Find delegate in a swift Array of delegates

  13. 13

    How to delegate to an array of objects in JavaScript

  14. 14

    NullReferenceException was unhandled in datarow array C#

  15. 15

    Getting NullReferenceException when trying to add a value to an array

  16. 16

    NullReferenceException in C# (using array of lists)

  17. 17

    NullReferenceException unhandled when reached null element in array

  18. 18

    Create delegate of array get method fails

  19. 19

    Marshalling dynamic array to C# delegate (callback)

  20. 20

    c# delegate array with string & double

  21. 21

    Getting 'NullReferenceException' when putting int in Array of variety Object

  22. 22

    Getting 'NullReferenceException' when putting int in Array of variety Object

  23. 23

    c# System.NullReferenceException while assign the value to Enum array

  24. 24

    How to delegate click event to children of jQuery element array?

  25. 25

    How to use delegate with lambda expression to sum up the odd number in the array

  26. 26

    Class Array usage generates the error "NullReferenceException: Object reference not set to an instance of an object"

  27. 27

    NullReferenceException with Owin

  28. 28

    NullReferenceException was unhandeled

  29. 29

    Dapper NullReferenceException

HotTag

Archive