5 Replies - 1343 Views - Last Post: 25 April 2010 - 09:33 AM Rate Topic: -----

#1 Tevin90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-March 10

Error In Gui application

Posted 25 April 2010 - 07:58 AM

The program compiles, but this is the error message I receive whenever I try to run it

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1045)
at java.awt.Container.add(Container.java:365)
at ServicePanel.<init>(ServicePanel.java:51)
at ServicePanel.main(ServicePanel.java:26)







import javax.swing.*;
import java.awt.*;

public class ServicePanel extends JPanel
{
 public final double OIL_CHANGE = 26.00;
 public final double LUBE_JOB = 18.00;
 public final double RADIATOR_FLUSH = 30.00;
 public final double TRANSMISSION_FLUSH = 80.00;
 public final double INSPECTION = 15.00;
 public final double MUFFLER_REPLACEMENT = 100.00;
 public final double TIRE_ROTATION = 20.00;
 public final double Labor = 20.00;
 
 private JCheckBox oilChange;
 private JCheckBox lubeJob;
 private JCheckBox radiatorFlush;
 private JCheckBox transmissionFlush;
 private JCheckBox inspection;
 private JCheckBox mufflerReplacement;
 private JCheckBox tireRotation;
 private JCheckBox calcButton;
 
 public static void main(String[] args) 
 {
  new ServicePanel(); 
 }

 
 public ServicePanel()
 { 
  setLayout(new GridLayout(8, 1)); 
 
  oilChange = new JCheckBox("Oil Change"); 
  lubeJob = new JCheckBox("Lube Job"); 
  radiatorFlush = new JCheckBox("Radiator Flush"); 
  transmissionFlush = new JCheckBox("Transmission Flush"); 
  inspection = new JCheckBox("Inspection");
  mufflerReplacement = new JCheckBox("Muffler Replacement"); 
  tireRotation = new JCheckBox("Tire Rotation");
  
  setBorder(BorderFactory.createTitledBorder("Service"));
  
  add(oilChange);
  add(lubeJob);
  add(radiatorFlush);
  add(transmissionFlush);
  add(inspection);
  add(mufflerReplacement);
  add(tireRotation);
  add(calcButton);
 }
 
 public double getServiceCost()
 {
  double serviceCost = 0.0;
  
  if (oilChange.isSelected())
     serviceCost += OIL_CHANGE;
  if (lubeJob.isSelected())
     serviceCost += LUBE_JOB;
  if (radiatorFlush.isSelected())
     serviceCost += RADIATOR_FLUSH;
  if (transmissionFlush.isSelected())
     serviceCost += TRANSMISSION_FLUSH;
  if (inspection.isSelected())
     serviceCost += INSPECTION;
  if (mufflerReplacement.isSelected())
     serviceCost += MUFFLER_REPLACEMENT;
  if (tireRotation.isSelected())
     serviceCost +=TIRE_ROTATION;
	  
  return serviceCost;
  }
 }


Is This A Good Question/Topic? 0
  • +

Replies To: Error In Gui application

#2 Ogadai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-April 10

Re: Error In Gui application

Posted 25 April 2010 - 08:46 AM

You have declared calcButton which looks like it was meant to be a JButton
private JCheckBox calcButton;


but it hasn't been initialised, so when you try to add it
add(calcButton);


it is still set to null.
Was This Post Helpful? 0
  • +
  • -

#3 Tevin90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-March 10

Re: Error In Gui application

Posted 25 April 2010 - 08:59 AM

Are you saying that I should change the JCheckBox to a JButton instead and should I add an action listener also?
Was This Post Helpful? 0
  • +
  • -

#4 Ogadai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-April 10

Re: Error In Gui application

Posted 25 April 2010 - 09:05 AM

I'm just saying that with a name like calcButton, you probably wanted to declare it as a JButton rather than a JCheckBox. You have initialised all your JCheckBoxes
oilChange = new JCheckBox("Oil Change"); 
lubeJob = new JCheckBox("Lube Job"); 
radiatorFlush = new JCheckBox("Radiator Flush"); 
transmissionFlush = new JCheckBox("Transmission Flush"); 
inspection = new JCheckBox("Inspection");
mufflerReplacement = new JCheckBox("Muffler Replacement"); 
tireRotation = new JCheckBox("Tire Rotation");


but haven't done anything with calcButton, whatever you want it to be, so when you try to add it to your panel, you get a NullPointerException, because it's still null.
Was This Post Helpful? 0
  • +
  • -

#5 Tevin90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-March 10

Re: Error In Gui application

Posted 25 April 2010 - 09:15 AM

I've made the changes and it still compiles properly, but now it still won't run.
Was This Post Helpful? 0
  • +
  • -

#6 Ogadai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-April 10

Re: Error In Gui application

Posted 25 April 2010 - 09:33 AM

There are a couple of reasons why your GUI won't display. First is that your class extends JPanel, but you haven't added it to a JFrame, which is a top-level window with a title and borders. You can't just display a JPanel by itself. It might be easier for a simple program to just work with a JFrame rather than a JPanel, as you can always add a JPanel later on to make it pretty. See this tutorial for a simple Notepad app as a guide http://www.dreaminco...ad-application/

The other issue you will run into once you fix that is that the GUI will be invisible by default so you will need to add
setVisible(true);


at some point.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1