OutOfBoundsException but whyyy?

public class CopyOfSTEUERUNG
{
    KARTE[][] spielfeld;
    KARTE[][] KWahl;
    Rechteck black;
    int freieKarten;
    int x2;
    int y2;
    

    public CopyOfSTEUERUNG()
    {
        spielfeld = new KARTE[4][4];
        KWahl = new KARTE[4][4];
        freieKarten = 0;
        black = new Rechteck();
        black.GrößeSetzen(10, 430);
        black.PositionSetzen(440, 0);
        black.FarbeSetzen("schwarz");
        
        for(int i= 0; i<spielfeld.length; i++)
        {
         spielfeld[0][i]=new KARTE0(i*110, 0);
         spielfeld[1][i]=new KARTE0(i*110, 110);
         spielfeld[2][i]=new KARTE0(i*110, 220);
         spielfeld[3][i]=new KARTE0(i*110, 330 );

        }
    }

void KartenPlatzieren()
    {
        KWahl[0][0]=new KARTE1(0+460, 0);
        KWahl[0][1]=new KARTE2(110+460, 0);
        KWahl[0][2]=new KARTE3(220+460, 0);
        KWahl[0][3]=new KARTE4(330+460, 0);
        KWahl[1][0]=new KARTE5(0+460, 110);
        KWahl[1][1]=new KARTE6(110+460, 110);
        KWahl[1][2]=new KARTE7(220+460, 110);
        KWahl[1][3]=new KARTE8(330+460, 110);
        KWahl[2][0]=new KARTE9(0+460, 220);
        KWahl[2][1]=new KARTE10(110+460, 220);
        KWahl[2][2]=new KARTE11(220+460, 220);
        KWahl[2][3]=new KARTE12(330+460, 220);
        KWahl[3][0]=new KARTE13(0+460, 330);
        KWahl[3][1]=new KARTE14(110+460, 330);
        KWahl[3][2]=new KARTE15(220+460, 330);
        KWahl[3][3]=new KARTE16(330+460, 330);
    }

    void KartenEntfernen()
    {
        for(int i=0;i<KWahl.length; i++)
        {
            KWahl[0][i].Entfernen();
            KWahl[1][i].Entfernen();
            KWahl[2][i].Entfernen();
            KWahl[3][i].Entfernen();
        }      
    }
 void KarteWahl(int x, int y, int k)
    {
        switch(k)
        {
            case 0:
                spielfeld[y][x]=new KARTE0(x*110,y*110);
                break;
            case 1:
                spielfeld[y][x]=new KARTE1(x*110,y*110);
                break;
            case 2:
                spielfeld[y][x]=new KARTE2(x*110,y*110);
                break;
            case 3:
                spielfeld[y][x]=new KARTE3(x*110,y*110);
                break;
            case 4:
                spielfeld[y][x]=new KARTE4(x*110,y*110);
                break;
            case 5:
                spielfeld[y][x]=new KARTE5(x*110,y*110);
                break;
            case 6:
                spielfeld[y][x]=new KARTE6(x*110,y*110);
                break;
            case 7:
                spielfeld[y][x]=new KARTE7(x*110,y*110);
                break;
            case 8:
                spielfeld[y][x]=new KARTE8(x*110,y*110);
                break;
            case 9:
                spielfeld[y][x]=new KARTE9(x*110,y*110);
                break;
            case 10:
                spielfeld[y][x]=new KARTE10(x*110,y*110);
                break;
            case 11:
                spielfeld[y][x]=new KARTE11(x*110,y*110);
                break;
            case 12:
                spielfeld[y][x]=new KARTE12(x*110,y*110);
                break;
            case 13:
                spielfeld[y][x]=new KARTE13(x*110,y*110);
                break;
            case 14:
                spielfeld[y][x]=new KARTE14(x*110,y*110);
                break;
            case 15:
                spielfeld[y][x]=new KARTE15(x*110,y*110);
                break;
            case 16:
                spielfeld[y][x]=new KARTE16(x*110,y*110);
                break;
        }
        x2 = x;
        y2 = y;
    }
    int freieKartenAngeben()
    {
    freieKarten = 0;
        for(int i=0; i<3; i++)
        {
            for( int j=0;j<3;j++)
            {
              if(spielfeld[i][j] instanceof KARTE0)
              {
                freieKarten++;
                }
            }
        }
        return freieKarten;
    
    }
    void f()
    {
     KarteWahl(x2,y2,0);
    }
     boolean PlatzierungKorrekt(){ 
        boolean spf = !(spielfeld[y2][x2] instanceof KARTE0); 
    //Ecken Karten    
        if(!(spielfeld[0][0] instanceof KARTE0))
        {
         if((!(spielfeld[0][1] instanceof KARTE0)))
         {
            if((spielfeld[0][0].obenrechts != spielfeld[0][1].obenlinks) || (spielfeld[0][0].untenrechts != spielfeld[0][1].untenlinks))
            {
                f();
                return false;
            
            }
            
        }
        if((!(spielfeld[1][0] instanceof KARTE0)))
        {
         if((spielfeld[1][0].obenrechts != spielfeld[0][0].untenrechts) || (spielfeld[1][0].obenlinks != spielfeld[0][0].untenlinks))
         {
            f();
            return false;
            }
        
        }
    }
    if(!(spielfeld[3][0] instanceof KARTE0))
     {
        if((!(spielfeld[3][1] instanceof KARTE0)))
        {
         if((spielfeld[3][0].obenrechts != spielfeld[3][1].obenlinks) || (spielfeld[3][0].untenrechts != spielfeld[3][1].untenlinks))
         {
            f();
            return false;
            }
        
        }
        
        if((!(spielfeld[2][0] instanceof KARTE0)))
        {
         if((spielfeld[2][0].untenlinks != spielfeld[3][0].obenlinks) || (spielfeld[2][0].untenrechts != spielfeld[3][0].obenrechts))
         {
            f();
            return false;
            }
        
        }
            if(!(spielfeld[0][3] instanceof KARTE0))
        {
         if((!(spielfeld[0][2] instanceof KARTE0)))
         {
            if((spielfeld[0][2].obenrechts != spielfeld[0][3].obenlinks) || (spielfeld[0][2].untenrechts != spielfeld[0][3].untenlinks))
            {
                f();
                return false;
            
            }
            
        }
        if((!(spielfeld[1][3] instanceof KARTE0)))
        {
         if((spielfeld[1][3].obenrechts != spielfeld[0][3].untenrechts) || (spielfeld[1][3].obenlinks != spielfeld[0][3].untenlinks))
         {
            f();
            return false;
            }
        
        }
    }
        if(!(spielfeld[3][3] instanceof KARTE0))
        {
         if((!(spielfeld[3][2] instanceof KARTE0)))
         {
            if((spielfeld[3][2].obenrechts != spielfeld[3][3].obenlinks) || (spielfeld[3][2].untenrechts != spielfeld[3][3].untenlinks))
            {
                f();
                return false;
            
            }
            
        }
        if((!(spielfeld[2][3] instanceof KARTE0)))
        {
         if((spielfeld[3][3].obenrechts != spielfeld[2][3].untenrechts) || (spielfeld[3][3].obenlinks != spielfeld[2][3].untenlinks))
         {
            f();
            return false;
            }
        
        }
    }
        }
    //Kanten 
    if((x2== 0 && 0<y2<<spielfeld[y2].length))
    {   
     if(!(spielfeld[y2+1][x2] instanceof KARTE0))
     {
        if((spielfeld[y2][x2].obenrechts != spielfeld[y2+1][x2].obenlinks) || (spielfeld[y2][x2].untenrechts != spielfeld[y2+1][x2].untenlinks))
        {`your text`
         f();
         return false;
        }
        }
     if(!(spielfeld[-1+y2][x2] instanceof KARTE0))
     {
        if((spielfeld[y2][x2].obenlinks != spielfeld[-1+y2][x2].untenlinks) || (spielfeld[y2][x2].obenrechts != spielfeld[-1+y2][x2].untenrechts))
        {
         f();
         return false;
        }
        
        }
     if(!(spielfeld[y2][x2+1] instanceof KARTE0))
     {
        if((spielfeld[y2][x2].obenrechts != spielfeld[y2][x2+1].obenlinks) || (spielfeld[y2][x2].untenrechts != spielfeld[y2][x2+1].untenlinks))
        {
         f();
         return false;
        }
        
        }   
    }
       
    
    if(0<x2<<spielfeld[x2].length && y2==3)
    {
     if(!(spielfeld[y2-1][x2] instanceof KARTE0))
     {
     if((spielfeld[y2][x2].obenlinks != spielfeld[y2-1][x2].untenlinks) || (spielfeld[y2][x2].obenrechts != spielfeld[y2-1][x2].untenrechts))
     {
        f();
        return false;
        
        }
     }
     if(!(spielfeld[y2][x2-1] instanceof KARTE0))
     {
        if((spielfeld[y2][x2].obenlinks != spielfeld[y2][x2-1].obenrechts) || (spielfeld[y2][x2].untenlinks != spielfeld[y2][x2-1].untenrechts))
        {
          f();
          return false;  
        }
        }
     if(!(spielfeld[y2][x2+1] instanceof KARTE0))
     {
        if((spielfeld[y2][x2].obenrechts != spielfeld[y2][x2+1].obenlinks) || (spielfeld[y2][x2].untenrechts != spielfeld[y2][x2+1].untenlinks))
        {
         f();
         return false;
        }
        }
    }
    
    if(x2 == 3 && 0<y2<<spielfeld[y2].length)
    {
     if(!(spielfeld[y2-1][x2] instanceof KARTE0))
     {
        if((spielfeld[y2][x2].obenlinks != spielfeld[y2-1][x2].untenlinks) || spielfeld[y2][x2].obenrechts != spielfeld[y2-1][x2].untenrechts)
        {
         f();
         return false;
        }
     }
     if(!(spielfeld[y2+1][x2] instanceof KARTE0))
     {
        if((spielfeld[y2][x2].untenlinks != spielfeld[y2+1][x2].obenlinks) || spielfeld[y2][x2].untenrechts != spielfeld[y2+1][x2].obenrechts)
        {
         f();
         return false;
        }
        }
     if(!(spielfeld[y2][x2-1] instanceof KARTE0))
     {
        if((spielfeld[y2][x2].obenlinks != spielfeld[y2][x2-1].obenrechts) || spielfeld[y2][x2].untenlinks != spielfeld[y2][x2-1].untenrechts)
        {
         f();
         return false;
        }
        }
    }
    
    
    if(0<x2<<spielfeld[x2].length && y2== 0)
    {
     if(!(spielfeld[y2][x2-1] instanceof KARTE0))
     {
        if((spielfeld[y2][x2].obenlinks != spielfeld[y2][x2-1].obenrechts) || spielfeld[y2][x2].untenlinks != spielfeld[y2][x2-1].untenrechts)
        {
         f();
         return false;
        }
     }
     if(!(spielfeld[y2][x2+1] instanceof KARTE0))
     {
        if((spielfeld[y2][x2].obenrechts != spielfeld[y2][x2+1].obenlinks) || spielfeld[y2][x2].untenrechts != spielfeld[y2][x2+1].untenlinks)
        {
         f();
         return false;
        }
     }
     if(!(spielfeld[y2+1][x2] instanceof KARTE0))
     {
        if((spielfeld[y2][x2].untenlinks != spielfeld[y2+1][x2].obenlinks) || spielfeld[y2][x2].untenrechts != spielfeld[y2+1][x2].obenrechts)
        {
         f();
         return false;
        }
        }
    }
    
    //Mittleren Karten
    if(0<x2<<(spielfeld[x2].length-1) && 0<y2<<(spielfeld[y2].length-1))
    {
     if(!(spielfeld[y2][x2-1] instanceof KARTE0))
     {
        if((spielfeld[y2][x2].obenlinks != spielfeld[y2][x2-1].obenrechts) || spielfeld[y2][x2].untenlinks != spielfeld[y2][x2-1].untenrechts)
        {
         f();
         return false;
        }
     }
     if(!(spielfeld[y2][x2+1] instanceof KARTE0))
     {
        if((spielfeld[y2][x2].obenrechts != spielfeld[y2][x2+1].obenlinks) || spielfeld[y2][x2].untenrechts != spielfeld[y2][x2+1].untenlinks)
        {
         f();
         return false;
        }
        }
     if(!(spielfeld[y2+1][x2] instanceof KARTE0))
     {
        if((spielfeld[y2][x2].untenlinks != spielfeld[y2+1][x2].obenlinks) || spielfeld[y2][x2].untenrechts != spielfeld[y2+1][x2].obenrechts)
        {
         f();
         return false;
        }
        }   
     if(!(spielfeld[y2-1][x2] instanceof KARTE0))
     {
        if((spielfeld[y2][x2].obenlinks != spielfeld[y2-1][x2].untenlinks) || spielfeld[y2][x2].obenrechts != spielfeld[y2-1][x2].untenrechts)
        {
         f();
         return false;
        }
     }
    }
     return true;
    }

    void Vorlage2()
    {
        //nicht lösbar
        KarteWahl(1, 0, 7);
        KarteWahl(2, 1, 9);
        KarteWahl(0, 3, 14);
        KarteWahl(3, 0, 1);
    }

    void Vorlage3()
    {
        //schwer
        KarteWahl(2, 1, 16);
        KarteWahl(3, 1, 11);
        KarteWahl(1, 3, 8);
        KarteWahl(0, 1, 10);
        KarteWahl(3, 3, 13);
        KarteWahl(0, 0, 11);
        KarteWahl(0, 3, 10);
        KarteWahl(3, 0, 3);
        KarteWahl(2, 3, 15);
        KarteWahl(3, 2, 12);
    }
    

    
     boolean LösungVorhanden()
        {
            if(freieKartenAngeben() != 2 || !PlatzierungKorrekt())
            {
                return false;
            }
            int posx = -1;
            int posy = -1;
            int posix = -1;
            int posiy = -1;
            for(int i = 0;i<spielfeld.length;i++)
            {
                for(int j = 0;j<spielfeld[i].length;j++)
                {   
                    if(spielfeld[j][i] instanceof KARTE0)
                    {
                        posx = i;
                        posy = j;
                        for(int k=1;k<=16;k++)
                        {
                            KarteWahl(posx, posy, k);
                            if(PlatzierungKorrekt())
                            {
                                for(int l=0;l<spielfeld.length;l++)
                                {
                                    for(int m=0;m<spielfeld[l].length;m++)
                                    {
                                        if(spielfeld[m][l] instanceof KARTE0)
                                        {
                                            posix = l;
                                            posiy = m;
                                            for(int g = 1;g<=16;g++)
                                            {
                                                KarteWahl(posix, posiy, g);
                                                if(PlatzierungKorrekt())
                                                {
                                                    KarteWahl(posx, posy, 0);
                                                    KarteWahl(posix, posiy, 0);
                                                    return true;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

        return false;
    }
    }

` at line 360 I get a OutOfBoundsException: Index 4 out of bounds for length 4

-> if(!(spielfeld[y2][x2+1] instanceof KARTE0))
{
if((spielfeld[y2][x2].obenrechts != spielfeld[y2][x2+1].obenlinks) || spielfeld[y2][x2].untenrechts != spielfeld[y2][x2+1].untenlinks)
{
f();
return false;
}
}

I dont know why there is a Exception because its basically the same code as in the other if brackets.`

New contributor

user25549156 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật