-2

Alright, since it didn't work out last time. I'm going to post my full code here and i hope i can get some replies as to why it's not working. I'm not getting any compiling errors, the applet runs and then nothing shows up and at the bottom it says "applet not initialized." I'm using blueJ. I apologize for the length of this post, I could not figure out how to make this same error with a shorter code.

I have a JApplet program with multiple classes. RegPanel,WorkshopPanel, ConferenceGUI, ConferenceHandler and ConferenceClient. Basically RegPanel and WorkShop panel are added to the ConferenceGUI, which also creates and adds a couple small panels. The ConferenceClient class is just used to initaite the class to run the applet. The ConferenceHandler is used to handle the action events for the JButtons, JTextArea, JCheckBox, etc... Normally this whole program works fine. Except when i add a listener for the JCheckBox, it stops working. The problem area is in the ConferenceGUI class, it is commented with stars to be clear what's causing the problem.

I've been stuck on this error for about a day now and the frustration i'm feeling is overwhelming. So, to get to the point, here's the complete code: (please, i don't need tips on any other part of the code, I just need help with that error). You may want to skip over the code and just read the ConferenceGUI class where the error is located. If you could also explain to me why this isn't working, it would be very helpful to me. Thank you in advance!

RegPanel Class:

import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class RegPanel extends JPanel
{
    protected JTextField regNameTextBox;

    protected JCheckBox keynoteCheckBox;

    protected final String[] REGTYPES = {"Please select a   type","Business","Student","Complimentary"};
    protected JPanel registrationPanel, keynotePanel;
    protected final double BUSINESSFEE = 895,STUDENTFEE = 495,COMPLIMENTARYFEE = 0;
    protected JComboBox regTypeComboBox;
    public RegPanel()
    {
        //Set the layout for the RegPanel to be 2 rows and 1 column.
        setLayout(new GridLayout(2, 1));

        //initiate the registration panel and add a border
        registrationPanel = new JPanel();
        registrationPanel.setLayout(new FlowLayout());
        registrationPanel.setBorder(BorderFactory.createTitledBorder("Registrant's Name & Type"));


        //initiate the comboBox and add the registration types
        regTypeComboBox = new JComboBox(REGTYPES);

        //Initiate the textfield with a size of 20
        regNameTextBox = new JTextField(20);

        //Add the registration name textbox and type combobox to the registration panel
        registrationPanel.add(regNameTextBox);
        registrationPanel.add(regTypeComboBox);

        //initiate the second panel for the checkbox
        keynotePanel = new JPanel();
        keynotePanel.setLayout(new FlowLayout());

        //initiate the checkbox and add it to the keynote panel
        JCheckBox keynoteCheckBox = new JCheckBox("Dinner and Keynote Speach");
        keynotePanel.add(keynoteCheckBox);

        //Add the two panels to the main panel
        add(registrationPanel);
        add(keynotePanel);

    }    
    public double getRegistrationCost()
    {
        double regFee = 0;
        String comboBoxAnswer = (String)regTypeComboBox.getSelectedItem();

        switch (comboBoxAnswer)
            {
                case "Business": regFee = BUSINESSFEE;
                   break;
                case "Student": regFee = STUDENTFEE;
                    break;

            }
        return regFee;    
    }        
    public double getKeynoteCost()
    {
        double keynoteCost = 0;
        if(keynoteCheckBox.isSelected())
        {
            keynoteCost = 30;
        }    
        return keynoteCost;
    }
    public String getRegType()
    {
        String regType = (String)regTypeComboBox.getSelectedItem();
        return regType;
    }    
}

WorkshopPanel Class:

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

public class WorkshopPanel extends JPanel
{
    protected final double ITFEE = 295, DREAMFEE = 295, JAVAFEE = 395, ETHICSFEE = 395;
    protected final String[] WORKSHOPS = {"IT Trends in Manitoba","Creating a Dream Career","Advanced Java Programming","Ethics: The Challenge Continues"};
    protected JList workshopList;
    public WorkshopPanel()
    {
        setLayout(new FlowLayout());

        workshopList = new JList(WORKSHOPS);
        workshopList.setSelectionMode(2);


        BorderFactory.createTitledBorder("Workshops");

        add(workshopList);
    }    

    public double getWorkshopCost()
    {
        Object[] workshops = workshopList.getSelectedValues();
        double cost = 0;
        String workshopString;
        for (int i = 0; i < workshops.length; i++)
        {
          workshopString = (String)workshops[i];
          switch(workshopString)
          {
              case "IT Trends in Manitoba":
                cost += ITFEE;
                break;
              case "Creating a Dream Career":  
                cost += DREAMFEE;
                break;
              case "Advanced Java Programming":
                cost += JAVAFEE;
                break;
              case "Ethics: The Challenge Continues":
                cost += ETHICSFEE;
                break;

        }    
    }    
        return cost;

    }
    public Object[] getWorkshopList()
    {
        Object[] workshopListArray = workshopList.getSelectedValues();
        return workshopListArray;
    }    
}    

ConferenceGUI class (THIS CONTAINS THE ERROR):

import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class ConferenceGUI extends JPanel
{
   protected JPanel titlePanel, buttonPanel;
   protected RegPanel regPanel;
   protected WorkshopPanel workshopPanel;
   protected JLabel titleLabel;
   protected JButton calculateButton, clearButton;
   protected JTextArea resultArea;
   protected JScrollPane textScroll;


   public ConferenceGUI()
   {



       setLayout(new BorderLayout());

       titlePanel = new JPanel();

       titleLabel = new JLabel("Select Registration Options",JLabel.CENTER);

       Font titleFont = new Font("SansSerif", Font.BOLD, 18);

       titleLabel.setFont(titleFont);

       titlePanel.add(titleLabel);

       add(titlePanel, BorderLayout.NORTH);

       regPanel = new RegPanel();
       add(regPanel, BorderLayout.WEST);


       workshopPanel = new WorkshopPanel();
       add(workshopPanel, BorderLayout.EAST);

       buildButtonPanel();
       add(buttonPanel, BorderLayout.SOUTH);


       ConferenceHandler handler = new ConferenceHandler(this);

       regPanel.regTypeComboBox.addItemListener(handler);
       regPanel.regNameTextBox.addFocusListener(handler);
       //****************************************************************
       //The line below is what causes the error. Without it the code
       //Works, with it it doesn't and i get the aforementioned error.  


       //regPanel.keynoteCheckBox.addItemListener(handler);




   }    
   private void buildButtonPanel()
   {
       buttonPanel = new JPanel();
       buttonPanel.setLayout(new FlowLayout());

       calculateButton = new JButton("Calculate Charges");
       buttonPanel.add(calculateButton);

       clearButton = new JButton("Clear");
       buttonPanel.add(clearButton);


       resultArea = new JTextArea(5,30);
       textScroll = new JScrollPane(resultArea);

       buttonPanel.add(textScroll);

       ConferenceHandler handler = new ConferenceHandler(this);
       calculateButton.addActionListener(handler);
       clearButton.addActionListener(handler);

   }    
}

ConferenceHandler class( this class is unfinished until i get that error straightened out) :

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

public class ConferenceHandler implements ActionListener, ItemListener, FocusListener
{
    protected ConferenceGUI gui;
    public ConferenceHandler(ConferenceGUI gui)
    {
        this.gui = gui;
    }    
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == gui.calculateButton)
        {
           String regType = gui.regPanel.getRegType();
           Object[] workshopList = gui.workshopPanel.getWorkshopList();

           String workshopString;




           if (regType == "Please select a type")
           {
               JOptionPane.showMessageDialog(null,"Please select a registration type","Type Error",JOptionPane.ERROR_MESSAGE );
           }    
           else
           {
               if(gui.regPanel.keynoteCheckBox.isSelected())

               {  
                   gui.resultArea.append("Keynote address will be attended/n");
               } 
               else
               {
                   gui.resultArea.append("Keynot address will not be attended/n");
               }    

           }
        }
        if (e.getSource() == gui.clearButton)
        {
            gui.resultArea.append("CLEAR");
        }    
    }
    private double getTotalCharges()
    {
        double charges = 0;



        return charges;
    }    
    public void itemStateChanged(ItemEvent e)
    {
    }
    public void focusLost(FocusEvent e)
    {
    }
    public void focusGained(FocusEvent e)
    {
    }    

}

ConferenceClient Class:

import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class ConferenceClient extends JApplet
{
  private final int WINDOW_HEIGHT = 700, WINDOW_WIDTH = 250; 
  private ConferenceGUI gui;

  private Container c;

  public ConferenceClient()
  {
      gui = new ConferenceGUI();

      c = getContentPane();

      c.setLayout(new BorderLayout());

      c.add(gui, BorderLayout.CENTER);

      setSize(WINDOW_HEIGHT, WINDOW_WIDTH);
  }    
}
Stan Harris
  • 15
  • 1
  • 4
  • 1
    This: `regType == "Please select a type"` is not how to compare `String`s in Java, you want to use something more like `"Please select a type".equals(regType)` – MadProgrammer Oct 22 '15 at 05:32

1 Answers1

1

You're shadowing your keynoteCheckBox variable. First you create a instance field in RegPanel, but in the constructor, you redeclare it...

public class RegPanel extends JPanel {

    protected JCheckBox keynoteCheckBox;
    //...

    public RegPanel() {
        //...
        //initiate the checkbox and add it to the keynote panel
        JCheckBox keynoteCheckBox = new JCheckBox("Dinner and Keynote Speach");
        keynotePanel.add(keynoteCheckBox);

This leaves the instance field as null which will cause a NullPointerException

Also, this: regType == "Please select a type" is not how to compare Strings in Java, you want to use something more like "Please select a type".equals(regType)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you SO much! i've spent so much time combing over the same blocks of code and i kept overlooking that. – Stan Harris Oct 22 '15 at 05:48
  • 2
    @StanHarris, well if you problem is solved, then don't forget to "accept" the answer by clicking on the check mark. You have asked 14 questions and not once have you accepted an answer. – camickr Oct 22 '15 at 14:59