Null Exception with Array.FindIndex searching in a string array in C# - c#

Someone has chosen to mark this as duplicate, and though I can appreciate trying to clean up a question asking community, the I have examined the "duplicated" material in detail prior to posting my question. As indicated in my question below, I show the outputs of the issue in question, which has no nulls.
I have a method I create based on a user selecting a spinner value that looks like this:
public void terrain_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spinner = (Spinner)sender;
var ttype = string.Format("{0}", spinner.GetItemAtPosition(e.Position));
Console.WriteLine(ttype);
foreach (var item in Terrainstr) { Console.WriteLine(item); }
var index = Array.FindIndex(Terrainstr, x => x.Contains(ttype));
As you can see, the string ttype is simply the selection the user makes with the spinner.
I have a string array Terrainstr that's initialized before OnCreate like this:
string[] Terrainstr = new string[10];
I populate it later with string fragments based on a number of user selections.
You'll notice I have Console.WriteLine commands in the method. My (annotated) output when that method is used is:
Fast (ttype)
Stock (Terrainstr[0])
Slow
Slow Sand
Slow Sand Rocks Whoops
Slow Sand Mud Rocks Whoops
Slow Mud Rocks Whoops
Slow Mud Rocks (Terrainstr[6])
I want to find which indeces of Terrainstr contain ttype. In this case, it wouldn't fit it at all. I get a null exception using the command shown here:
var index = Array.FindIndex(Terrainstr, x => x.Contains(ttype));
Please advise!

Basically your array is of size 10 and you only have 8 elements in it (based on Console.WriteLine output) so the last two are nulls.
Array.FindIndex doesn't check if the element is null or not and pass it to your lambda where you try to run .Contains. If it's null then you get a NullReferenceExeception.
Filter you array and only do the FindIndex on non-null elements.
var index = Array.FindIndex(Terrainstr, x => x != null && x.Contains(ttype));

Related

How to step into Linq.FindAll query to determine cause of failure using a breakpoint [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 8 months ago.
I want to use the FindAll method in C# in like this:
List<Card> cards = new List<Card>();
cards.AddRange(testCaseCards);
Console.WriteLine(cards.Count);
List<List<Card>> cardsSuits = new List<List<Card>>();
List<Card> tempList = new List<Card>();
for (int i = 0; i < 4; ++i)
{
Console.WriteLine(cards[i].Suit);
tempList = cards.FindAll(card => card.Suit == (Suit)i);
cardsSuits.Add(tempList);
}
When I initialize my code with testCaseCards containing 9 cards, everything works just fine up until the FindAll query. The value of cards.Count is 9 and cards[i].Suit returns the respective suits.
But when FindAll executes it throws an exception that "card was null" and I'm looking for a way to step into the FindAll as it goes through the cards and set a breakpoint to examine values and determine the exact cause of failure. I looked at this answer which mentions Linq in passing but doesn't provide any real guidance about debugging a null exception while the Linq query is running.
Also, if you notice any other problems in my code please point them out.
One way to debug the FindAll (or any Linq) is by creating a Predicate method. For example:
private static bool isSuit(Card card, Suit suit)
{
if (card == null)
{
// Set a break point here. When the null card comes in
// (as it surely will) you can answer 'why' for yourself.
System.Diagnostics.Debug.Assert(false, "Expecting a non-null card");
return false; // But at least it won't crash now.
}
else
{
return card.Suit == suit;
}
}
Now you will be able to step into this method by changing your FindAll statement to call the predicate instead:
tempList = cards.FindAll(card => isSuit(card, suit));
In addition there is another potential problem. You have a loop where i could mean anything.
for (int i = 0; i < 4; ++i)
In fact the first two statements in the loop interpret it in very different ways. The first statement in the loop seems to think that i is the index in the cards collection. You say cards holds 9 cards. Why stop the loop after 4 then?
Console.WriteLine(cards[i].Suit);
On the other hand, the second line tries to cast i to Suit (which has only four values). if i does go past 4, this won't be good. I would strongly encourage you to change your loop to something like this where we know for sure which collection is being iterated:
foreach (Suit suit in Enum.GetValues(typeof(Suit)))
{
Console.Write(suit.ToString());
tempList = cards.FindAll(card => isSuit(card, suit));
cardsSuits.Add(tempList);
Console.WriteLine($" has {tempList.Count} cards.");
}
The test case I ran gets this:

Index out of range using lists and for loop

I have this loop here, which for each question it is supposed to create, it generates and then formats a 'worded question' from an array of questions, such as; 'What is the sum of {0} + {1}?'. This loop then formats it, adds the worded question and the answer to an array.
// Use for loop to create the correct amount of questions
for (int i = 0; i < Data.Questions.numQuestions; i++)
{
Random rnd = new Random();
Data.Questions.Sum sum = new Data.Questions.Sum();
// Create part one and part two of the question using random numbers
// ex. 3 + 5
// 3 = partOne, 5 = partTwo
int partOne = rnd.Next(Data.Questions.Sum.min, Data.Questions.Sum.max);
int partTwo = rnd.Next(Data.Questions.Sum.min, Data.Questions.Sum.max);
// Randomly select one of the word questions
string fullQuestion = Data.Questions.Sum.wordedQuestions[rnd.Next(0, Data.Questions.Sum.wordedQuestions.Length)];
// Format the string with the generated numbers
fullQuestion = string.Format(fullQuestion, partOne, partTwo);
// Set out-of-class variables to be displayed to the user
Data.Questions.Sum.questions[i] = fullQuestion;
Data.Questions.Sum.answers[i] = partOne + partTwo;
}
Both Data.Questions.Sum.questions and Data.Questions.Sum.answers are List<string>'s and List<int>'s.
However, when this loop is run, with i = 0, I am thrown;
System.ArgumentOutOfRangeException: 'Index was out of range. Must be
non-negative and less than the size of the collection. Parameter name:
index'
Does anyone know what I'm doing wrong? As far as I know lists are dynamic, and I've defined like this;
// Arrays containing all questions and answers
// used to display questions and check answers
public static List<string> questions = new List<string>();
public static List<int> answers = new List<int>();
Also, to clarify, I do not want to use .Add(), as I have a settings panel which when you hit apply, re-runs this loop so the questions are up to date to the current settings. I need the loop to override the previous values.
Edit:
When using arrays, the better option here, I get;
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
On assigning Data.Questions.Sum.answers[i], after assigning the array like so; public static int[] answers {};
If you can't .Add() in this lists - create a copy of this lists and .Add() there. Lists has special for that kind of thing: new List<T>(IEnumerable<T>)
If you need to dynamically scale the collection, but you also need to iterate over it multiple times, then you'll need to check whether it is large enough to either insert, or just update.
You can do this with an extension method such as this...
public static class ListExtentions
{
public static void AddOrUpdate<T>(this List<T> that, int index, T value)
{
if (that.Count > index)
{
that[index] = value;
}
else
{
that.Add(value);
}
}
}
...which can then be called like this...
list.AddOrUpdate(index, value);
...however you can make things easier for yourself if you know how many questions you are going to have to start with.
If the number of questions changes when your UI changes, then you will also have to deal with the issue have scaling down the collection to ensure old elements are removed, which is much simpler if you just re-instantiate the collections every time you need to regenerate the questions / answers.
This is likly to be cause of your problem,(I have asked for clarification in comments where you didn't reply).
Still putting this as an answer, as it is potential error spot and you need to fix this.
As you mentioned you are facing this exception on i=0. there are high chanced that this is every time case not any specific case.
If Data.Questions.Sum.questions is empty then, Data.Questions.Sum.questions[i] = fullQuestion; , will surely throw such exception. Same way for Data.Questions.Sum.answers too.
In such case, you must use .Add() to insert into list.
so your code should be,
if (Data.Questions.Sum.questions.Count > i)
Data.Questions.Sum.questions[i] = fullQuestion;
else
Data.Questions.Sum.questions.Add(fullQuestion);
But if they are not empty, it must not be the cause of this exception.
One more thing i have noticed in your code is Data.Questions.Sum.wordedQuestions.
Even if you have valid list (here Data.Questions.Sum.wordedQuestions) - as you have Length prop, it must be Array not list.
If it is empty, while doing this
string fullQuestion = Data.Questions.Sum.wordedQuestions[rnd.Next(0, Data.Questions.Sum.wordedQuestions.Length)];
this line will surely throw
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
as you are trying to get 0th index's data from it.
So before fetching data from list or array, first you need to check if it does have data init, and also it does have that index which you are asking.
something like
string fullQuestion = string.Empty;
if (Data.Questions.Sum.wordedQuestions != null &&
Data.Questions.Sum.wordedQuestions.Length > 0)
{
//here the way you are creating random number,
// you are assured about index is present in array.
int indexForWordedQuestion = rnd.Next(0, Data.Questions.Sum.wordedQuestions.Length);
fullQuestion = Data.Questions.Sum.wordedQuestions[indexForWordedQuestion];
}

array comparison(T) delegate NullReferenceException

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 :(
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);
});
When you're working with an array that's only partially filled, that indicates that you don't actually want an array, you want List<T>.
Using that, you can have a collection that contains 8 items, but can be later expanded to 20 efficiently. And when you call Sort() on such list, you're not going to have any problems with nulls.

c# - BinarySearch StringList with wildcard

I have a sorted StringList and wanted to replace
foreach (string line3 in CardBase.cardList)
if (line3.ToLower().IndexOf((cardName + Config.EditionShortToLong(edition)).ToLower()) >= 0)
{
return true;
}
with a binarySearch, since the cardList ist rather large(~18k) and this search takes up around 80% of the time.
So I found the List.BinarySearch-Methode, but my problem is that the lines in the cardList look like this:
Brindle_Boar_(Magic_2012).c1p247924.prod
But I have no way to generate the c1p... , which is a problem cause the List.BinarySearch only finds exact matches.
How do I modify List.BinarySearch so that it finds a match if only a part of the string matches?
e. g.
searching for Brindle_Boar_(Magic_2012) should return the position of Brindle_Boar_(Magic_2012).c1p247924.prod
List.BinarySearch will return the ones complement of the index of the next item larger than the request if an exact match is not found.
So, you can do it like this (assuming you'll never get an exact match):
var key = (cardName + Config.EditionShortToLong(edition)).ToLower();
var list = CardBase.cardList;
var index = ~list.BinarySearch(key);
return index != list.Count && list[index].StartsWith(key);
BinarySearch() has an overload that takes an IComparer<T> has second parameter, implement a custom comparer and return 0 when you have a match within the string - you can use the same IndexOf() method there.
Edit:
Does a binary search make sense in your scenario? How do you determine that a certain item is "less" or "greater" than another item? Right now you only provide what would constitute a match. Only if you can answer this question, binary search applies in the first place.
You can take a look at the C5 Generic Collection Library (you can install it via NuGet also).
Use the SortedArray(T) type for your collection. It provides a handful of methods that could prove useful. You can even query for ranges of items very efficiently.
var data = new SortedArray<string>();
// query for first string greater than "Brindle_Boar_(Magic_2012)" an check if it starts
// with "Brindle_Boar_(Magic_2012)"
var a = data.RangeFrom("Brindle_Boar_(Magic_2012)").FirstOrDefault();
return a.StartsWith("Brindle_Boar_(Magic_2012)");
// query for first 5 items that start with "Brindle_Boar"
var b = data.RangeFrom("string").Take(5).Where(s => s.StartsWith("Brindle_Boar"));
// query for all items that start with "Brindle_Boar" (provided only ascii chars)
var c = data.RangeFromTo("Brindle_Boar", "Brindle_Boar~").ToList()
// query for all items that start with "Brindle_Boar", iterates until first non-match
var d = data.RangeFrom("Brindle_Boar").TakeWhile(s => s.StartsWith("Brindle_Boar"));
The RageFrom... methods perform a binary search, find the first element greater than or equal to your argument, that returns an iterator from that position

C# (.Net 2.0) Micro-Optimization Part 2: Finding Contiguous Groups within a grid

I have a very simple function which takes in a matching bitfield, a grid, and a square. It used to use a delegate but I did a lot of recoding and ended up with a bitfield & operation to avoid the delegate while still being able to perform matching within reason. Basically, the challenge is to find all contiguous elements within a grid which match the match bitfield, starting from a specific "leader" square.
Square is somewhat small (but not tiny) class. Any tips on how to push this to be even faster? Note that the grid itself is pretty small (500 elements in this test).
Edit: It's worth noting that this function is called over 200,000 times per second. In truth, in the long run my goal will be to call it less often, but that's really tough, considering that my end goal is to make the grouping system be handled with scripts rather than being hardcoded. That said, this function is always going to be called more than any other function.
Edit: To clarify, the function does not check if leader matches the bitfield, by design. The intention is that the leader is not required to match the bitfield (though in some cases it will).
Things tried unsuccessfully:
Initializing the dictionary and stack with a capacity.
Casting the int to an enum to avoid a cast.
Moving the dictionary and stack outside the function and clearing them each time they are needed. This makes things slower!
Things tried successfully:
Writing a hashcode function instead of using the default: Hashcodes are precomputed and are equal to x + y * parent.Width. Thanks for the reminder, Jim Mischel.
mquander's Technique: See GetGroupMquander below.
Further Optimization: Once I switched to HashSets, I got rid of the Contains test and replaced it with an Add test. Both Contains and Add are forced to seek a key, so just checking if an add succeeds is more efficient than adding if a Contains fails check fails. That is, if (RetVal.Add(s)) curStack.Push(s);
public static List<Square> GetGroup(int match, Model grid, Square leader)
{
Stack<Square> curStack = new Stack<Square>();
Dictionary<Square, bool> Retval = new Dictionary<Square, bool>();
curStack.Push(leader);
while (curStack.Count != 0)
{
Square curItem = curStack.Pop();
if (Retval.ContainsKey(curItem)) continue;
Retval.Add(curItem, true);
foreach (Square s in curItem.Neighbors)
{
if (0 != ((int)(s.RoomType) & match))
{
curStack.Push(s);
}
}
}
return new List<Square>(Retval.Keys);
}
=====
public static List<Square> GetGroupMquander(int match, Model grid, Square leader)
{
Stack<Square> curStack = new Stack<Square>();
Dictionary<Square, bool> Retval = new Dictionary<Square, bool>();
Retval.Add(leader, true);
curStack.Push(leader);
while (curStack.Count != 0)
{
Square curItem = curStack.Pop();
foreach (Square s in curItem.Neighbors)
{
if (0 != ((int)(s.RoomType) & match))
{
if (!Retval.ContainsKey(s))
{
curStack.Push(s);
Retval.Add(curItem, true);
}
}
}
}
return new List<Square>(Retval.Keys);
}
The code you posted assumes that the leader square matches the bitfield. Is that by design?
I assume your Square class has implemented a GetHashCode method that's quick and provides a good distribution.
You did say micro-optimization . . .
If you have a good idea how many items you're expecting, you'll save a little bit of time by pre-allocating the dictionary. That is, if you know you won't have more than 100 items that match, you can write:
Dictionary<Square, bool> Retval = new Dictionary<Square, bool>(100);
That will avoid having to grow the dictionary and re-hash everything. You can also do the same thing with your stack: pre-allocate it to some reasonable maximum size to avoid resizing later.
Since you say that the grid is pretty small it seems reasonable to just allocate the stack and the dictionary to the grid size, if that's easy to determine. You're only talking grid_size references each, so memory isn't a concern unless your grid becomes very large.
Adding a check to see if an item is in the dictionary before you do the push might speed it up a little. It depends on the relative speed of a dictionary lookup as opposed to the overhead of having a duplicate item in the stack. Might be worth it to give this a try, although I'd be surprised if it made a big difference.
if (0 != ((int)(s.RoomType) & match))
{
if (!Retval.ContainsKey(curItem))
curStack.Push(s);
}
I'm really stretching on this last one. You have that cast in your inner loop. I know that the C# compiler sometimes generates a surprising amount of code for a seemingly simple cast, and I don't know if that gets optimized away by the JIT compiler. You could remove that cast from your inner loop by creating a local variable of the enum type and assigning it the value of match:
RoomEnumType matchType = (RoomEnumType)match;
Then your inner loop comparison becomes:
if (0 != (s.RoomType & matchType))
No cast, which might shave some cycles.
Edit: Micro-optimization aside, you'll probably get better performance by modifying your algorithm slightly to avoid processing any item more than once. As it stands, items that do match can end up in the stack multiple times, and items that don't match can be processed multiple times. Since you're already using a dictionary to keep track of items that do match, you can keep track of the non-matching items by giving them a value of false. Then at the end you simply create a List of those items that have a true value.
public static List<Square> GetGroup(int match, Model grid, Square leader)
{
Stack<Square> curStack = new Stack<Square>();
Dictionary<Square, bool> Retval = new Dictionary<Square, bool>();
curStack.Push(leader);
Retval.Add(leader, true);
int numMatch = 1;
while (curStack.Count != 0)
{
Square curItem = curStack.Pop();
foreach (Square s in curItem.Neighbors)
{
if (Retval.ContainsKey(curItem))
continue;
if (0 != ((int)(s.RoomType) & match))
{
curStack.Push(s);
Retval.Add(s, true);
++numMatch;
}
else
{
Retval.Add(s, false);
}
}
}
// LINQ makes this easier, but since you're using .NET 2.0...
List<Square> matches = new List<Square>(numMatch);
foreach (KeyValuePair<Square, bool> kvp in Retval)
{
if (kvp.Value == true)
{
matches.Add(kvp.Key);
}
}
return matches;
}
Here are a couple of suggestions -
If you're using .NET 3.5, you could change RetVal to a HashSet<Square> instead of a Dictionary<Square,bool>, since you're never using the values (only the keys) in the Dictionary. This would be a small improvement.
Also, if you changed the return to IEnumerable, you could just return the HashSet's enumerator directly. Depending on the usage of the results, it could potentially be faster in certain areas (and you can always use ToList() on the results if you really need a list).
However, there is a BIG optimization that could be added here -
Right now, you're always adding in every neighbor, even if that neighbor has already been processed. For example, when leader is processed, it adds in leader+1y, then when leader+1y is processed, it puts BACK in leader (even though you've already handled that Square), and next time leader is popped off the stack, you continue. This is a lot of extra processing.
Try adding:
foreach (Square s in curItem.Neighbors)
{
if ((0 != ((int)(s.RoomType) & match)) && (!Retval.ContainsKey(s)))
{
curStack.Push(s);
}
}
This way, if you've already processed the square of your neighbor, it doesn't get re-added to the stack, just to be skipped when it's popped later.

Categories