Gustavo Melo
Gustavo Melo

Reputation: 199

Problem with Dictionary<string, string>

Can anyone help me?

I had a simple code:

private void ObterRelatorios(string id) { 
    var relatorios = new Dictionary<string, string>();

    var xml = new XmlDocument();

    xml.Load("my_path");

    foreach (XmlNode node in relatoriosStaticos.DocumentElement.ChildNodes)
        relatorios.Add(node.Attributes["Titulo"].InnerText, string.Concat(node.Attributes["Url"].InnerText, id));
}

My xml is quite simple with 5 nodes and always is the same.

This is wierd because sometimes works, sometimes not.

This is what I get when throw an exception

Error Details:

Exception of type 'System.Web.HttpUnhandledException' was thrown.

Full Stack:

[ArgumentNullException: Value cannot be null.
Parameter name: key]
at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
at System.Collections.Generic.Dictionary.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary.Add(TKey key, TValue value)

Upvotes: 0

Views: 5417

Answers (4)

Mike Gledhill
Mike Gledhill

Reputation: 29161

Just to say, I had this same error, Value cannot be null, when using ToDictionary() and two of the Key values were the same.

I didn't realise this, and found the error message a little misleading, as none of my values were null.

My solution (applying it to your problem) would be to take your original code:

foreach (XmlNode node in relatoriosStaticos.DocumentElement.ChildNodes)
        relatorios.Add(node.Attributes["Titulo"].InnerText, string.Concat(node.Attributes["Url"].InnerText, id));

And split it into two parts.

First, populate a List<> variable with a list of Keys & Values, which you plan to put into a Dictionary.

public class KeysAndValues
{
    public string Key;
    public string Value;

    public override string ToString()
    {
       return string.Format("{0}: {1}", Key, Value);
    }
}

List<KeysAndValues> dict = new List<KeysAndValues>();
foreach (XmlNode node in relatoriosStaticos.DocumentElement.ChildNodes)
{
    dict.Add(new KeysAndValues() 
    {
        Key = node.Attributes["Titulo"].InnerText,
        Value = string.Concat(node.Attributes["Url"].InnerText, id)
    });
}

Next, get your code to check for NULL values, and duplicated Key values.

If all's well, then we can convert the List<> into a Dictionary.

foreach (KeysAndValues kv in dict)
{
    relatorios.Add(kv.Key, kv.Value);
}

It's a little more work, but this is a neat way to check for Dictionary problems, rather than waiting for an Exception to occur, if there's a problem with your data.

Upvotes: 0

Andre Gross
Andre Gross

Reputation: 263

Maybe a "Titulo" Node is in some cases empty. You have to check it before you want to add it to your Dictionary, because the Key-Property can`t have empty Value.

Here an example to prevent it.

foreach (XmlNode node in relatoriosStaticos.DocumentElement.ChildNodes)
{
    if (node.Attributes["Titulo"].InnerText == string.Empty)
    {
        continue;
    }
    else
    {
        relatorios.Add(node.Attributes["Titulo"].InnerText,
            string.Concat(node.Attributes["Url"].InnerText, id));
    }
}

Upvotes: 1

Vlad Bezden
Vlad Bezden

Reputation: 89547

Are you sure you always have value in your node.Attributes["Titulo"].InnerText attribute. If not you will have ArgumentNullException. You can't have null value for key in Dictionary.

Upvotes: 0

Jamie Treworgy
Jamie Treworgy

Reputation: 24334

The error says "[ArgumentNullException: Value cannot be null. Parameter name: key]"

The value for:

node.Attributes["Titulo"].InnerText

apparently is null in some cases, and this is not allowed. The key for a dictionary entry cannot be null.

Upvotes: 4

Related Questions