System.IndexOutOfRangeException

The exception that is thrown when an attempt is made to access an element of an array or collection with an index that is outside its bounds.

Minimum version: >= 1.1 >= Core 1.0

Statistics

18
elmah.io logo 14

How to handle it

try
{

}
catch (System.IndexOutOfRangeException e)
{

}
try
{

}
catch (System.IndexOutOfRangeException e) when (e.Message.Contains("something"))
{

}
try
{

}
catch (System.IndexOutOfRangeException e) when (LogException(e))
{

}

private static bool LogException(Exception e)
{
    logger.LogError(...);
    return false;
}

How to avoid it

Before
var myArray = new[] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 };
var index = 10;

try
{
    var result = myArray[index];
}
catch (IndexOutOfRangeException e)
{
}
After
var myArray = new[] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 };
var index = 10;

if (index < myArray.Length)
{
    var result = myArray[index];
}

Links

YouTube videos

Possible fixes from StackOverflow

if (myReader["Additional"] != DBNull.Value)
{
    ltlAdditional.Text = "contains data";
}
else
{
     ltlAdditional.Text = "is null";
}

There are two basic ways how to map bidirectional one-to-one association in NH. Let's say the classes look like this:

public class Setting
{
    public virtual Guid Id { get; set; }
    public virtual Student Student { get; set; }
}

public class Student
{
    public virtual Guid Id { get; set; }
    public virtual Setting Setting { get; set; }
}

Setting class is a master in the association ("aggregate root"). It is quite unusual but it depends on problem domain...

Primary key association

public SettingMap()
{
    Id(x => x.Id).GeneratedBy.Guid();
    HasOne(x => x.Student).Cascade.All();
}

public StudentMap()
{
    Id(x => x.Id).GeneratedBy.Foreign("Setting");
    HasOne(x => x.Setting).Constrained();
}

and a new setting instance should be stored:

        var setting = new Setting();

        setting.Student = new Student();
        setting.Student.Name = "student1";
        setting.Student.Setting = setting;
        setting.Name = "setting1";

        session.Save(setting);

Foreign key association

public SettingMap()
{
    Id(x => x.Id).GeneratedBy.Guid();
    References(x => x.Student).Unique().Cascade.All();
}

public StudentMap()
{
    Id(x => x.Id).GeneratedBy.Guid();
    HasOne(x => x.Setting).Cascade.All().PropertyRef("Student");
}

Primary key association is close to your solution. Primary key association should be used only when you are absolutely sure that the association will be always one-to-one. Note that AllDeleteOrphan cascade is not supported for one-to-one in NH.

EDIT: For more details see:

http://fabiomaulo.blogspot.com/2010/03/conform-mapping-one-to-one.html

http://ayende.com/blog/3960/nhibernate-mapping-one-to-one

You're accessing the list by different threads, but a list is not threadsafe:

You could lock the list with:

lock(listTotalCost)
    listTotalCost.Add(temp.Value);

Or use Concurrent collections.

With Dictionary<,> you have to lock both reading and writing. So both

lock( ConnectionList ) {
   ConnectionList.Add( key, res );
}

and

lock( ConnectionList ) {
   res = ConnectionList[ key ];
}

and

lock( ConnectionList ) {
   int cnt = ConnectionList.Count;
}

and

lock( ConnectionList ) {
   ConnectionList.Clear();
}

and

lock( ConnectionList ) {
   foreach ( var kv in ConnectionList ) {
      // Do things
   }
}

and so on :-)

With ConcurrentDictionary<,> you don't need any locking, but note that the syntax is a little different than the one of the Dictionary<,>

I haven't used DataReaders for 3+ years, so I wanted to confirm my memory and found this. Anyway, for anyone who happens upon this post like I did and wants a method to test IsDBNull using the column name instead of ordinal number, and you are using VS 2008+ (& .NET 3.5 I think), you can write an extension method so that you can pass the column name in:

public static class DataReaderExtensions
{
    public static bool IsDBNull( this IDataReader dataReader, string columnName )
    {
        return dataReader[columnName] == DBNull.Value;
    }
}

Kevin