Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Bank Simulation - Null Pointer Exception

  1. #1

    Default Bank Simulation - Null Pointer Exception

    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!


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Bank Simulation - Null Pointer Exception

    1. Kindly indent properly so that it could be easy to read.
    2.
    panel2 = new SimOutputPanel();
    panel3 = new SimButtonPanel();
    Where are these? Hopefully you have them...
    3.
    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);
        }
    As far as i see, i think your panel has scope only inside the constructor and when it runs out of it, it throws NULL as there is no such object exists outside. Throw this statement as global to simInputPanel() and see.
    JPanel panInput = new JPanel();
    4.
    getContentPane().add(panel, BorderLayout.NORTH);
            getContentPane().add(panel3, BorderLayout.CENTER);
            getContentPane().add(panel2, BorderLayout.SOUTH);
    getContentPane of which Container? There should be a container that will call this getContentPane().

  3. #3

    Default Re: Bank Simulation - Null Pointer Exception

    I noticed a few of those observances you made and made adjustments without prevail. I moved the creation of panInput into the global variables as you had suggested and moved the creation of my Frame into the view as it should be (I don't know why I didn't in the first place) so the view code now looks like:

    public class SimView extends JFrame {
     
        private SimController controller;
        private SimInputPanel panel;
        private SimOutputPanel panel2;
        private SimButtonPanel panel3;
     
        public SimView(SimController controller) {
     
            JFrame frame = new JFrame("Banking Simulation");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            this.controller = controller;
            panel = new SimInputPanel();
            panel2 = new SimOutputPanel();
            panel3 = new SimButtonPanel();
            frame.getContentPane().add(panel, BorderLayout.NORTH);
            frame.getContentPane().add(panel3, BorderLayout.CENTER);
            frame.getContentPane().add(panel2, BorderLayout.SOUTH);
     
            setSize(600, 615);
            setVisible(true);
        }

    I am still, however, receiving the error... the exact exception I am getting is:

    Exception in thread "main" java.lang.NullPointerException
    at bankSimulationFinal.SimInputPanel.<init>(SimInputP anel.java:16)
    at bankSimulationFinal.SimView.<init>(SimView.java:23 )
    at bankSimulationFinal.SimController.<init>(SimContro ller.java:55)
    at bankSimulationFinal.SimController.main(SimControll er.java:17)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 3 seconds)
    When I look to the reference in SimInputPanel, it goes to this line:

    private JTextField runTime = new JTextField(Integer.toString(view.getControllerInfo ("runTime")));

    Which it would appropriately give this error for every instance of the TextFields for all of the variables... my path to my methods should be functioning correctly... communication to my view, which then communicates to my controller and returns the value to the panel in the form of an Integer from the HashMap and converts it into a String the TextField can use to appropriately set its defaults.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Bank Simulation - Null Pointer Exception

    view = new SimView(this);
    Once you exit the form, why do you hope SimView to show "this". What "this" exists? Not exists. SO, you either need to hide instead of closing and then using this. Because as soon as you exit that form, there is null and with this statement, it throws null exception.

  5. #5

    Default Re: Bank Simulation - Null Pointer Exception

    Well... to be honest, the problem was actually quite simple... it pains me to think I spent several hours on a 10 minute solution.
    My panels had no means of communicating to my view. By sending to each panel's constructor an instance of my view with the line "panel = new SimInputPanel(this);" and for the other correlating panels, I could then immediately instantiate them within the constructor by making "this.view = view", and thus the communication is complete...

Similar Threads

  1. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 10:07 AM
  2. Null Pointer Exception Help!!
    By puzzledstudent in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 11th, 2010, 06:46 PM
  3. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM
  4. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM

Tags for this Thread