Results 1 to 13 of 13
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By jim829

Thread: Null Pointer Error

  1. #1
    rebelguy13 is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Null Pointer Error

    Good Morning Everyone! I'm using NetBeans to create this project and I have the gui registering text boxes, but it won't retrieve what the user entered, so the calculate button returns a null exception error. Help!

    Java Code:
    package payroll;
    
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class Interface extends JFrame{
    
        private JPanel employeeName; //Employee name panel
        private JPanel salary;       //Employee salary panel
        private JPanel hours;        //Employee hours panel
        private JComboBox departments;     //Departments combo box
        private JLabel empNameLabel;      //employee name label
        private JLabel empDeptLabel;      //employee department label
        private JLabel salaryLabel;   //employee Salary label
        private JLabel empHourLabel;
        private JTextField empName;
        private JTextField selectedDept; //The selected department
        private JTextField salaryValue; //salary input
        private JTextField hourValue;
        private JButton calcButton;
        private double hour;
        private double salAmt;
        
        
        /*
        Constructor for the interface
        */
        
        public Interface() {
            
            super("Payroll Calculator");
            setSize(700, 150);
            
            
            //default close operation
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            //Layout manager
            setLayout(new BorderLayout());
            
                  
            //build the panels
            buildEmpName();
            buildEmpSal();
            buildEmpHours();
            
            //add panels to content pane
            add(employeeName, BorderLayout.NORTH);
            add(salary, BorderLayout.CENTER);
            add(hours, BorderLayout.SOUTH);
            
            
            
            //pack and display
            pack();
            setVisible(true);
            
            
        }
    
        private void buildEmpName() {
            
            //create panel
            employeeName = new JPanel();
    
            //label for employee text
            empNameLabel = new JLabel("Employee Name:");
            
            //Text Field to store employee new and last name
            JTextField empName = new JTextField("Employee Last, First Name");
            empName.setEditable(true);
            
            //label for employee text
            empDeptLabel = new JLabel("Employee Department:");
            
            //Combo box for department
            String[] departmentList = {"Accounting", "HR", "IT", "Legal", "Marketing", "Service Center"};
                    JComboBox departments = new JComboBox(departmentList);
            
                    
            //add components to panel
            employeeName.add(empNameLabel, BorderLayout.WEST);
            employeeName.add(empName, BorderLayout.EAST);
            employeeName.add(empDeptLabel, BorderLayout.WEST);
            employeeName.add(departments, BorderLayout.EAST);
            
            setVisible(true);
            
        }
    
        private void buildEmpSal() {
            
            //create panel
            salary = new JPanel();
           
            //label for employee text
            salaryLabel = new JLabel("Hourly Rate:");
            
            //Text Field to store employee new and last name
            JTextField salaryValue = new JTextField();
            salaryValue.setEditable(true);
            salaryValue.setColumns(15);
            
            
            //add components to panel
            salary.add(salaryLabel, BorderLayout.WEST);
            salary.add(salaryValue, BorderLayout.EAST);
            
            setVisible(true);
        }
    
        private void buildEmpHours() {
            
            //create panel
            hours = new JPanel();
    
            
            //label for employee hours
            empHourLabel = new JLabel("Hours Worked:");
            
            //Text Field to store employee new and last name
            JTextField hourValue = new JTextField();
            hourValue.setEditable(true);
            hourValue.setColumns(15);
            
            //add calculate button
            calcButton = new JButton ("Calculate");
            
            //add action listener to button
            calcButton.addActionListener(new CalcButtonListener());
            
            
            //add components to panel
            hours.add(empHourLabel, BorderLayout.WEST);
            hours.add(hourValue, BorderLayout.EAST);
            hours.add(calcButton, BorderLayout.PAGE_END);
        }
        
        
        private class CalcButtonListener implements ActionListener {
    
            public void actionPerformed(ActionEvent e) {
                
                double salary;
                double overtime;
                double totalPay;
                String sal1;
                String hr1;
                
    
            
            
                //Retrieve Salary amt
                sal1 = salaryValue.getText();
                salAmt = Double.parseDouble(sal1);
                
                //Retrieve Hours 
                hr1 = hourValue.getText();
                hour = Double.parseDouble(hr1);
                
                //debugging print message    
                System.out.println("Reading: " + sal1 + "Reading2: " + hr1 );            
                
                //calculate salary
                salary = (salAmt * 40);
                overtime = (hour - 40) * (salAmt * 1.5);
                totalPay = salary + overtime;
                
                //Create decimal format object
                DecimalFormat dollar = new DecimalFormat("0.00");
                
                //Display the total 
                JOptionPane.showMessageDialog(null, "Salary: $" + dollar.format(salary) + "\n" +
                        "Overtime: $" + dollar.format(overtime) + "\n" +
                        "Total Pay: $" + dollar.format(totalPay));
                
            }
        }
    
        public static void main(String[] args) {
            // TODO code application logic here
             new Interface();
            
    }
        
        
        
        }
    Last edited by rebelguy13; 03-31-2014 at 05:19 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Null Pointer Error

    Please copy the full text of the error message and paste it here. It has important info about the error.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    rebelguy13 is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Null Pointer Error

    Sorry! I made the edit. Thanks!

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Null Pointer Error

    You forgot the error message.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Null Pointer Error

    You are declaring your JTextField salaryValue inside of a method. So it's scope is limited to that method. Make it an instance field.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    rebelguy13 is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Null Pointer Error

    That's a good idea! Thanks Jim! As you can probably tell, I'm a new Java user so it was driving me nuts, but that's a great idea. Let me try that and if it doesn't work I'll post the error message.

    Thank you!!

    Quote Originally Posted by jim829 View Post
    You are declaring your JTextField salaryValue inside of a method. So it's scope is limited to that method. Make it an instance field.

    Regards,
    Jim

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Null Pointer Error

    I also suggest you change the name of your class. Interface can be somewhat confusing since interface (lowercase i) is a Java keyword.

    Regards,
    Jim
    gimbal2 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Null Pointer Error

    Next to the name conflict, it also doesn't describe what the class does. It would have to be a name that mentions at least 'PayrollCalculator', how you finish that name is up to personal taste. If it were my code it would become PayrollCalculatorFrame.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    rebelguy13 is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Null Pointer Error

    I tried creating the JTextFields as an instance instead and it didn't work. Below is the error message (in case someone wants to see it) with an updated code.

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at payrollCalculator.payrollCalculatorFrame$CalcButtonListener.actionPerformed(payrollCalculatorFrame.java:181)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6505)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    	at java.awt.Component.processEvent(Component.java:6270)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4861)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2719)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:694)
    	at java.awt.EventQueue$3.run(EventQueue.java:692)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:708)
    	at java.awt.EventQueue$4.run(EventQueue.java:706)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
    
    
    package payrollCalculator;
    
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    /**
     *
     * @author Antonio School
     */
    public class payrollCalculatorFrame extends JFrame{
    
        private JPanel employeeName; //Employee name panel
        private JPanel salary;       //Employee salary panel
        private JPanel hours;        //Employee hours panel
        private JComboBox departments;     //Departments combo box
        private JLabel empNameLabel;      //employee name label
        private JLabel empDeptLabel;      //employee department label
        private JLabel salaryLabel;   //employee Salary label
        private JLabel empHourLabel;
        private JTextField empName;
        private JTextField selectedDept; //The selected department
        public JTextField salaryValue;
        public JTextField hourValue;
        private JButton calcButton;
        private double hour;
        private double salAmt;
        
        
        /*
        Constructor for the GUI
        */
        
        public payrollCalculatorFrame() {
            
            super("Payroll Calculator");
            setSize(700, 150);
            
            
            //default close operation
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            //Layout manager
            setLayout(new BorderLayout());
            
                  
            //build the panels
            //create panel
            employeeName = new JPanel();
    
            //label for employee text
            empNameLabel = new JLabel("Employee Name:");
            
            //Text Field to store employee new and last name
            JTextField empName = new JTextField("Employee Last, First Name");
            empName.setEditable(true);
            
            //label for employee text
            empDeptLabel = new JLabel("Employee Department:");
            
            //Combo box for department
            String[] departmentList = {"Accounting", "HR", "IT", "Legal", "Marketing", "Service Center"};
                    JComboBox departments = new JComboBox(departmentList);
            
                    
            //add components to panel
            employeeName.add(empNameLabel, BorderLayout.WEST);
            employeeName.add(empName, BorderLayout.EAST);
            employeeName.add(empDeptLabel, BorderLayout.WEST);
            employeeName.add(departments, BorderLayout.EAST);
            
            
            //create panel
            salary = new JPanel();
           
            //label for employee text
            salaryLabel = new JLabel("Hourly Rate:");
            
            //Text Field to store employee new and last name
            JTextField salaryValue = new JTextField();
            salaryValue.setEditable(true);
            salaryValue.setColumns(15);
            
            
            //add components to panel
            salary.add(salaryLabel, BorderLayout.WEST);
            salary.add(salaryValue, BorderLayout.EAST);
            
            //create panel
            hours = new JPanel();
    
            
            //label for employee hours
            empHourLabel = new JLabel("Hours Worked:");
            
            //Text Field to store employee new and last name
            JTextField hourValue = new JTextField();
            hourValue.setEditable(true);
            hourValue.setColumns(15);
            
            //add calculate button
            calcButton = new JButton ("Calculate");
            
            //add action listener to button
            calcButton.addActionListener(new CalcButtonListener());
            
            //add exit button
            JButton button = new JButton ("Close");
            
                button.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        System.exit(0);
                    }
                });
            
            //add components to panel
            hours.add(empHourLabel, BorderLayout.WEST);
            hours.add(hourValue, BorderLayout.EAST);
            hours.add(calcButton, BorderLayout.PAGE_END);
            hours.add(button, BorderLayout.AFTER_LINE_ENDS);
            
            //add panels to content pane
            add(employeeName, BorderLayout.NORTH);
            add(salary, BorderLayout.CENTER);
            add(hours, BorderLayout.SOUTH);
            
            
            
            //pack and display
            pack();
            setVisible(true);
            
            
        }
    
          
        private class CalcButtonListener implements ActionListener {
    
            public void actionPerformed(ActionEvent e) {
                
                double salary;
                double overtime;
                double totalPay;
                String sal1;
                String hr1;
                     
                //Retrieve Salary amt
                sal1 = salaryValue.getText();
                salAmt = Double.parseDouble(sal1);
                
                //Retrieve Hours 
                hr1 = hourValue.getText();
                hour = Double.parseDouble(hr1);
                
                //debugging print message    
                System.out.println("Reading: " + sal1 + "Reading2: " + hr1 );            
                
                //calculate salary
                salary = (salAmt * 40);
                overtime = (hour - 40) * (salAmt * 1.5);
                totalPay = salary + overtime;
                
                //Create decimal format object
                DecimalFormat dollar = new DecimalFormat("0.00");
                
                //Display the total 
                JOptionPane.showMessageDialog(null, "Salary: $" + dollar.format(salary) + "\n" +
                        "Overtime: $" + dollar.format(overtime) + "\n" +
                        "Total Pay: $" + dollar.format(totalPay));
                
            }
        }
    
        public static void main(String[] args) {
            // TODO code application logic here
             new payrollCalculatorFrame();
            
    }
        
        
        
        }
    Last edited by rebelguy13; 04-01-2014 at 04:30 AM.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Null Pointer Error

    Check line #127 in your revised posted code. You redeclared salaryValue so its hiding the instance field.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    rebelguy13 is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Null Pointer Error

    Do I even need the section at 127?

    This:

    JTextField salaryValue = new JTextField();

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Null Pointer Error

    Yes! At least somewhere you need to create a JTextField instance. But you delcared the JTextField as an instance variable. So now, just
    do the following:
    Java Code:
    salaryValue = new JTextField();
    Just like you did with other instance variables.

    Regards,
    Jim
    rebelguy13 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    rebelguy13 is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Null Pointer Error

    THANK YOU THANK YOU THANK YOU!!!!! That worked!!

Similar Threads

  1. Null Pointer error, need help
    By barr94 in forum New To Java
    Replies: 6
    Last Post: 06-04-2013, 07:31 AM
  2. Unusual null pointer error
    By PRW56 in forum New To Java
    Replies: 8
    Last Post: 03-22-2012, 07:58 PM
  3. null pointer error help!!
    By fakepics500 in forum New To Java
    Replies: 1
    Last Post: 07-16-2011, 03:57 PM
  4. Null Pointer Exception error
    By tfitz666 in forum New To Java
    Replies: 3
    Last Post: 03-28-2010, 08:20 PM
  5. Null pointer exception error
    By brownie_jedi in forum New To Java
    Replies: 3
    Last Post: 03-15-2008, 07:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •