Good Evening Forum,
A project I have been tasked with requires that I create a banking simulation program. I have designed my program to be initiated within a "Controller" class that creates a HashMap, places default variables to be grabbed by my View class and sent to one of the 3 panels that exist within the view. My "Input" class, has a series of textfields that contain default values I intend on placing in there with references to the HashMap I mentioned earlier, allow them to be changed, and then sent to the HashMap and update the internal values within it to be sent across the program.
In the creation of my view class, I am running into a Null Pointer Exception that appears to originate when my input panel is created from within my view. My HashMap values are stored (I have tested their existence by placing output checks when they're added BEFORE the view is created), but when my Constructor for my controller class calls my view, my view seems to try to create my panels inside the view but blows up when trying to create the "input" panel and points to my first textfield saying that my method for acquiring the default values to place in my textfield is null. It's not though! Allow me to now present my code:
The most important part of my Controller class:
public class SimController { private HashMap<String, Integer> variables; private CustomerProducer producer; private SimView view; public static void main(String[] args) { new SimController(); //Main method calls it's constructor to start the program } public SimController() { // Creates instance of HashMap and stores values variables = new HashMap(); variables.put("runTime", 10000); variables.put("maxNumTellers", 4); variables.put("meanCustomerArrivalTime", 240); variables.put("varCustomerArrivalTime", 20); variables.put("meanOpenAcctTime", 300); variables.put("varOpenAcctTime", 10); variables.put("meanCloseAcctTime", 200); variables.put("varCloseAcctTime", 8); variables.put("meanWithdrawTime", 150); variables.put("varWithdrawTime", 10); variables.put("meanDepositTime", 90); variables.put("varDepositTime", 5); variables.put("meanRegCustWaitTolerance", 300); variables.put("varRegCustWaitTolerance", 30); variables.put("meanBusyCustWaitTolerance", 120); variables.put("varBusyCustWaitTolerance", 20); // Nevermind this... no problem here. producer = new CustomerProducer(variables.get("meanCustomerArrivalTime"), variables.get("varCustomerArrivalTime"), variables.get("meanOpenAcctTime"), variables.get("varOpenAcctTime"), variables.get("meanCloseAcctTime"), variables.get("varCloseAcctTime"), variables.get("meanWithdrawTime"), variables.get("varWithdrawTime"), variables.get("meanDepositTime"), variables.get("varDepositTime"), variables.get("meanRegCustWaitTolerance"), variables.get("varRegCustWaitTolerance"), variables.get("meanBusyCustWaitTolerance"), variables.get("varBusyCustWaitTolerance")); JFrame frame = new JFrame("Banking Simulation"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); view = new SimView(this); //Creates my "view" class, which then proceeds to //blow up. } //Method for getting info stored in the HashMap public int getHashInfo(String x) { return variables.get(x); }
And my "view" class:
public class SimView extends JFrame { private SimController controller; private SimInputPanel panel; private SimOutputPanel panel2; private SimButtonPanel panel3; public SimView(SimController controller) { this.controller = controller; panel = new SimInputPanel(); panel2 = new SimOutputPanel(); panel3 = new SimButtonPanel(); getContentPane().add(panel, BorderLayout.NORTH); getContentPane().add(panel3, BorderLayout.CENTER); getContentPane().add(panel2, BorderLayout.SOUTH); setSize(600, 615); setVisible(true); } //Method to call the Controller and get a particular HashMap entry public int getControllerInfo(String x) { return controller.getHashInfo(x); }
Lastly... my troublesome class... the Input Class:
public class SimInputPanel extends JPanel { private SimView view; private JLabel headerText = new JLabel("Input Information"); private JLabel headerTextLine = new JLabel("-------------------------------------"); private JLabel runTimeLabel = new JLabel("Total Run Time:"); private JTextField runTime = new JTextField(Integer.toString(view.getControllerInfo("runTime"))); private JLabel maxNumTellersLabel = new JLabel("Maximum Number of Tellers:"); private JTextField tempMaxNumTellers = new JTextField(view.getControllerInfo("maxNumTellers")); private JLabel meanCustArrivalTimeLabel = new JLabel("Mean Customer Arrival" + " Time"); private JTextField tempMeanCustArrivalTime = new JTextField(view.getControllerInfo("meanCustArrivalTime")); private JLabel varCustArrivalTimeLabel = new JLabel("Variance (Customer Arrival" + " Time)"); private JTextField tempVarCustArrivalTime = new JTextField(view.getControllerInfo("varCustArrivalTime")); private JLabel meanOpenAcctTimeLabel = new JLabel("Mean Open Account Time"); private JTextField tempMeanOpenAcctTime = new JTextField(view.getControllerInfo("meanopenAcctTime")); private JLabel varOpenAcctTimeLabel = new JLabel("Variance (Open Account Time)"); private JTextField tempVarOpenAcctTime = new JTextField(view.getControllerInfo("varOpenAcctTime")); private JLabel meanCloseAcctTimeLabel = new JLabel("Mean Close Account Time"); private JTextField tempMeanCloseAcctTime = new JTextField(view.getControllerInfo("meanCloseAcctTime")); private JLabel varCloseAcctTimeLabel = new JLabel("Variance (Close Account Time)"); private JTextField tempVarCloseAcctTime = new JTextField(view.getControllerInfo("varCloseAcctTime")); private JLabel meanWithdrawTimeLabel = new JLabel("Mean Withdraw Time"); private JTextField tempMeanWithdrawTime = new JTextField(view.getControllerInfo("meanWithdrawTime")); private JLabel varWithdrawTimeLabel = new JLabel("Variance (Withdraw Time)"); private JTextField tempVarWithdrawTime = new JTextField(view.getControllerInfo("varWithdrawTime")); private JLabel meanDepositTimeLabel = new JLabel("Mean Deposit Time"); private JTextField tempMeanDepositTime = new JTextField(view.getControllerInfo("meanDepositTime")); private JLabel varDepositTimeLabel = new JLabel("Variance (Deposit Time)"); private JTextField tempVarDepositTime = new JTextField(view.getControllerInfo("varDepositTime")); private JLabel meanRegCustWaitToleranceLabel = new JLabel("Mean Regular Customer" + " Wait Tolerance"); private JTextField tempMeanRegCustWaitTolerance = new JTextField(view.getControllerInfo("meanRegCustWaitTolerance")); private JLabel varRegCustWaitToleranceLabel = new JLabel("Variance (Regular" + " Customer Wait Tolerance)"); private JTextField tempVarRegCustWaitTolerance = new JTextField(view.getControllerInfo("carRegCustWaitTolerance")); private JLabel meanBusyCustWaitToleranceLabel = new JLabel("Mean Busy Customer Wait" + " Tolerance"); private JTextField tempMeanBusyCustWaitTolerance = new JTextField(view.getControllerInfo("meanBusyCustWaitTolerance")); private JLabel varBusyCustWaitToleranceLabel = new JLabel("Variance (Busy Customer" + " Wait tolerance)"); private JTextField tempVarBusyCustWaitTolerance = new JTextField(view.getControllerInfo("varBusyCustWaitTolerance")); public SimInputPanel() { JPanel panInput = new JPanel(); panInput.setLayout(new GridLayout(17, 2)); panInput.add(headerText); panInput.add(headerTextLine); panInput.add(runTimeLabel); panInput.add(runTime); panInput.add(maxNumTellersLabel); panInput.add(tempMaxNumTellers); panInput.add(meanCustArrivalTimeLabel); panInput.add(tempMeanCustArrivalTime); panInput.add(varCustArrivalTimeLabel); panInput.add(tempVarCustArrivalTime); panInput.add(meanOpenAcctTimeLabel); panInput.add(tempMeanOpenAcctTime); panInput.add(varOpenAcctTimeLabel); panInput.add(tempVarOpenAcctTime); panInput.add(meanCloseAcctTimeLabel); panInput.add(tempMeanCloseAcctTime); panInput.add(varCloseAcctTimeLabel); panInput.add(tempVarCloseAcctTime); panInput.add(meanWithdrawTimeLabel); panInput.add(tempMeanWithdrawTime); panInput.add(varWithdrawTimeLabel); panInput.add(tempVarWithdrawTime); panInput.add(meanDepositTimeLabel); panInput.add(tempMeanDepositTime); panInput.add(varDepositTimeLabel); panInput.add(tempVarDepositTime); panInput.add(meanRegCustWaitToleranceLabel); panInput.add(tempMeanRegCustWaitTolerance); panInput.add(varRegCustWaitToleranceLabel); panInput.add(tempVarRegCustWaitTolerance); panInput.add(meanBusyCustWaitToleranceLabel); panInput.add(tempMeanBusyCustWaitTolerance); panInput.add(varBusyCustWaitToleranceLabel); panInput.add(tempVarBusyCustWaitTolerance); setLayout(new BorderLayout()); add(panInput, BorderLayout.NORTH); }
I apologize for it's extensive length, and if there is a more preferable method for me to post my questions in the future, I would be certainly happy to accommodate that. I've just been looking at this code for hours, and I can't seem to get any help from anyone on this... I don't just like throwing my code out there and being like, "Fix it", cause I certainly wouldn't want to insult profound programmers with my mundane tasks.
Thank you very much for any insight you could provide to me!