aspose file tools*
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
Author

calendar

Ebu Kon
Greenhorn

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>(CalendarGUI.java:63)
at CalendarGUI.main(CalendarGUI.java:122)

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",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};

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*/) {
//super("Calendar");
setSize(450, 275);
setLocation(200,200);
//myCalendar = cal;
//myCalendar.setMonth("January");

// 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));


comboMonth.addItemListener(this);


pnlData.add(comboMonth);
pnlData.add(comboCent);
pnlData.add(comboDec);
pnlData.add(comboYear);
pnlData.add(btnCalc);

btnCalc.addActionListener(this);

Container c = getContentPane();
c.add(pnlData, BorderLayout.NORTH);
//c.add(txtAreaMonth, BorderLayout.CENTER);
addWindowListener(this);
setVisible(true);
}


/**
* 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());
//myCalendar.setYear(textfieldyear);
System.out.println(comboMonth.getSelectedItem());
// txtAreaMonth.setText(myCalendar.monthToString());
}
}

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

public void itemStateChanged(ItemEvent e) {
//myCalendar.setMonth((String)e.getItem());
}

/**
* 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()*/);

//cg.actionPerformed();
}

}
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

See this:
at CalendarGUI.<init>(CalendarGUI.java:63)
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
Greenhorn

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
Greenhorn

Joined: Mar 26, 2004
Posts: 6
now it's May 11, 2006 10:12 AM in beijing
I'a chinese boy.
Anonymous
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: http://aspose.com/file-tools
 
subject: calendar