New Posts  All Forums:Forum Nav:

JAVA GUI code error

post #1 of 3
Thread Starter 
So I tried to create a simple GUI class in Java with some checkboxes. Eclipse doesn't show any error, however when I run it it shows this error. I have tried making the label to set some default text in there so it's no null, and all the text area are already not null. But for some reason it still has this error. If anyone knows what's wrong please help. Thanks.
Quote:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at edu.unlv.assignment4.JoesAutomotive$BtnCalculateActionListener.actionPerformed(JoesAutomotive.java:163)
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:3321)
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:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
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:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
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)

Here's the code.
Code:
package assignment4;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class JoesAutomotive extends JFrame {

        private JPanel contentPane;
        private JCheckBox chckbxOilChange;
        private JCheckBox chckbxLubeJob;
        private JCheckBox chckbxRadiatorFlush;
        private JCheckBox chckbxTransmissionFlush;
        private JCheckBox chckbxInspections;
        private JCheckBox chckbxMufflerReplacement;
        private JCheckBox chckbxTireRotation;
        private JRadioButton rdbtnRegularPrice;
        private JRadioButton rdbtnOff10;
        private JRadioButton rdbtnOff20;
        private JTextField txtParts;
        private JTextField txtLabor;
        private JLabel lblTotal;

        /**
         * Launch the application.
         */
        public static void main(String[] args) {
                EventQueue.invokeLater(new Runnable() {
                        public void run() {
                                try {
                                        JoesAutomotive frame = new JoesAutomotive();
                                        frame.setVisible(true);
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                        }
                });
        }

        /**
         * Create the frame.
         */
        public JoesAutomotive() {
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setBounds(100, 100, 550, 300);
                contentPane = new JPanel();
                contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                setContentPane(contentPane);
                contentPane.setLayout(null);
                
                JLabel lblRoutineMaintenanceServices = new JLabel("Routine Maintenance Services");
                lblRoutineMaintenanceServices.setBounds(10, 11, 189, 14);
                contentPane.add(lblRoutineMaintenanceServices);
                
                JCheckBox chckbxOilChange = new JCheckBox("Oil Change: $26.00");
                chckbxOilChange.setBounds(6, 29, 193, 23);
                contentPane.add(chckbxOilChange);
                
                JCheckBox chckbxLubeJob = new JCheckBox("Lube job: $18.00");
                chckbxLubeJob.setBounds(6, 55, 193, 23);
                contentPane.add(chckbxLubeJob);
                
                JCheckBox chckbxRadiatorFlush = new JCheckBox("Radiator flush: $30.00");
                chckbxRadiatorFlush.setBounds(6, 81, 193, 23);
                contentPane.add(chckbxRadiatorFlush);
                
                JCheckBox chckbxTransmissionFlush = new JCheckBox("Transmission flush: $80.00");
                chckbxTransmissionFlush.setBounds(6, 107, 193, 23);
                contentPane.add(chckbxTransmissionFlush);
                
                JCheckBox chckbxInspections = new JCheckBox("Inspections: $15.00");
                chckbxInspections.setBounds(6, 133, 193, 23);
                contentPane.add(chckbxInspections);
                
                JCheckBox chckbxMufflerReplacement = new JCheckBox("Muffler replacement: $100.00");
                chckbxMufflerReplacement.setBounds(6, 159, 193, 23);
                contentPane.add(chckbxMufflerReplacement);
                
                JCheckBox chckbxTireRotation = new JCheckBox("Tire rotation: $20.00");
                chckbxTireRotation.setBounds(6, 185, 193, 23);
                contentPane.add(chckbxTireRotation);
                
                JLabel lblNonroutineServices = new JLabel("Nonroutine Services");
                lblNonroutineServices.setBounds(209, 11, 189, 14);
                contentPane.add(lblNonroutineServices);
                
                JLabel lblParts = new JLabel("Parts:");
                lblParts.setBounds(205, 33, 46, 14);
                contentPane.add(lblParts);
                
                JLabel lblLabor = new JLabel("Labor:");
                lblLabor.setBounds(205, 55, 46, 14);
                contentPane.add(lblLabor);
                
                JTextArea txtParts = new JTextArea();
                txtParts.setText("0");
                txtParts.setBounds(257, 28, 63, 22);
                contentPane.add(txtParts);
                
                JTextArea txtLabor = new JTextArea();
                txtLabor.setText("0");
                txtLabor.setBounds(257, 54, 63, 22);
                contentPane.add(txtLabor);
                
                JLabel lblHoursPer = new JLabel("hours ($20 per hour)");
                lblHoursPer.setBounds(330, 59, 151, 14);
                contentPane.add(lblHoursPer);
                
                JRadioButton rdbtnRegularPrice = new JRadioButton("Regular Price");
                rdbtnRegularPrice.setBounds(201, 107, 109, 23);
                contentPane.add(rdbtnRegularPrice);
                
                JRadioButton rdbtnOff10 = new JRadioButton("10% off");
                rdbtnOff10.setBounds(201, 133, 109, 23);
                contentPane.add(rdbtnOff10);
                
                JRadioButton rdbtnOff20 = new JRadioButton("20% off");
                rdbtnOff20.setBounds(201, 159, 109, 23);
                contentPane.add(rdbtnOff20);
                
                JButton btnCalculate = new JButton("Calculate");
                btnCalculate.addActionListener(new BtnCalculateActionListener());
                btnCalculate.setBounds(330, 159, 89, 23);
                contentPane.add(btnCalculate);
                
                ButtonGroup btnGroup = new ButtonGroup();
                btnGroup.add(rdbtnRegularPrice);
                btnGroup.add(rdbtnOff10);
                btnGroup.add(rdbtnOff20);
                
                JLabel lblTotal = new JLabel("not null");
                lblTotal.setBounds(10, 215, 260, 14);
                contentPane.add(lblTotal);
                
        }
        private class BtnCalculateActionListener implements ActionListener {
                public void actionPerformed(ActionEvent e) {
                        double oilChange = 26.0;
                        double lubeJob = 18.0;
                        double radiatorFlush = 30.0;
                        double transmissionFlush = 80.0;
                        double inspection = 15.0;
                        double mufflerReplacement = 100.0;
                        double tireRotation = 20.0;
                        double discount = 0.0;
                        int parts = 0;
                        int laborHour = 0;
                        
                        //If selected set to price, if not selected set to 0 for each type of services
                        if(chckbxOilChange.isSelected() == true) {
                                oilChange = 26.0;
                        }
                        else {
                                oilChange = 0.0;
                        }
                        
                        if(chckbxLubeJob.isSelected() == true) {
                                lubeJob = 18.0;
                        }
                        else {
                                lubeJob = 0.0;
                        }
                        
                        if(chckbxRadiatorFlush.isSelected() == true) {
                                radiatorFlush = 30.0;
                        }
                        else {
                                radiatorFlush = 0.0;
                        }
                        
                        if(chckbxTransmissionFlush.isSelected() == true) {
                                transmissionFlush = 80.0;
                        }
                        else {
                                transmissionFlush = 0.0;
                        }
                        
                        if(chckbxInspections.isSelected() == true) {
                                inspection = 15.0;
                        }
                        else {
                                inspection = 0.0;
                        }
                        
                        if(chckbxMufflerReplacement.isSelected() == true) {
                                mufflerReplacement = 100.0;
                        }
                        else {
                                mufflerReplacement = 0.0;
                        }
                        
                        if(chckbxTireRotation.isSelected() == true) {
                                tireRotation = 20.0;
                        }
                        else {
                                tireRotation = 0.0;
                        }
                        
                        //If discounts are selected, set discount percentage
                        if(rdbtnRegularPrice.isSelected() == true) {
                                discount = 1.0;
                        }
                        else if(rdbtnOff10.isSelected() == true) {
                                discount = 0.9;
                        }
                        else if(rdbtnOff20.isSelected() == true) {
                                discount = 0.8;
                        }
                        
                        //Get parts price
                        parts = Integer.parseInt(txtParts.getText());
                        
                        //Get labor hour
                        laborHour = Integer.parseInt(txtLabor.getText());
                        
                        //Calculate the total
                        double total = (oilChange+lubeJob+radiatorFlush+transmissionFlush+inspection+mufflerReplacement+tireRotation+parts+(laborHour*20))*discount;
                        
                        lblTotal.setText("The total is $"+total);
                        
                }
        }
}
FairladyZ
(14 items)
 
Twin Turbo S2000
(13 items)
 
Skyline
(13 items)
 
CPUMotherboardGraphicsRAM
Core i7 2600k Asus P8Z68-M Pro MSI GTX970 Gaming 4G Corsair Ballistix Sport DDR3-1600 16GB 
Hard DriveOptical DriveCoolingOS
Western Digital Black Hybrid 1TB WD10S12X Asus BW-12B1ST Thermaltake WATER2.0 Pro Windows 7 Ultimate x64 SP1 
MonitorMonitorKeyboardPower
MAG Innovision 22 inch LCD Samsung SyncMaster 2220wm Microsoft Sidewinder X4 Antec Earthwatts 750w 
CaseMouse
Cooler Master Elite 430 Microsoft Trackball Optical 1.0 
CPUMotherboardGraphicsRAM
Q6600 (@3.4ghz, lapped) ASRock P45TurboTwins2000 HD4850 (@800gpu/1087mem) Patriot PC2-6400 2x2GB 
Hard DriveOptical DriveOSMonitor
WD6400AALS/WD6400AAKS Emprex DVD±RW Win7 Ultimate x86/XP Pro(Dual Boot) MAG 22" WS LCD 
KeyboardPowerCaseMouse
HP 5302H Multimedia Keyboard Antec Basiq 500w + Ultra Xconnect 500w Cooler Master Mystique 632S Microsoft Trackball Optical 1.0 
CPUMotherboardGraphicsRAM
Athlon X2 4600+ (@2.75ghz, lapped) ECS KN1 Lite Sapphire X1950GT 512mb (@681/661) 2x1GB Mushikin DDR400 
Hard DriveOptical DriveOSMonitor
Hitachi 80GB Maddog DVD-RW Windows XP Pro SP2 Mag 22" WS LCD LT2219WDB 
KeyboardPowerCaseMouse
Hewlett Packard 5302H Apevia 500w Generic CompUSA Microsoft Trackball Optical 1.0 
Mouse Pad
None 
  hide details  
Reply
FairladyZ
(14 items)
 
Twin Turbo S2000
(13 items)
 
Skyline
(13 items)
 
CPUMotherboardGraphicsRAM
Core i7 2600k Asus P8Z68-M Pro MSI GTX970 Gaming 4G Corsair Ballistix Sport DDR3-1600 16GB 
Hard DriveOptical DriveCoolingOS
Western Digital Black Hybrid 1TB WD10S12X Asus BW-12B1ST Thermaltake WATER2.0 Pro Windows 7 Ultimate x64 SP1 
MonitorMonitorKeyboardPower
MAG Innovision 22 inch LCD Samsung SyncMaster 2220wm Microsoft Sidewinder X4 Antec Earthwatts 750w 
CaseMouse
Cooler Master Elite 430 Microsoft Trackball Optical 1.0 
CPUMotherboardGraphicsRAM
Q6600 (@3.4ghz, lapped) ASRock P45TurboTwins2000 HD4850 (@800gpu/1087mem) Patriot PC2-6400 2x2GB 
Hard DriveOptical DriveOSMonitor
WD6400AALS/WD6400AAKS Emprex DVD±RW Win7 Ultimate x86/XP Pro(Dual Boot) MAG 22" WS LCD 
KeyboardPowerCaseMouse
HP 5302H Multimedia Keyboard Antec Basiq 500w + Ultra Xconnect 500w Cooler Master Mystique 632S Microsoft Trackball Optical 1.0 
CPUMotherboardGraphicsRAM
Athlon X2 4600+ (@2.75ghz, lapped) ECS KN1 Lite Sapphire X1950GT 512mb (@681/661) 2x1GB Mushikin DDR400 
Hard DriveOptical DriveOSMonitor
Hitachi 80GB Maddog DVD-RW Windows XP Pro SP2 Mag 22" WS LCD LT2219WDB 
KeyboardPowerCaseMouse
Hewlett Packard 5302H Apevia 500w Generic CompUSA Microsoft Trackball Optical 1.0 
Mouse Pad
None 
  hide details  
Reply
post #2 of 3
You're declaring your variables twice. Once as a global variable, and once inside the constructor.
Like this:

Global:
private JCheckBox chckbxOilChange;
...
then inside constructor it is declared again:
JCheckBox chckbxOilChange = new JCheckBox("Oil Change: $26.00");

So you're setting the values of the local variables in the constructor, and the global variables remain null. Which is why you get an error when the inner class tries to access them.

You just need to change each of the lines in your constructor to look like this:
chckbxOilChange = new JCheckBox("Oil Change: $26.00");
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
post #3 of 3
Further to my last post
Quote:
Originally Posted by awdrifter View Post

I have tried making the label to set some default text in there so it's no null, and all the text area are already not null. But for some reason it still has this error.

You're looking in the wrong place for the null. Take a look at the error trace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at edu.unlv.assignment4.JoesAutomotive$BtnCalculateActionListener.actionPerformed(JoesAutomotive.java:163)

That is the filename and line number where the NullPointerException is happening. Line 163 is this one:
if(chckbxOilChange.isSelected() == true) {

There's only one variable being accessed on that line, so you know that variable is null when that line runs. Knowing that makes finding the problem easy.
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming