I was working on a fairly large program and thought it was time to separate my classes. 1.java file for the GUI code and 1.java file for the mechanics behind the functions displayed by the graphical interface. But here is my problem, I created an instance of each class inside each other, and then the program refuses to run, so I'm obviously doing something wrong. In my RPG class, I have the following line of code:
public Mechanics mechanics = new Mechanics();
And for my Mechanics class, I have this code:
public RPG rpg = new RPG();
The reason I do this was to try it: Many of my variables are in the RPG class, and I want to be able to call them from my RPG and manipulate them, and then send them back to the RPG . Here is the code I used to test this function (from my Mechanics class):
class Mechanics{ public RPG rpg = new RPG(); public Mechanics(){ } public void helloWorld(){ System.out.println("Hello World!"); System.out.println("Health before:"+rpg.Health); rpg.Health = rpg.Health - 5; System.out.println("Health after:"+rpg.Health); } }
Yes, Health is a public int in my RPG class.
And in my RPG class, this is the code I use to test the Mechanics class:
mechanics.helloWorld();
Here is my problem: the code compiles, but then when I try to run it, I get this error:
at Mechanics.<init>(Mechanics.java:15) at RPG.<init>(RPG.java:127)
Here is my question. Am I doing it right? What is wrong with my code that my program does not want to work?
ADDED: I tried to name my other classes as private , and the program compiles and still refuses to run and gives me the same error
Line 15 from Mechanics :
public RPG rpg = new RPG();
line 127 from the RPG :
public Mechanics mechanics = new Mechanics();