Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA GUI Problem I have writtern my code (it is below the GUI output examples) b

ID: 3860508 • Letter: J

Question

JAVA GUI Problem

I have writtern my code (it is below the GUI output examples) but am getting messages about -Xlint:unchecked and -Xling:deprecation and there's a null exception error. If someone could look throw my code and find what I'm doing wrong in with the JList component (I'm pretty sure this is causing the major issue) and maybe the Clear button, I'd appreciate it.

Here is what the GUI should like upon completion:

  

//**************************************************************
// Programmer: Jennifer Bailey
// CTP 150 - Section 876
// Lab 7
//*************************************************************

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


/**
   This class creates the window for the workshop registration
*/
public class CRS extends JFrame
{
   private JButton calcButton; // To calculate the total cost
   private JButton clearButton; //To clear the user selections
   private JButton exitButton; // To exit the application
  
   private JPanel workshopsPanel;
   private JPanel descriptPanel;
   private JPanel attPanel;
   private JPanel dinnerPanel;
   private JPanel buttonPanel;
  
   private AttendantType type;
   private DinnerSpeech diner;
   private Workshops lessons;
  
   /**
Constructor
   */
   public CRS()
   {
//Set the title
setTitle("Conference Registration System");

//Specify an action for the close button
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//add layout manager
setLayout(new BorderLayout());

//build panels
buildButtonPanel();

//add the panels to the content pane
add(descriptPanel, BorderLayout.NORTH);
add(attPanel, BorderLayout.WEST);
add(dinnerPanel, BorderLayout.CENTER);
add(workshopsPanel, BorderLayout.EAST);
add(buttonPanel, BorderLayout.SOUTH);

//pack and display
pack();
setVisible(true);
   }
  
   private void buildButtonPanel()
   {
// Create the buttons
calcButton = new JButton("Calculate Charges");
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");

// Add an action listener to the buttons.
calcButton.addActionListener(new CalcButtonListener());
clearButton.addActionListener(new ClearButtonListener());
exitButton.addActionListener(new ExitButtonListener());

//Put the buttons in their own panel.
buttonPanel = new JPanel();
buttonPanel.add(calcButton);
buttonPanel.add(clearButton);
buttonPanel.add(exitButton);
   }
  
   /**
CalcButton Listener
   */
   private class CalcButtonListener implements ActionListener
   {
/**
   actionPerformed method
   @param e An ActionEvent object.
*/
public void actionPerformed(ActionEvent e)
{
   double totalCost;
  
   //Display error message if user hasn't selected
   if(type.getAttCost() == 0 || lessons.getWorkshopCosts() == 0)
JOptionPane.showMessageDialog(null, String.format("Please make sure you've selected a registration type " +
"and at least one workshop to attend."));

   //calculate the total charge
   totalCost = type.getAttCost() + diner.getDSCost(); lessons.getWorkshopCosts();
   JOptionPane.showMessageDialog(null, String.format("Total Charges: $%,.2f",totalCost));
}
   }
  
   /**
ClearButtonListener will handle clearing the info
enter by the user when clicked
   */
   private class ClearButtonListener implements ActionListener
   {
/**
   actionPerformed method
   @param e An ActionEvent object.
*/

public void actionPerformed(ActionEvent e)
{
   //write method to set attendant type to default setting
   type.attGroup.clearSelection();
  
   //write method to uncheck dinner and speech checkbox
   diner.dinnerSpeech.setSelected(false);
  
   //write method to clear choices on workshop list
   lessons.workshops.removeAll();
}
   }
  
  
   /**
ExitButtonListener is an action listener class for the
exitButton component.
   */

   private class ExitButtonListener implements ActionListener
   {
/**
   actionPerformed method
   @param e An ActionEvent object.
*/

public void actionPerformed(ActionEvent e)
{
   System.exit(0);
}
   }
  
   /**
The main method creates an instance of the EventCharge class
and causes it to display the GUI window.
   */
   public static void main(String[] args)
   {
CRS person1 = new CRS();
   }

}

/**
   This class will create and display a description of the window.
*/
class Description extends JPanel
{
   private JLabel description; //To display the description
  
   /**
Constructor
   */
   public Description()
   {
//Create the label
description = new JLabel("Select Registration Options");

//Add the label to the panel
add(description);
   }
}

/**
   This class will create and display a radio button option for the
   user to choose if they're a student or general attendant.
*/

class AttendantType extends JPanel
{
   public final double GENERAL = 895.0; //$895 for the general attendant
   public final double STUDENT = 495.0; //$495 for the student attendant
  
   private JRadioButton general;
   private JRadioButton student;
   public ButtonGroup attGroup; //radio button group for type of attenant
  
   /**
Constructor
   */
   public AttendantType()
   {
//Create radio buttons
general = new JRadioButton("General Registration", true);
student = new JRadioButton("Student Registration");

//Group radio buttons
attGroup = new ButtonGroup();
attGroup.add(general);
attGroup.add(student);

// Create a GridLayout manager.
setLayout(new GridLayout(1, 2));

// Create a border.
setBorder(BorderFactory.createTitledBorder("Registration Type"));

// Add the radio buttons to this panel.
add(general);
add(student);
   }
  
   /**
The getAttCost method returns the cost for the selected attendant registration.
@return attCost One of the constants GENERAL or STUDENT will be returned.
   */

   public double getAttCost()
   {
double attCost = 0.0;

if (general.isSelected())
   attCost = GENERAL;
else if (student.isSelected())
   attCost = STUDENT;

return attCost;
   }

}

/**
   This class will create and display a checkbox option for the
   user to choose if they want to attend an optional dinner and keynote speech.
*/

class DinnerSpeech extends JPanel
{
   public final double DINNER = 30.0; //$30 per person cost for the dinner & speeach
  
   public JCheckBox dinnerSpeech;
  
   /**
Contructor
   */
   public DinnerSpeech()
   {
//Create a Gridlayout manager
setLayout(new GridLayout(1,0));

//Create checkbox
dinnerSpeech = new JCheckBox("Dinner and Keynote Speech", false);

//Add checkbox to the panel
add(dinnerSpeech);
   }
  
   /**
The getDSCost method returns the cost for the dinner and keynote speech.
@return attCost One of the constants GENERAL or STUDENT will be returned.
   */

   public double getDSCost()
   {
double keynoteCost = 0.0;

if (dinnerSpeech.isSelected())
   keynoteCost = DINNER;
else
   keynoteCost = 0.0;

return keynoteCost;
   }
}

/**
   This class will create and display a list of workshops for the
   user to choose as many as they want and in any order.
*/
class Workshops extends JPanel
{
   public final double ECOMMERCE = 295.0; //cost of ecommerce workshop
   public final double FUTURE_WEB = 295.0; //cost of future of the web workshop
   public final double JAVA = 395.0; //cost of advanced java workshop
   public final double NET_SECURITY = 395.0; //cost of network security workshop

   public JList workshops;
   private String[] seminars = {"Introduction to E-commerce", "The Future of the Web",
"Advanced Java Programming", "Network Security"};
  
   /**
Constructor
   */
   public Workshops()
   {
//Create a list
workshops = new JList(seminars);

//set selection mode
workshops.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

//set visible rows
workshops.setVisibleRowCount(4);

//create layout manager
setLayout(new GridLayout(1,1));

//create a border
setBorder(BorderFactory.createTitledBorder("Workshops"));

//add list to panel
add(workshops);
   }
  
   /**
The getWorkshopCosts will get the total costs for the workshops
the user will attend.
@return workshopTotal will hold the total costs of the selected workshops
   */

   public double getWorkshopCosts()
   {
double ecommerce; //charges of e-commerce workshop
double web; //charges of future of the web workshop
double java; //charges of advanced java workshop
double security; //charges of network security workshop

double workshopTotal = 0.0; //total cost of selected workshops

//Get the selected values
Object[] selected = workshops.getSelectedValues();

if(selected.equals("Introduction to E-commerce"));
   ecommerce = ECOMMERCE;
ecommerce = 0.0;
  
if(selected.equals("The Future of the Web"));
   web = FUTURE_WEB;
web = 0.0;

if(selected.equals("Advanced Java Programming"));
   java = JAVA;
java = 0.0;
  
if(selected.equals("Network Security"));
   security = NET_SECURITY;
security = 0.0;

//Get total
workshopTotal = ecommerce + web + java + security;

return workshopTotal;
   }
}

Explanation / Answer

The error was that you were not intializing your private JPanel Variables.

You can modify your private JPanel varibales like below , it will work , it will not throw NullPointerException Now

private JPanel workshopsPanel=new JPanel();
private JPanel descriptPanel= new JPanel();
private JPanel attPanel=new JPanel();
private JPanel dinnerPanel=new JPanel();
private JPanel buttonPanel=new JPanel(
);

I hope it is clear , Please upvote my Question If you satisfied, Else Please Comment your concern

Cheers :)