calling up different classes verschiedene Klassenaufrufen

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

calling up different classes verschiedene Klassenaufrufen

Postby donler » Thu Jan 13, 2011 7:22 pm

Hi wir haben hier nen kleines Problem und es währe net wenn jemand uns möglichst schnell helfen könnte. Wir wollen ein etwas größeres Projekt machen bei dem es sich kaum vermeiden lässt von der ersten Klasse eine zweite klasse aufzurufen, welche dann wieder eine neue Klasse aufruft. Dies funktioniert aber wenn man jetzt vonn der letzten Klasse wieder die zweite Klasse aufrufen will dann bekommt man so eine komische StackOverflow Error message -.-
Hier ist ein Beispiel Projekt bei dem der Fehler auftritt.

Hi we've got a little problem and it would be good if someone could answer us as quik as possible. We want to do a bit larger projekt in wich you cant avoid calling from the first class a second class and then from this class another. Until now it works but if you want to call up the second class from the third class you'll get a StackOverflow Error message -.-
Here is a example projekt, where the Error message comes up.

erste Klasse/ first class:
Code: Select all
import lejos.nxt.*;
import javax.microedition.lcdui.*;


public class Test1
{
    public Test1()
    {
       
    }
   
    public void Init()
    {
        LCD.drawString("Test1",0,0);
    }
   
    public static void main(String[] args)
    {
        Test1 test1 = new Test1();
        Test2 test2 = new Test2();
       
        test1.Init();
        test2.Init();
       
        while (true){
            try {
                 Thread.sleep(100000);
            } catch(InterruptedException ie) { }
        }
    }
}


zweite Klasse/ second class:
Code: Select all
import lejos.nxt.*;
import javax.microedition.lcdui.*;

public class Test2
{
    Test3 test3;
    Test1 test1;
   
    public Test2(){
        test1 = new Test1();
        test3 = new Test3();
    }
   
    public void Init()
    {
        LCD.drawString("Test2",0,1);
        test3.Init();
    }
}


dritte Klasse/third class:
Code: Select all
import lejos.nxt.*;
import javax.microedition.lcdui.*;

public class Test3
{
    Test2 test2;
   
    public Test3()
    {
        test2 = new Test2();
    }
   
    public void Init()
    {
        LCD.drawString("Test3",0,2);
       
    }
}

Hat jemand für dieses Problem eine Lösung?
Danke schon mal im voraus!

Has someone a solution for this problem?
Thanks in advance!
donler
New User
 
Posts: 2
Joined: Thu Jan 13, 2011 6:54 pm

Postby gloomyandy » Thu Jan 13, 2011 10:59 pm

You have an infinite recursion between the constructors in class test2 and test3 (test2 creates an instance of test2 which creates an instance of test which creates...), so you need to fix this... Without seeing your real code it is hard to provide the best way to resolve your problem...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4112
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby donler » Fri Jan 14, 2011 3:47 pm

Hi Andy thanks to your reply we now have a solution for the problem:
We have a class in wich we creat an object of every class we've got in our project at the beginning. Then this class cals up an init method in all classes wich creat an object of all the classes they need to communicat with later. So then we won't creat an object of them later.

Thanks for the quik reply again!
donler
New User
 
Posts: 2
Joined: Thu Jan 13, 2011 6:54 pm


Return to NXJ Projects

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests

more stuff