Hey guys, I've been working on this issue for a couple days now and I can't seem to figure it out. I'm receiving an "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" in the following code. It seems to occure only when i try to use an array object but i can't figure out why. Any help will be appreciated. package timesheet;import java.awt.*;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JButton;import javax.swing.JComponent;import java.awt.Toolkit;import java.awt.BorderLayout;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;/** * * @author Jamie Snipes */public class Main extends JFrame implements ActionListener { public static void main(String[] args) { new Main().setVisible(true); } /** Creates a new instance of Main */ public Main() { initComponents(); pack(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = getSize(); setLocation(new Point((screenSize.width - frameSize.width), (screenSize.height - frameSize.width)/ 6 )); } /** * @param args the command line arguments */ private void initComponents() { mainPanel = new javax.swing.JPanel(); mainPanel.setLayout(new BorderLayout()); eventTrackerPanel = new javax.swing.JPanel(); //eventTrackerPanel.setLayout(new GridLayout(5,1, 5, 5)); projectTitleLabel = new javax.swing.JLabel(); buttonsPanel = new javax.swing.JPanel(); buttonsPanel.setLayout(new BorderLayout()); newItemPanel = new javax.swing.JPanel(); newItemPanel.setLayout(new FlowLayout()); startButton = new javax.swing.JButton("Start"); notesButton = new javax.swing.JButton("Add Note"); stopButton = new javax.swing.JButton("Stop"); menuBar = new javax.swing.JMenuBar(); menuItem= new javax.swing.JMenuItem("Exit"); menu= new javax.swing.JMenu("File"); addTimeSheetItem= new javax.swing.JButton("Add") ; projectNameBox= new javax.swing.JComboBox(); removeProjectButton=new javax.swing.JButton("remove") ; setTitle("Time Sheet"); addWindowListener(new java.awt.event.WindowAdapter() { //public void windowClosing(java.awt.event.WindowEvent r) { //exitForm(evt); //} }); //Menu Bar Itmes // menu.setMnemonic(KeyEvent.VK_A); // menu.getAccessibleContext().setAccessibleDescription(); menuBar.add(menu); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitMenuItemActionPerformed(evt); } }); menu.add(menuItem); setJMenuBar(menuBar); mainPanel.setLayout(new java.awt.BorderLayout()); mainPanel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(5, 5, 5, 5))); mainPanel.setMinimumSize(new java.awt.Dimension(800, 800)); projectTitleLabel.setText("Project Name:"); //Top Panel projectNameBox.setEditable(true); eventTrackerPanel.setLayout(new GridLayout(0,1, 5, 5)); projectNameBox.addActionListener(this); newItemPanel.add(projectNameBox); newItemPanel.add(addTimeSheetItem); // eventPanel (Center)Panel // buttonsPanel (Bottom) Panel buttonsPanel.add(startButton, BorderLayout.WEST); buttonsPanel.add(notesButton, BorderLayout.CENTER); buttonsPanel.add(stopButton, BorderLayout.EAST); // Main Panel mainPanel.add(newItemPanel, BorderLayout.NORTH); mainPanel.add(eventTrackerPanel, BorderLayout.CENTER); mainPanel.add(buttonsPanel, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(mainPanel); getContentPane().setSize(800, 800); } public void actionPerformed(ActionEvent e){ if(e.getSource()==projectNameBox){ projectArray[i]= new String(); projectArray[i]=(String)projectNameBox.getSelectedItem(); projectNameBox.addItem(projectArray[i]); i++; //projectNameBox.addItem(projectNameBox.getSelectedItem()); //projectName= (String)projectNameBox.getSelectedItem(); //System.out.println(projectName); //selectedProjectToggleButton[i]= new JToggleButton(); //projectArray[i]=projectName; //selectedProjectToggleButton[i].setText(projectName); //eventTrackerPanel.add(selectedProjectToggleButton[i]); //return; //eventTrackerPanel.add(removeProjectButton); //return; } else{}; } private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } // Variables declaration - do not modify private javax.swing.JPanel buttonsPanel; private javax.swing.JPanel newItemPanel; private javax.swing.JPanel eventTrackerPanel; private javax.swing.JMenuItem exitMenuItem; private javax.swing.JLabel feedbackLabel; public javax.swing.JMenu fileMenu; private javax.swing.JButton[] buttonArray; private javax.swing.JButton startButton; private javax.swing.JButton stopButton; private javax.swing.JButton notesButton; private javax.swing.JButton addTimeSheetItem; private javax.swing.JButton removeProjectButton; private javax.swing.JLabel projectTitleLabel; private javax.swing.JPanel mainPanel; private javax.swing.JMenuBar menuBar; private javax.swing.JMenuItem menuItem; private javax.swing.JMenu menu; private javax.swing.JComboBox projectNameBox; private java.awt.event.ActionListener isClicked; private int i=0; private javax.swing.JToggleButton[] selectedProjectToggleButton; private String[] projectArray; private java.lang.Boolean projectEntered; private String projectName; // End of variables declaration }
10/11/2006 12:11:10 PM
A stacktrace would be really helpful.
10/11/2006 12:29:06 PM
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at timesheet.Main.actionPerformed(Main.java:154) at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1197) at javax.swing.JComboBox.contentsChanged(JComboBox.java:1268) at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:100) at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:88) at javax.swing.JComboBox.actionPerformed(JComboBox.java:1251) at javax.swing.JTextField.fireActionPerformed(JTextField.java:487) at javax.swing.JTextField.postActionEvent(JTextField.java:668) at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:783) at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1571) at javax.swing.JComponent.processKeyBinding(JComponent.java:2763) at javax.swing.JComponent.processKeyBindings(JComponent.java:2798) at javax.swing.JComponent.processKeyEvent(JComponent.java:2726) at java.awt.Component.processEvent(Component.java:5265) at java.awt.Container.processEvent(Container.java:1966) at java.awt.Component.dispatchEventImpl(Component.java:3955) at java.awt.Container.dispatchEventImpl(Container.java:2024) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1810) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:672) at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:920) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:798) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:636) at java.awt.Component.dispatchEventImpl(Component.java:3841) at java.awt.Container.dispatchEventImpl(Container.java:2024) at java.awt.Window.dispatchEventImpl(Window.java:1774) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.EventQueue.dispatchEvent(EventQueue.java:463) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149) at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
10/11/2006 12:55:30 PM
doesn't the comp sci department have an honor code or something where you can only get help from the ta's or prof?btw, you imported the javax.swing.JButton package already. you don't need all this shit: startButton = new javax.swing.JButton();just declare it JButton startButton= new JButton("Start");same with all your JPanel, FlowLayout, etc statements. your loading the package a second time basically.and your problem is here:if(e.getSource()==projectNameBox){ projectArray[i]= new String(); projectArray[i]=(String)projectNameBox.getSelectedItem(); projectNameBox.addItem(projectArray[i]);i++;}1) you have not specified the size of the array in your main or at least locally.2) every time the action is done, your array will be erased. do you want this?[Edited on October 11, 2006 at 2:46 PM. Reason : make the time and go get help from your ta. there might be more problems. pwnd bitch!]
10/11/2006 2:44:46 PM
I had considered limiting the size to 10 items or using an array list in case i didn't want to limit the items. When i used array list i came out with the same error. I just figured it was something else. What i would like would be a time sheet that could take as many items as you wanted to put in. Since the most projects we might be working on in a day is 10, limiting it to 10 would be fine too. I'd just like to get passed this error one way or the other.
10/11/2006 2:57:19 PM
USE ECLIPSE TO WRITE JAVA CODEit makes it more idiot proof to debug your code
10/11/2006 3:01:55 PM
i'm using netbeans. I've never heard of eclipse. if you have it plug this code in and tell me what the prob is! Java really drives me nuts sometimes
10/11/2006 3:10:47 PM
you never initialized projectArray
10/11/2006 3:19:27 PM
k, so i initialized projectArray asprojectArray= new String[10];that fixes the problem. Thanks guys[Edited on October 11, 2006 at 3:45 PM. Reason : .]
10/11/2006 3:42:26 PM
now you need a means of handling sizing issues or you'll get an arrayIndexOutofBounds exception[Edited on October 11, 2006 at 6:41 PM. Reason : ]
10/11/2006 6:41:14 PM
PEBKAC
10/11/2006 9:07:50 PM
K, I'm back with another question. For some reason, on the JComboBox ActionListener, it seems that every time i hit the enter key in the combo box after entering text, it enters the text twice in the ComboBox drop down menu. I cant figure out why this is happening. Any help will be greatly appreciated. * Main.java * * Created on September 19, 2006, 9:50 AM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */package timesheet;import java.awt.*;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JButton;import javax.swing.JComponent;import java.awt.Toolkit;import java.awt.BorderLayout;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;/** * * @author Jamie Snipes */public class Main extends JFrame implements ActionListener{ public static void main(String[] args) { new Main().setVisible(true); } /** Creates a new instance of Main */ public Main() { initComponents(); pack(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = getSize(); setLocation(new Point((screenSize.width - frameSize.width), (screenSize.height - frameSize.width)/ 6 )); } /** * @param args the command line arguments */ private void initComponents() { mainPanel = new javax.swing.JPanel(); mainPanel.setLayout(new BorderLayout()); eventTrackerPanel = new javax.swing.JPanel(); //eventTrackerPanel.setLayout(new GridLayout(5,1, 5, 5)); projectTitleLabel = new javax.swing.JLabel(); buttonsPanel = new javax.swing.JPanel(); buttonsPanel.setLayout(new BorderLayout()); newItemPanel = new javax.swing.JPanel(); newItemPanel.setLayout(new FlowLayout()); startButton = new javax.swing.JButton("Start"); notesButton = new javax.swing.JButton("Add Note"); stopButton = new javax.swing.JButton("Stop"); menuBar = new javax.swing.JMenuBar(); menuItem= new javax.swing.JMenuItem("Exit"); menu= new javax.swing.JMenu("File"); addTimeSheetItem= new javax.swing.JButton("Add") ; projectArray= new String[10]; removeProjectButton=new javax.swing.JButton("remove") ; setTitle("Time Sheet"); //Menu Bar Itmes // menu.setMnemonic(KeyEvent.VK_A); // menu.getAccessibleContext().setAccessibleDescription(); menuBar.add(menu); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { if(e.getSource()==menuItem){ exitMenuItemActionPerformed(e); } } }); menu.add(menuItem); setJMenuBar(menuBar); mainPanel.setLayout(new java.awt.BorderLayout()); mainPanel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(5, 5, 5, 5))); mainPanel.setMinimumSize(new java.awt.Dimension(800, 800)); projectTitleLabel.setText("Project Name:"); //Top Panel projectNameBox= new javax.swing.JComboBox(); projectNameBox.setEditable(true); eventTrackerPanel.setLayout(new GridLayout(0,1, 5, 5)); projectNameBox.addActionListener(this); eventTrackerPanel.add(projectNameBox); newItemPanel.add(projectNameBox); newItemPanel.add(addTimeSheetItem); // eventPanel (Center)Panel // buttonsPanel (Bottom) Panel buttonsPanel.add(startButton, BorderLayout.WEST); buttonsPanel.add(notesButton, BorderLayout.CENTER); buttonsPanel.add(stopButton, BorderLayout.EAST); // Main Panel mainPanel.add(newItemPanel, BorderLayout.NORTH); mainPanel.add(eventTrackerPanel, BorderLayout.CENTER); mainPanel.add(buttonsPanel, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(mainPanel); getContentPane().setSize(800, 800); } public void actionPerformed(ActionEvent e){ if(e.getSource()==projectNameBox){ for(int i=0; i<=10; i++){ selectedProjectToggleButton= new javax.swing.JToggleButton[10]; projectArray[i]= new String(); selectedProjectToggleButton[i]= new javax.swing.JToggleButton(); projectArray[i]=(String)projectNameBox.getSelectedItem(); System.out.println(projectArray[0]); projectNameBox.addItem(projectArray[i]); selectedProjectToggleButton[i].setText(projectArray[i]); return; } } System.out.println(projectArray[1]); eventTrackerPanel.add(selectedProjectToggleButton[i]); //projectNameBox.addItem(projectNameBox.getSelectedItem()); //projectName= (String)projectNameBox.getSelectedItem(); //System.out.println(projectName); // //projectArray[i]=projectName; //selectedProjectToggleButton[i].setText(projectName); //eventTrackerPanel.add(selectedProjectToggleButton[i]); //return; //eventTrackerPanel.add(removeProjectButton); //return; } private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } // Variables declaration - do not modify private javax.swing.JPanel buttonsPanel; private javax.swing.JPanel newItemPanel; private javax.swing.JPanel eventTrackerPanel; private javax.swing.JMenuItem exitMenuItem; private javax.swing.JLabel feedbackLabel; public javax.swing.JMenu fileMenu; private javax.swing.JButton[] buttonArray; private javax.swing.JButton startButton; private javax.swing.JButton stopButton; private javax.swing.JButton notesButton; private javax.swing.JButton addTimeSheetItem; private javax.swing.JButton removeProjectButton; private javax.swing.JLabel projectTitleLabel; private javax.swing.JPanel mainPanel; private javax.swing.JMenuBar menuBar; private javax.swing.JMenuItem menuItem; private javax.swing.JMenu menu; private javax.swing.JComboBox projectNameBox; private java.awt.event.ActionListener isClicked; private int i; private javax.swing.JToggleButton[] selectedProjectToggleButton; private String[] projectArray; private java.lang.Boolean projectEntered; private String projectName; // End of variables declaration }
10/12/2006 3:48:26 PM
Are you in CSC116 or CSC216? You need to go get help from either the instructor or TA's. Also, take the time to learn how to debug rather than posting to a message board.
10/12/2006 4:16:54 PM
I actually graduated a couple of years ago and seeing as how I don't have an instructor I'm left to books, google and TWW.
10/12/2006 4:32:20 PM
It's pretty clear he's not in either 116 or 216. They don't teach that kind of stuff in either of those classes, nor would they have given him an assignment like this.Jeez, you people are itching to be narcs or something.
10/12/2006 8:47:09 PM
Your java is...well it just looks confused. You aren't really being consistent. Try to refactor out your ActionListeners or at the very least look at switching to the typical Anonymous inner class pattern that Swing usually subscribes to. Also when posting code use the [Code] tags so that it is more readable.Why java for a timesheet?Go look up Ruby on Rails and solve all your worries.
10/12/2006 9:10:18 PM
10/12/2006 11:51:04 PM
Your actionPerformed needs some cleanup. I have no idea if that will help you solve your specific problem, but it would be a good place to start. Figure out how/when your variables change.
10/13/2006 7:47:53 AM
I'm trying to develop some tools for the engineers in my lab to use and we can't spend money on it because Nortel has a software development group.... but they won't touch it because its such a small deal. So java is free and because i took it years ago, i know a little.Thanks for the help so far guys.
10/13/2006 9:47:26 AM
The problem is that an Item is entered two times if you hit the enter key after typing it into the boxAn item is entered only one time if you select the item from the drop down menu. I think the problem is def. with my action listener, but I'm not certain what exactly is wrong with it. I've seen several examples of Action Listener implementations and this seems to be ok from what i've see. Any of you guys see anything wrong here?[Code]package timesheet;import java.awt.*;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JButton;import javax.swing.JComponent;import java.awt.Toolkit;import java.awt.BorderLayout;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;/**** @author Jamie Snipes*/public class Main extends JFrame implements ActionListener{public static void main(String[] args) {new Main().setVisible(true);}/** Creates a new instance of Main */public Main() {initComponents();pack();Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();Dimension frameSize = getSize();setLocation(new Point((screenSize.width - frameSize.width),(screenSize.height - frameSize.width)/ 6 ));}/*** @param args the command line arguments*/private void initComponents() {mainPanel = new javax.swing.JPanel();mainPanel.setLayout(new BorderLayout());eventTrackerPanel = new javax.swing.JPanel();//eventTrackerPanel.setLayout(new GridLayout(5,1, 5, 5));projectTitleLabel = new javax.swing.JLabel();buttonsPanel = new javax.swing.JPanel();buttonsPanel.setLayout(new BorderLayout());newItemPanel = new javax.swing.JPanel();newItemPanel.setLayout(new FlowLayout());startButton = new javax.swing.JButton("Start");notesButton = new javax.swing.JButton("Add Note");stopButton = new javax.swing.JButton("Stop");menuBar = new javax.swing.JMenuBar();menuItem= new javax.swing.JMenuItem("Exit");menu= new javax.swing.JMenu("File");addTimeSheetItem= new javax.swing.JButton("Add") ;projectArray= new String[10];removeProjectButton=new javax.swing.JButton("remove") ;setTitle("Time Sheet");//Menu Bar Itmes// menu.setMnemonic(KeyEvent.VK_A);// menu.getAccessibleContext().setAccessibleDescription();menuBar.add(menu);menuItem.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent e) {if(e.getSource()==menuItem){exitMenuItemActionPerformed(e);}}});menu.add(menuItem);setJMenuBar(menuBar);mainPanel.setLayout(new java.awt.BorderLayout());mainPanel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(5, 5, 5, 5)));mainPanel.setMinimumSize(new java.awt.Dimension(800, 800));projectTitleLabel.setText("Project Name:");//Top PanelprojectNameBox= new javax.swing.JComboBox();projectNameBox.setEditable(true);eventTrackerPanel.setLayout(new GridLayout(0,1, 5, 5));projectNameBox.addActionListener(this);eventTrackerPanel.add(projectNameBox);newItemPanel.add(projectNameBox);newItemPanel.add(addTimeSheetItem);// buttonsPanel (Bottom) PanelbuttonsPanel.add(startButton, BorderLayout.WEST);buttonsPanel.add(notesButton, BorderLayout.CENTER);buttonsPanel.add(stopButton, BorderLayout.EAST);// Main PanelmainPanel.add(newItemPanel, BorderLayout.NORTH);mainPanel.add(eventTrackerPanel, BorderLayout.CENTER);mainPanel.add(buttonsPanel, BorderLayout.SOUTH);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);getContentPane().add(mainPanel);getContentPane().setSize(800, 800);}public void actionPerformed(ActionEvent e){if(e.getSource()==projectNameBox){for(int i=0; i<=100; i++){selectedProjectToggleButton= new javax.swing.JToggleButton[100];projectArray[i]= new String();selectedProjectToggleButton[i]= new javax.swing.JToggleButton();//projectArray[i]=(String)projectNameBox.getSelectedItem();System.out.println(projectArray[0]);projectNameBox.addItem("1");selectedProjectToggleButton[i].setText(projectArray[i]);eventTrackerPanel.add(selectedProjectToggleButton[i]);return;}}}private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {System.exit(0);}// Variables declaration - do not modifyprivate javax.swing.JPanel buttonsPanel;private javax.swing.JPanel newItemPanel;private javax.swing.JPanel eventTrackerPanel;private javax.swing.JMenuItem exitMenuItem;private javax.swing.JLabel feedbackLabel;public javax.swing.JMenu fileMenu;private javax.swing.JButton[] buttonArray;private javax.swing.JButton startButton;private javax.swing.JButton stopButton;private javax.swing.JButton notesButton;private javax.swing.JButton addTimeSheetItem;private javax.swing.JButton removeProjectButton;private javax.swing.JLabel projectTitleLabel;private javax.swing.JPanel mainPanel;private javax.swing.JMenuBar menuBar;private javax.swing.JMenuItem menuItem;private javax.swing.JMenu menu;private javax.swing.JComboBox projectNameBox;private java.awt.event.ActionListener isClicked;private int i;private javax.swing.JToggleButton[] selectedProjectToggleButton;private String[] projectArray;private java.lang.Boolean projectEntered;private String projectName;// End of variables declaration}[/Code]
10/13/2006 12:41:31 PM
In case anyone has this problem in the future, I finally got it figured out. When you edit the combo box, the combo box action listener returns two commands e.getActionCommand returns these:comboBoxEditedcomboBoxChangedWhen you select an item from the drop down menue.getActionCommand returns:comboBoxChangedso i just added an if statement if(e.getActionCommand()=="comboBoxChanged")fixed it right up
10/16/2006 4:23:38 PM