0

Hey i have written some kind of Binary Search Tree, which has a insert method. So it gets a Object to insert, a Char Array and a Integer which gives it the Index to look at.

So this is the insert method :

public void insert(Buchstabe pBuchstabe,char[] pChar,int pStelle)
{
    if(pBuchstabe==null)
        return;

    if(baum.isEmpty())
    {
        baum=new BinaryTree(pBuchstabe);
    }
    else 
    if(pStelle <= pChar.length)
    {
        if(pChar[pStelle] == '.')
        {
            Mybaum lTree=this.getLeftTree();
            lTree.insert(pBuchstabe,pChar,pStelle+1);
            this.baum.setLeftTree(lTree.baum);
        }
        else
        if(pChar[pStelle]=='-')
        {
            Mybaum lTree=this.getRightTree();
            lTree.insert(pBuchstabe,pChar,pStelle+1);
            this.baum.setLeftTree(lTree.baum);
        }
    }
}

I have a Method which passes the required Parameters (in this case) : A Object Buchstabe,then the Char Array['.','.'] and the integer 0 to the insert method.

And i get a out of bounds error :

java.lang.ArrayIndexOutOfBoundsException: 2
at Mybaum.insert(Mybaum.java:22)
at Mybaum.insert(Mybaum.java:25)
at Mybaum.insert(Mybaum.java:25)
at Mörserbaum.einlesen(Mörserbaum.java:42)

Does anyone know what ive made wrong ?

2 Answers 2

0

Looks like you have an issue

 if(baum.isEmpty())
    {
        baum=new BinaryTree(pBuchstabe);
    }
    else 
    **if(pStelle <= pChar.length)**
    {
        **if(pChar[pStelle] == '.')**
        {
            Mybaum lTree=this.getLeftTree();
            lTree.insert(pBuchstabe,pChar,pStelle+1);
            this.baum.setLeftTree(lTree.baum);
        }

if(pChar[pStelle] == '.') -- you get indexOutOfBound bc/ you need to say if(pChar[pStelle-1] == '.') .. since Java array index starts from 0, if the length is 5, last index would be pChar[4]...

there could be more issues with this code, since we don't have full code/context i can't speculate more.. but this is one of the reason you could get indexoutofbound

Sign up to request clarification or add additional context in comments.

7 Comments

I dont think so because i pass 0 as parameter to the method at the beginning. I changed 'if(pStelle <= pChar.length)' to 'if(pStelle < pChar.length' now it does not create a error but it does not work as it should ill post the complete code here
in your code can you tell me what is at line 22? java.lang.ArrayIndexOutOfBoundsException: 2 at Mybaum.insert(Mybaum.java:22)
also I would write a small unit test to find which case exactly produces the error.. I would also suggest to generify your code to make it easier to understand
line 22 is the if statement : if(pChar[pStelle] == '.')
what does generify mean ? :D
|
0
public void einlesen()
{
    Buchstabeenschlange sch = new Buchstabeenschlange();
    for(int i = 0;i<codeTabelle.length;i++)
    {
        Buchstabe a = new Buchstabe(alphabet[i],codeTabelle[i]);
        if(a == null)
        {
            System.out.println("Buchstabe mit Error == "+a);
        }
        System.out.println("Buchstabe == "+a);
         sch.hinzufuegen(a);

        System.out.println("------------");

    }
    List l = sch.gibListe();
    sch.druckeListe();
    l.toFirst();
    while(l.hasAccess())
    {

      Buchstabe buch = (Buchstabe) l.getObject();  
      char[] code = buch.getCode().toCharArray();
      baum.insert(buch,code,0);
      l.next();
    }
    TreeViewGUI view = new TreeViewGUI(baum);
}

This creates the object Buchstabe and sorts it in a List so that you have the shortest Strings at the beginning. Then it inserts them into a Binaray Tree and displays it.

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.