File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes calendar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calendar" Watch "calendar" New topic


Ebu Kon

Joined: May 08, 2006
Posts: 2
I'm working on a Calendar program. I'm getting these errors:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at CalendarGUI.<init>(
at CalendarGUI.main(

here is the source code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CalendarGUI extends JFrame implements ActionListener,
ItemListener, WindowListener {

//private Calendar myCalendar;

private JComboBox comboMonth;
private JComboBox comboCent;
private JComboBox comboDec;
private JComboBox comboYear;
private JMenuBar FileMenu;
private JMenu Menu;
private JPanel pnlData;
private JButton btnCalc;

private String names [] = { "January",

private int centuries [] = { 19,20 };
private int decades [] = { 0,1,2,3,4,5,6,7,8,9 };
private int year [] = { 0,1,2,3,4,5,6,7,8,9 };
* Answers a new GUI for use with the Calendar object
* @param Calendar cal The model calendar object

public CalendarGUI(/*Calendar cal*/) {
setSize(450, 275);
//myCalendar = cal;

// add comboboxes here use in 3 places in gui
comboMonth = new JComboBox(names);
btnCalc = new JButton("Show Calendar");
pnlData = new JPanel();
// pnlFileBar = new JPanel();

FileMenu = new JMenuBar();
// txtAreaMonth.setFont(new Font("Courier", Font.PLAIN, 16));




Container c = getContentPane();
c.add(pnlData, BorderLayout.NORTH);
//c.add(txtAreaMonth, BorderLayout.CENTER);

* Listen and handle action events
* @param ActionEvent e The event listened for

public void actionPerformed(ActionEvent e) {

if (e.getSource() == btnCalc) {
//int textfieldyear = Integer.parseInt(txtYear.getText());
// txtAreaMonth.setText(myCalendar.monthToString());

* Listen and handle item event
* @param ItemEvent e item (selected from month combo )

public void itemStateChanged(ItemEvent e) {

* Listen and handle window events
* Exits when window is closing.
* @param WindowEvent e the window event

public void windowClosing(WindowEvent e) { System.exit(0); }
public void windowClosed(WindowEvent e) { };
public void windowActivated(WindowEvent e) { };
public void windowDeactivated(WindowEvent e) { };
public void windowOpened(WindowEvent e) { };
public void windowIconified(WindowEvent e) { };
public void windowDeiconified(WindowEvent e) { };

public static void main(String args[]) {
CalendarGUI cg = new CalendarGUI(/*new Calendar()*/);


Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

See this:
at CalendarGUI.<init>(
That tells you that the exception occurred at line 63 of your class. It's a bit difficult for us to tell which line that is, but your editor should tell you. To help, the line above it:
at java.awt.Container.add(Unknown Source)
tells you that your code was calling the "add" method of something that's a subclass of Container. And the exception itself:
Exception in thread "main" java.lang.NullPointerException
says there was an unexpected null value. That would most likely be related to whatever you were passing to the "add" method.

Hope this helps you get started.
stevie liu

Joined: Mar 26, 2004
Posts: 6
It's really difficult for us to tell which line that is, you'd better show the line number of your code in jbuilder or eclipse.
[ May 10, 2006: Message edited by: stevie liu ]
stevie liu

Joined: Mar 26, 2004
Posts: 6
now it's May 11, 2006 10:12 AM in beijing
I'a chinese boy.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
looking at your code it seems that you only initalize one of your combo !!!
I agree. Here's the link:
subject: calendar
It's not a secret anymore!