I’m a java newbie, and am having a very confusing issue with StackOverflow errors / ability to access files between classes. I understand that the underlying cause is likely that I have some recursive call, but the syntax of fixing it is escaping me. I think it has something to do with how the classes are linked through one extending another — but, if the InputScreen class doesn’t extend the ViewController, I can’t access the methods there that I need. I’ve put the high-level code below (making a program to track gas mileage).
Goal of this is to be able to open an xml file with some historical mileage data (using the doOpenAsXML() method), then allow the user to add data to some text fields (defined in the InputScreen class), add another data point to the ArrayList, and then save using the doSaveAsXML method.
Anyone have ideas on how to make this work? Thanks!!!
// Simple main just opens a ViewController window
public class MpgTracking {
public static void main(String[] args) {
ViewController cl = new ViewController();
cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cl.setVisible(true);
} // end main
}
public class ViewController extends JFrame {
// the array list that I want to fill using the historical data
public ArrayList<MpgRecord> hist;
public ViewController() {
doOpenAsXML(); // open historical data, put into 'hist'
InputScreen home = new InputScreen ();
}
public void doSaveAsXML() {
// ...long block to save in correct xml format
}
public void doOpenAsXML() {
// ...long block to open in correct xml format
}
}
public class InputScreen extends ViewController {
// statements to define a screen with text fields and a 'Save' button
// statements to create a listener on the Save button
// statements to add to the ArrayList hist, opened in the ViewController method
doSaveAsXML();
}
This seems to be the root cause of your problem:
To access (non-static) methods in another class, you need an object of this class. In your case, the InputStream would need to have a ViewController object, not to be a ViewController object. (On the same lines, the ViewController should not be a JFrame, but have one – although this does not give problems here.)
If you change this, you don’t get your constructor-loop.