0

Someone please help me with this code its showing array outofbond exception.

datain file new_hosts.txt is like:

test      test       28/6/2015    09:45 PM
test      fafagf     30/6/2015    01:00 PM
test      sfaasfag   28/6/2015    10:05 PM

code:

public void notification()
    {
        string regvalue;
        int ctr = 0;
        bool isNotificationExits=false;
        string[] meetingdata=new string[]{};
        string[] notificationsdata = new string[]{};
        try
        {
            //ctr = File.ReadAllLines("new_hosts.txt").Length;
            meetingdata = File.ReadAllLines("new_hosts.txt");
        }
        catch (FileNotFoundException)
        {
            //showmainmenu();
        }
        List<String> notificationsdata = new List<String>();
        foreach(string data in meetingdata)
        {
            string trimed = data;
            //Console.WriteLine(data);
            Regex d = new Regex(@"(\d+[\/]\d+[\/]\d+)");
            Regex t = new Regex(@"(\d+)([\:])(\d+)\s(PM|AM)");
            Match mt =t.Match(trimed);
            Match md = d.Match(trimed);
            regvalue = md.Value +" " +mt.Value;
            DateTime datetime = new DateTime();
            if (md.Success && mt.Success)
            {
                datetime = DateTime.ParseExact(regvalue, "d/M/yyyy hh:mm tt", new CultureInfo("en-US"), DateTimeStyles.None);
                //Console.Write(datetime);
            }
            else { Console.WriteLine("Opps someting Went Wrong Please Contact Developer...!!!"); }//this is not going to happend ever
            if (!(datetime < DateTime.Now))
            {                   
                if (datetime <= DateTime.Now.AddDays(5))
                {
                    //Console.WriteLine(ctr + "   you here");
                    isNotificationExits = true;
                    notificationsdata[ctr]=data; <<-----Array Out Of bond Exception here
                    ctr++;
                }
            }                
        }

        if(isNotificationExits==true)
        {
            Console.WriteLine("\n\t\tHey..! You have Some Reminders here\n ");
            Console.Write("Sr.no |");
            Console.Write("    Subject    |");
            Console.Write("    Place    |");
            Console.Write("     Date     |");
            Console.WriteLine("    Time");
            for(int j=0; j <= notificationsdata.Length ; j++)
            {
                Console.WriteLine(j + 1);
                Console.WriteLine(notificationsdata[j]);
            }
            Console.WriteLine();
        }

    }
3
  • Where does your problem occur? Commented Jun 28, 2015 at 20:13
  • Always post full error data. And minimize the code. Commented Jun 28, 2015 at 20:25
  • ok sir this is my first time here. sorry Commented Jun 29, 2015 at 15:24

3 Answers 3

4

In addition to Nikola's point which you'll also need to fix is that you haven't set a size for the array

string[] notificationsdata = new string[]{};

If you don't know what the size is ahead of time, you can use another data structure such as a List<string>. I see you've created a List<string> object but you've named it the same as the array. This is confusing so you should either rename one of them or delete one.

To use the list, replace

notificationsdata[ctr]=data;

with

notificationsdata.Add(data);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks sir for your help. notificationsdata.Add(data); is working.. but now i want to ask that if <list> notificationdata contains multiple line or data then how i retrive them in a loop or something like that.... for(int j=0; j <= notificationsdata.Exists ; j++) this is not working as .Lenght does not exits for list
ok sir i done like this int j = 0; foreach(string a in notificationsdata) { j++; Console.Write(" "+j +" |"); Console.WriteLine(a); } Console.WriteLine();
Hi Hardy, you're welcome - are you happy with the answer?
1

You are not specifying where exactly you get the exception, but I would take the (not-so) wild guess and tell you that this piece of code is what bugs you:

for(int j=0; j <= notificationsdata.Length ; j++)
{
    Console.WriteLine(j + 1);
    Console.WriteLine(notificationsdata[j]);
}

Problem is, you are iterating over the array starting from index zero until index notificationsdata.Length. However, the Length method returns the number of items in the array, therefore an index with the array's length wouldn't exist.

What you can do to prevent this is simply change the less-than-or-equal sign to just less-than:

for(int j=0; j < notificationsdata.Length; j++)

or subtract one from the loop maximum:

for(int j=0; j <= notificationsdata.Length - 1; j++)

1 Comment

This is correct but the error is being thrown earlier on in the code (notificationsdata[ctr]=data;). However, your answer still stands as that will be the next error to occur.
0

If you're using a List, you must add an item to the list using notificationsdata.Add(data).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.