I am getting the error in the title of this post when I try to run my ColorFactory class. Can someone tell me where Im going wrong?
Heres my code:

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

public class ColorFactory extends JFrame
{
   //Fields of the class
   private JLabel messageLabel;               //A message to the user
   private JPanel panelNorth;                 //A holding panel for the north border panel
   private JPanel panelSouth;                 //A holding panel for the south border panel
   private JPanel panelCenter;                //A holding panel for the center border panel
   private JButton redButton;                 //Changes panel color to red
   private JButton orangeButton;              //Changes panel color to orange
   private JButton yellowButton;			  //Changes panel color to yellow
   private JRadioButton greenRadioButton;     //Changes text color to green     
   private JRadioButton blueRadioButton;	  //Changes text color to blue
   private JRadioButton cyanRadioButton;      //Changes text color to cyan
   private final int WINDOW_WIDTH = 500;      //Sets width of window
   private final int WINDOW_HEIGHT = 300;     //Sets height of window
   
   /** 
    Constructor +ColorFactory()
    */
    public ColorFactory()
    {
    	//Set the title of the window to Color Factory.
    	setTitle("Color Factory");
    	
    	//Set the size of the window using the constants 500 for window width and 300 for window height.
    	setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    	
    	//Specify what happens when the close button is clicked.
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	//Get the content pane of the JFrame and set the layout manager to border layout.
    	getContentPane().setLayout(new BorderLayout());
    	
    	//Call the method buildNorthPanel to build the top panel.
    	buildNorthPanel();
    	
    	//Add panelNorth to the north part of the content pane.
    	add(panelNorth, BorderLayout.NORTH);
    	
    	//Call the method buildSouthPanel to build the bottom panel.
    	buildSouthPanel();
    	
    	//Add panelSouth to the south part of the content pane.
    	add(panelSouth, BorderLayout.SOUTH);
    	
    	//Create a label that contains the message “Top buttons change the panel color and
		//bottom radio buttons change the text color.”
		messageLabel = new JLabel("Top buttons change the panel color and bottom radio buttons change the text color.");
		
		//Add this label to the center part of the content pane.
		add(panelCenter, BorderLayout.CENTER);
		
		//Use setVisible method to display window on screen.
		setVisible(true); 
	
	}//end constructor	
		
		/*
		 The buildNorthPanel method creates a panel that contains three buttons, red, orange, and yellow in the top panel.
		*/
		private void buildNorthPanel()
		{
			//Create three buttons, red, orange, and yellow.
			redButton = new JButton("Red");
			orangeButton = new JButton("Orange");
			yellowButton = new JButton("Yellow");
			
			//Use flow layout for the panel. 
			setLayout(new FlowLayout());
			
			//Set the background to be white.
			panelNorth.setBackground(Color.WHITE);
			
			//Display red, orange, and yellow buttons in their respective colors.
			redButton.setBackground(Color.RED);
			orangeButton.setBackground(Color.ORANGE);
			yellowButton.setBackground(Color.YELLOW);
			
			//Add button listener that implements action listener for each button.
			redButton.addActionListener(new ButtonListener());
			orangeButton.addActionListener(new ButtonListener());
			yellowButton.addActionListener(new ButtonListener());
				
		}//end buildNorthPanel method
    	
    		/*
		 The buildSouthPanel method creates a panel that contains three radio buttons, green, blue, and cyan in the 
		 bottom panel.
		*/
		private void buildSouthPanel()
		{
			//Create three buttons, green, blue, and cyan.
			greenRadioButton = new JRadioButton("Green");
			blueRadioButton = new JRadioButton("Blue");
			cyanRadioButton = new JRadioButton("Cyan");
			
			//Use flow layout for the panel. 
			setLayout(new FlowLayout());
			
			//Set the background to be white.
			panelSouth.setBackground(Color.WHITE);
			
			//Display green, blue, and cyan radio buttons in their respective colors.
			greenRadioButton.setBackground(Color.GREEN);
			blueRadioButton.setBackground(Color.BLUE);
			cyanRadioButton.setBackground(Color.CYAN);
			
			//Add button listener that implements action listener for each radio button.
			greenRadioButton.addActionListener(new RadioButtonListener());
			blueRadioButton.addActionListener(new RadioButtonListener());
			cyanRadioButton.addActionListener(new RadioButtonListener());
				
		}//end buildSouthPanel method
		
		/*
		 Write a private inner class called ButtonListener that implements ActionListener. It
					should contain an actionPerformed method to handle the button events. 
		*/
		private class ButtonListener implements ActionListener
		{
			//actionPerformed method to handle all button events for red, orange, and yellow buttons.
			public void actionPerformed(ActionEvent e)
			{
				//Determine which button was clicked to change color of center panel to respective color.
				if (e.getSource() == redButton)
					panelCenter.setBackground(Color.RED);
				else if (e.getSource() == orangeButton)
					panelCenter.setBackground(Color.ORANGE);
				else if (e.getSource() == yellowButton)
					panelCenter.setBackground(Color.YELLOW);
			
			}//end actionPerformed method
		
		}//end ButtonListener inner class
		
			/*
		 Write a private inner class called RadioButtonListener that implements ActionListener. It
					should contain an actionPerformed method to handle the radio button events. 
		*/
		private class RadioButtonListener implements ActionListener
		{
			//actionPerformed method to handle all radio button events for green, blue, and cyan buttons.
			public void actionPerformed(ActionEvent e)
			{
				//Determine which radio button was clicked to change color of text in center panel to respective color.
				if (e.getSource() == greenRadioButton)
					panelCenter.setForeground(Color.GREEN);
				else if (e.getSource() == blueRadioButton)
					panelCenter.setForeground(Color.ORANGE);
				else if (e.getSource() == cyanRadioButton)
					panelCenter.setForeground(Color.YELLOW);
			
			}//end actionPerformed method
		
		}//end RadioButtonListener inner class 		
	
}//end class

In a separate driver file, I have this:

public class Good_Brandon_Lab_08 //MUST match the file name!
{
   public static void main (String[ ] args) 
   {
		//Identify Programmer, Class, and Program Number 
		System.out.println("Program results by Brandon Good for CSCI 2911-H.");
		System.out.println("This program solves Lab Week 8.");
		
		//Create instance of ColorFactory.
		ColorFactory colors = new ColorFactory(); 		

   }//end main method
}//end class

Any suggestions would be greatly appreciated!

Recommended Answers

All 5 Replies

Launch Good_Brandon_Lab_08 class:

>java Good_Brandon_Lab_08

You need to share with us the class and line number where the error is thrown

This is exactly the output I get inside JCreator when I run the driver file:
--------------------Configuration: <Default>--------------------
Program results by Brandon Good for CSCI 2911-H.
This program solves Lab Week 8.
Exception in thread "main" java.lang.NullPointerException
at ColorFactory.buildNorthPanel(ColorFactory.java:174)
at ColorFactory.<init>(ColorFactory.java:137)
at Good_Brandon_Lab_08.main(Good_Brandon_Lab_08.java:30)

Process completed.

Both the class file and driver file compiles.

P.S. I tried to launch the driver file in the command prompt like adatapost suggested, no joy..

Instantiate the JPanel instances:

public class ColorFactory extends JFrame
{
   //Fields of the class
   private JLabel messageLabel;  
   private JPanel panelNorth=new JPanel();                 //A holding panel for the north border panel
   private JPanel panelSouth=new JPanel();;                 //A holding panel for the south border panel
   private JPanel panelCenter=new JPanel();;                //A holding panel for the center border panel

  ......
  ......

Thanks! That got it to display a window, but a big blank window with nothing I designed. I'm exhausted and I'll look at it tomorrow..

Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.