BlackSheep Suspended 1575 Posts user info edit post |
Guys, I'm having some problems with this program I've written and i can not figure out why i'm getting the error I'm getting.
Below in the action listener Othertimer[i] i get a
Quote : | "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at timesheet.Main$2$2.actionPerformed(Main.java:129)" |
public Main() { initComponents(); getContentPane().setSize(800, 800); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = getSize(); setLocation(new Point((screenSize.width - frameSize.width) / 2,(screenSize.height - frameSize.width) / 2)); } /** * @param args the command line arguments */ private void initComponents() { mainPanel = new javax.swing.JPanel(); mainPanel.setLayout(new BorderLayout()); projectTitleLabel = new javax.swing.JLabel(); buttonsPanel = new javax.swing.JPanel(); buttonsPanel.setLayout(new BorderLayout()); newItemPanel = new javax.swing.JPanel(); newItemPanel.setLayout(new FlowLayout()); removeProjectButton = new javax.swing.JButton("Remove"); 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") ; eventTrackerPanel= new javax.swing.JPanel(); projectArray= new String[100]; removeProjectButton=new javax.swing.JButton("remove") ;
setTitle("Time Sheet"); //Menu Bar Itmes menuBar.add(menu); menuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { if(evt.getSource()==menuItem){ 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= new javax.swing.JComboBox(); projectNameBox.setEditable(true); projectNameBox.addActionListener(new java.awt.event.ActionListener(){ public void actionPerformed(ActionEvent dropDown){ System.out.println("Drop Down item is" + dropDown.getActionCommand()); if(dropDown.getActionCommand()=="comboBoxChanged"){ selectedProjectToggleButton= new javax.swing.JToggleButton[100]; StartTimer= new ActionListener[100]; OtherTimer= new ActionListener[100]; ButtonClicked= new ActionListener[100]; timeField= new javax.swing.JTextArea[100]; timeField[i]= new javax.swing.JTextArea("Time: " + 0); projectArray[i]= new String(); selectedProjectToggleButton[i]= new javax.swing.JToggleButton(); projectArray[i]=(String)projectNameBox.getSelectedItem(); System.out.println("Array i=" + projectArray[i]); projectNameBox.addItem(projectArray[i]); selectedProjectToggleButton[i].setText(projectArray[i]); selectedProjectToggleButton[i].addActionListener(StartTimer[i]= new ActionListener() { public void actionPerformed(java.awt.event.ActionEvent srtTimer) { Calendar cal= Calendar.getInstance(); Date date = cal.getTime(); DateFormat dateFormatter =DateFormat.getTimeInstance(); System.out.println(date.toString()); b=dateFormatter.format(date); String currentTime=dateFormatter.format(date); System.out.println("i= " + i); timeField[i-1].setText(b); System.out.println("source is" + srtTimer.getActionCommand());
pack(); } }); selectedProjectToggleButton[i].addActionListener(OtherTimer[i]= new ActionListener() { public void actionPerformed(java.awt.event.ActionEvent otrTimer) { Calendar cal= Calendar.getInstance(); Date date = cal.getTime(); DateFormat dateFormatter =DateFormat.getTimeInstance(); System.out.println(date.toString()); b=dateFormatter.format(date); String currentTime=dateFormatter.format(date); System.out.println("a= " + a); timeField[a].setText(b); System.out.println("source is" + otrTimer.getActionCommand());
pack(); } }); timer2= new Timer(1000, StartTimer[a]);
selectedProjectToggleButton[i].addActionListener(ButtonClicked[i]=new ActionListener(){ public void actionPerformed(ActionEvent toggleClick){ if(toggleClick.getActionCommand()==selectedProjectToggleButton[i-1].getText()){ if(selectedProjectToggleButton[i-1].isSelected()==true){ startTime= timeField[i-1].getText(); timer.start(); } if(selectedProjectToggleButton[i-1].isSelected()==false){ timer.stop(); } } if(toggleClick.getActionCommand()!=selectedProjectToggleButton[i-1].getText() && toggleClick.getActionCommand()!= null){ for(a=0; a<=99; a++){ System.out.println("ActionCommand= " + projectArray[a]); if(projectArray[a].equals(toggleClick.getActionCommand())){ timer2.start(); System.out.println("One of the Previous and the index for the clicked button is " + a); break; } } }
} });
timer= new Timer(1000, StartTimer[i]);
11/2/2006 10:47:36 AM |
BlackSheep Suspended 1575 Posts user info edit post |
The problem occurs when I add a second project to the interface and attempt to start its timer. If anyone would like to have to program to run let me know and I'll get it to you. Thanks for any help. 11/2/2006 10:49:04 AM |
MiniMe_877 All American 4414 Posts user info edit post |
step 1 - get Eclipse step 2 - setup project in Eclipse step 3 - debug code in eclipse step 4 - find and fix problem 11/2/2006 12:21:39 PM |
BlackSheep Suspended 1575 Posts user info edit post |
why is eclipse any better than netbeans? After downloading it, it seems somewhat complicated how to debug the program. I've never heard of a IDE being able to tell you exactly what you messed up. That sounds pretty cool if that is what you are saying. I couldn't get it to tell me any more than netbeans told me though.
Can i send you the file and let you run it in and see if you come up with something? 11/2/2006 1:40:11 PM |
MiniMe_877 All American 4414 Posts user info edit post |
well, with the breakpoints in the right place, you can step through your code line by line, watch what values variables are, see exactly what is causing your NPE. I would say that Eclipse's debugging environment is superior to any that I've seen
I'm not about to do the work for you, but I'll tell you the tools to use to get the job done (its the whole teach a man to fish thing)
if you really want help, maybe some other Sheep can tell you exactly what you're doing wrong 11/2/2006 2:24:05 PM |
CapnObvious All American 5057 Posts user info edit post |
Quote : | "step 1 - get Eclipse step 1.5 - Cook and eat a 3 course dinner while you wait for Eclipse to boot up step 2 - setup project in Eclipse step 3 - debug code in eclipse step 4 - find and fix problem" |
11/2/2006 2:48:13 PM |
BlackSheep Suspended 1575 Posts user info edit post |
ur sheep link is broken 11/2/2006 2:57:27 PM |
MiniMe_877 All American 4414 Posts user info edit post |
^^ no step 1.5 for me, I'm lucky to have a speedy development machine here at work
helping you any further is not worth the effort, you can figure this out on your own from here 11/2/2006 3:13:29 PM |
BlackSheep Suspended 1575 Posts user info edit post |
Pick up some Duke dollars for answering this question.
http://forum.java.sun.com/thread.jspa?threadID=782346 11/2/2006 3:23:06 PM |
MiniMe_877 All American 4414 Posts user info edit post |
i've found your problem, using Eclipse and its debugger
see screenshot with highlights to the important bits: http://img146.imageshack.us/my.php?image=eclipseld7.png
you are accessing a null array element, which causes the NPE, a == 0 when I think you want it to equal 1 to get to the right array element
lesson here: LEARN TO USE A DEBUGGER 11/2/2006 3:48:43 PM |
BlackSheep Suspended 1575 Posts user info edit post |
I'm pretty sure that it needs to be 0. Since the arrays fill from 0-100, zero will be filled first. When a new item is added, it will be element 1 and the next will be 2 and so on.
Since one action listener handles the last element added, in the scenerio where you have two items it would handle item 1 which is [i-1] since i increments after each item is added. for example: i=0; first button is added and goes into postion 0 of array. i++; // i now is 1 Second item is added and goes into position 1 of the array. i++; // i now is 2 so [i-1] will get you to where the last element is stored.
The action listener that we are having trouble with is handling the previous items entered. This is why it searches from 0-100 to find an item that matches the button that was clicked. When it finds the item, it sets its array pointer to "a".
Since the first item added is at position 0 of the array, "a" should therefore be set to 0.
This is the confusing part for me. We know that position 0 is filled but some how it is showing up as null.
[Edited on November 2, 2006 at 4:25 PM. Reason : added example] 11/2/2006 4:21:53 PM |
MiniMe_877 All American 4414 Posts user info edit post |
holy fuck this is terrible
first of all, there's no comments anywhere in your program second your "Add" button doesn't do a God damn thing, your "add" action only fires because you type in text and the focus changes from the text box to the button
honestly, your best bet would be to scrap the entire thing and start over
but you can at least fix your problem by removing the line timeField = new javax.swing.JTextArea[100]; since it reinitializes the timeField array each time you add a new button / timer / whatever (this means you lose timeField[0] that you had set before), have the timeField array set in one location (at the timeField declaration), not inside the ActionListener
that is causing your NPE, but there are WAY more errors in this code than that
[Edited on November 2, 2006 at 4:33 PM. Reason : detail]11/2/2006 4:28:51 PM |
BlackSheep Suspended 1575 Posts user info edit post |
the reason the button doesn't do anything because i haven't told it to yet.
but i am a complete dumbass for not seeing where i was constructing those elements. Thanks a bunch man. 11/2/2006 4:38:31 PM |
Perlith All American 7620 Posts user info edit post |
^^ See previous threads. Give the dude a break ... had most of this dumped on him.
This is a dumb question but ... where in your code are you defining i? The line "timeField[i]= new javax.swing.JTextArea("Time: " + 0);" will cry itself if that's not defined.
[Edited on November 2, 2006 at 4:45 PM. Reason : .] 11/2/2006 4:43:47 PM |
surge Suspended 246 Posts user info edit post |
Surge's Steps for Successful Programming:
1. Read the damn book AND/OR go to class 2. Listen in class (if you go to class) 3. Go to the office hours, get help, and don't wait till 2 days before its due to start it.
I had to take csc216 a second time because I didn't do #3. 11/2/2006 5:56:56 PM |
ruffler Veteran 108 Posts user info edit post |
Use a debugger. Put breakpoints Step through the code and inspect the values of variables. 11/2/2006 7:05:59 PM |
MiniMe_877 All American 4414 Posts user info edit post |
^^ dude isnt in school anymore, so I guess we cut him a little slack, and obviously the program he's writing isnt for a class
FYI for anyone who ever may need it, Eclipse can do remote debugging to a JVM on a remote system. I was using it last week to debug a 3-tier (console, server, agent) Java system on three separate remote machines from my development machine. Its pretty sweet to have that sort of debugging power. 11/2/2006 9:13:33 PM |