I am developing JAVA programs in the Eclipse environment, not so much for the sake of the program, as for the sake of experience (although the program is also necessary, because the course)

the reason for contacting the professionals on the forum is an error java.lang.StackOverflowError - stack overflow. The situation in which the error occurs is as follows: in the first class of ClInitVisualElements, the handler for the button

btnOperationPoint.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0){ screenManagement.AddPoint(); }}); 

calls the AddPoint () method; second class clScreenManagementOperations

 public void AddPoint() { if (!bPointIn) visualLink.AddToLastPosition("."); } 

AddPoint in turn checks for a comma in the string and then calls the first class method (bPointIn is a boolean type variable)

AddToLastPosition (); - the first class method from which the AddPoint () call was made

 public void AddToLastPosition(String Text) { String StrTemp = TfDisplayOperation.getText(); TfDisplayOperation.setText(StrTemp+Text); } 

here and without error it is clear that the recursion is present, but the singular one is a sequential call of three methods between two classes, for some reason there are errors that indicate two lines of code:

 ClScreenManagementOperations screenManagement = new ClScreenManagementOperations(); 

first class link to second

  ClInitVisualElements visualLink = new ClInitVisualElements(); 

reference in second grade to first

Please tell me how to solve the problem.

thanks in advance

  • Nothing is clear where the recursion is here .. we press the button, it calls AddPoint, and that one calls AddToLastPosition .. and where is the recursion here? And what can get stuck here is unclear. But it is clear that StackOverflowError definitely makes it clear where exactly the error is and what goes in cycles - cy6erGn0m
  • I'm googling - people write that a stack overflow arises from recursion between classes, there is no place for recursion, because there really aren't any cycles anywhere. all java.lang.StackOverflowError errors point to two lines of code in turn: ClScreenManagementOperations screenManagement = new ClScreenManagementOperations (); link in the first class to the second ClInitVisualElements visualLink = new ClInitVisualElements (); link in the second grade to the first one and I now have to do a pulse without a pulse ... - pj-infest
  • So you create a second in one class and a second in the first? Why do you need this? - cy6erGn0m
  • and how then from the first class should I call the second method and vice versa? - pj-infest
  • First, read this: [java code conventions] [1] further enlighten on the theme of patterns: 1) delegation pattern 2) mediator 3) observer 4) proxy [1]: oracle.com/technetwork/java/codeconv-138413.html - jmu

1 answer 1

It is necessary to create instances of classes separately, and then from one to another put down a link. Or even outside, and then both inform links to both. For example, something like this

 class ClInitVisualElements { private ClScreenManagementOperations screenManager; public void setScreenManager(ClScreenManagementOperations manager) { screenManager = manager; } } class ClScreenManagementOperations { private ClInitVisualElements visualElements; public void setVisualElement(ClInitVisualElements elements) { visualElements = elements; } } class Main { public static void main(String[] args) { ClInitVisualElements elements = new ClInitVisualElements(); ClScreenManagementOperations manager = new ClScreenManagementOperations(); elements.setScreenManager(manager); manager.setVisualElements(elements); } } 
  • Thanks for the idea, but I do not understand the code, if you can, add comments to the code - pj-infest
  • What other comments could there be? You just need not to create instances inside the classes, but create them outside. - cy6erGn0m
  • private ClScreenManagementOperations screenManager; // this is creating an instance of the ClScreenManagementOperations class public void setScreenManager (ClScreenManagementOperations manager) {screenManager = manager; I can not understand what the eta procedure does, and what the parameter means? - pj-infest
  • It simply puts a link from the >> >> parameter. Creating an instance of the ClScreenManagementOperations class is no creating an instance of the class - cy6erGn0m
  • those. now I can access the methods of the ClSCreeenManagementOperations class through the manager, registering in another class private ClScreenManagementOperations screenManager; public void setScreenManager (ClScreenManagementOperations manager) {screenManager = manager; } elements.setScreenManager (manager); I understood the code entry correctly? - pj-infest