-5

I'm beginner and looking for some help when trying to get this code working. "public override string ToString()" method shouldn't be touched as that is correct. But the code returns an error below. What I might be doing wrong?

Unhandled Exception: System.IndexOutOfRangeException: Array index is out of range. at Jumppa..ctor (System.String nimi, System.String[] ajat, System.String[] paikat) [0x00000] in :0 at Jumppa.Main (System.String[] args) [0x00000] in :0 [ERROR] FATAL UNHANDLED EXCEPTION: System.IndexOutOfRangeException: Array index is out of range. at Jumppa..ctor (System.String nimi, System.String[] ajat, System.String[] paikat) [0x00000] in :0 at Jumppa.Main (System.String[] args) [0x00000] in :0

  Using System;
    using System.Collections.Generic;

class Jumppa
{
      public override string ToString()
  {
      string jumppa = nimi+"\najat:\n";
      for (int i=0; i < ajat.Length; i++)
      {
      jumppa+= ajat[i] + "\n";
      }

      jumppa += "\npaikat:\n";

      for (int i=0; i < paikat.Length; i++)
      {
      jumppa+= paikat[i] + "\n";
      }
      return jumppa;
   }

    public string nimi;
    public string[] ajat=new string[2];
    public string[] paikat=new string[2];


    public Jumppa(string nimi,string[] ajat,string[]paikat)
    {
    this.nimi = nimi;
    this.ajat[2] = ajat[2];
    this.paikat[2] = paikat[2];
    }

    public Jumppa jumppa(Jumppa J1)
    {
        return this;
    }


    static void Main(string[] args)

    {
    //System.Collections.ArrayList jumppa = new System.Collections.ArrayList();


        Jumppa[] J1=new Jumppa[3];

        J1[0]  = new Jumppa ("junior",new[] {"Wedn 9:30", "Frid 9:30"},new[]{"Gym", "Basement"});
        J1[1]  = new Jumppa ("adult",new[] {"Wedn 10:30", "Frid 10:30"},new[]{"Gym", "Basement"});
        J1[2]  = new Jumppa ("oldlady",new[] {"Wedn 11:30", "Frid 11:30"},new[]{"Gym", "Basement"});



    //jumppa.Add(J1);

        //for (int i=0; i<2; i++)
        //{
    Console.WriteLine(J1[0].jumppa(J1[0]));
        //}
        return;   
        }

    }
rianjs
  • 7,767
  • 5
  • 24
  • 40
KP2
  • 1
  • 2
  • 1
    Why not use a debugger and see by yourself what’s going on? – Uwe Keim Oct 29 '17 at 12:07
  • 4
    Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Progman Oct 29 '17 at 12:09
  • Thanks for reply. I'm using a school's web-based program without a proper debugger. Good suggestion. I haven't yet installed a proper compiler. I should, yes. – KP2 Oct 29 '17 at 12:10
  • Visual Studio Community is free. If you're on a Mac, you can use .NET Core with VSCode with the C# extension. (Actually you can do that on Windows, too.) – rianjs Oct 29 '17 at 12:13
  • Thanks for good suggestions and sorry for asking stupid questions :) I'm at Windows, going to download Visual Studio Community. – KP2 Oct 29 '17 at 12:16

1 Answers1

3

The problem is on these lines:

this.ajat[2] = ajat[2];
this.paikat[2] = paikat[2];

ajat and paikat are initialized with length 2, so valid indices are 0 and 1. Accessing index 2 causes an IndexOutOfRangeException. I suspect what you really want to do is this:

this.ajat = ajat;
this.paikat = paikat;
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758