Solved Annoying error

Discussion in 'Plugin Development' started by Coopah, Jan 30, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Coopah

    So it seems my casting is wrong, but I can't seem to figure out why.

    My classes seem to be ran, but stop at these lines:
    Code:
    public class bmSkills {
    
    Enable plugin;
        BroodMother bm;
    
        public bmSkills(Enable plugin) {
    
            this.plugin = plugin;
          //error line:
      bm = new BroodMother(plugin);
       
        }
       }
    
    Code:
    public class BroodMother {
    
        Enable plugin;
        bmSkills skills;
    
        public BroodMother(Enable plugin) {
    
            this.plugin = plugin;
          //error line:
            skills = new bmSkills(plugin);
    
        }
    }
    The console errors are:
    [​IMG]

    I'm assuming 'init' means initiation or instantiate? Other than that, I'm lost!
     
  2. Offline

    teej107

  3. Offline

    Coopah

    @teej107
    Oh because I'm casting the class to one another, solution? I don't want to make things static and would prefer if the whole class has access to it. Don't want to call it in every method.
     
  4. Offline

    I Al Istannen

    @Coopah Your class starts with a lower case letter. It really shouldn't there are certain conventions to make the reading of the code easier. If you follow them it will be much easier to find out what is going on. You can find some here, they should also be relevant today.

    To your problem:
    You are doing no casts if im right, but just instantiate objects. You don't need to make variables static to have access to them in the whole class. You can just create fields outside of any method / constructor, just like you did. You do realize, you can pass more than one object by using constructors?

    Have a look at what teej107 wrote and maybe look up what "recursion" means in programming languages. It should become obvious what the problem is and how you solve it.
     
  5. Offline

    Coopah

    @I Al Istannen
    As for conventions, I gave them a second look over. As for why I didn't use it, I honestly didn't like the look of 'BMskills' (preferred 'bmSkills').

    Fixed the issue, but I have a little question on statics etc.
    I have a class of methods and I used a constructor to get access of the class through another class. However, I then had another class which had more methods in it and I wanted to use those methods inside of my other class of methods. You get what I'm saying? I had to pass them both through the constructor (which caused the error). How would I fix this without statics?
     
  6. Offline

    timtower Administrator Administrator Moderator

  7. Offline

    Coopah

    @timtower
    Getters and setters create and return values. Doesn't really answer my question. I referring my methods to them ahaha.
     
  8. Offline

    timtower Administrator Administrator Moderator

    @Coopah You have 2 classes with methode, creatie them in the main class, add getters to be able to use them everywhere
     
  9. Offline

    I Al Istannen

    @Coopah If you read your code in the future you can say for sure, that everything starting with a capital letter is a class/interface, everything with a lower case is a variable and everything in caps is a constant. That is quite handy to know.

    If you want to use methods of other classes, you would need to either pass them through the constructor or make the methods static (or a static way to obtain an instance). You can use static methods for utils or things that are dependent on the class and not the object. Any other object can either be passed through the constructor or method arguments.

    Your code

    Code:
    public bmSkills(Enable plugin) {
      this.plugin = plugin;
      bm = new BroodMother(plugin); // this gets called EVERY time a new instance of bmSkills is created
    // CREATES a new instance of BroodMother
    }
    and

    Code:
    public BroodMother(Enable plugin) {
      this.plugin = plugin;
      skills = new bmSkills(plugin);  // this gets called EVERY time a new instance of BroodMother is created.
    // CREATES a new instance of bmSkills
    }
    See the error? A new bmSkills is created. This creates a new BroodMother. The BroodMother creates a new bmSkills........... . An infinitve loop. This goes on and on until your server runs out of memory for the new instances.The solution is to NOT make an infinitive loop.

    Could you say what the classes will do in the future? Is there some kind of hierachie? Does every BroodMother need to create a new bmSkills? Does every bmSkills need to create a new BroodMother? Can one BroodMother be passed to several bmSkills? Do you just need one instance of both classes in the whole plugin? You have to find a solution based on the function of the classes.
     
  10. Offline

    Coopah

    @I Al Istannen
    Oh sorry man, I must have mislead you. I already solve that error ahaha. I was just asking for a better way to call for methods inside other method classes. Don't worry anymore!
     
  11. Offline

    I Al Istannen

    @Coopah That was what I tried to say in the last paragraph. It depends on the classes, the methods and the structure of your program. You could create a Util class with static methods. You could pass the other classes through the constructor. You could make a manager class or use your main and then make getter for the classes. You could even use a singelton although that is getting more and more discouraged.
     
    Coopah likes this.
Thread Status:
Not open for further replies.

Share This Page