Results 1 to 14 of 14
  1. #1
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default Timer not ticking in GUI edited JFrame

    Hi all.
    The below code constructs and starts a simple Timer in the 'myClass()' constructor method. As soon as the first timer interval fires, a run time error is thrown.
    The problematic class was constructed by the NetBeans GUI editor. I tested the same code on another class I manually coded that also extends JFrame and it functioned correctly. Does anyone have a theory to why this may happen?

    The error reads: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    import javax.swing.Timer

    Java Code:
    public class myClass extends JFrame, implements ActionListener
    {
    	private javax.swing.Timer timer
    	
    	public myClass()
    	{
    		timer = new javax.swing.Timer(1000,this);
    		timer.addActionListener(this);
    		timer.start();
    	}
    	
    	public void actionPerformed(Actionevent e)
    	{
    		if(e.getSource()==timer)
    			// perform task
    	}
    Sorry, I only speak machine language. Yes or a No?:confused:

  2. #2
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    The problem is that you are using a swing timer outside of a gui enviroment. I don't know the details, but when using the swing timer, some swing stuff on the background has to be going on. Use the other java-timer and you should be fine.

  3. #3
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default

    The class extends javax.swing.JFrame though?
    Sorry, I only speak machine language. Yes or a No?:confused:

  4. #4
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default

    Ive never used another timer, any tips on achieving the above?
    Sorry, I only speak machine language. Yes or a No?:confused:

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    From your previous posting you stated:

    I have isolated the code above, the rest of the code is irrelevant
    The code you posted is useless. We have no idea what statement caused the problem. And until you solve the problem you don't know what is relevant or not. We can't tell from the code how you create and invoke the class.

    That fact that your class extends JFrame means nothing. If the frame isn't visible then the EDT has started yet which may (or may not) be a problem as has already been suggested.

    That is why you need to post a SSCCE.

  6. #6
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default

    Please see SSCCE

    Java Code:
    import java.util.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class StartUpFrame extends javax.swing.JFrame implements ActionListener
    {
    
        private ArrayList<Player> p1Team = new ArrayList();
        private ArrayList<Player> pcTeam = new ArrayList();
        private SelectionFrame sf;
        [COLOR="Red"]private javax.swing.Timer tmr;[/COLOR]
       
        /** Creates new form StartUpFrame */
        public StartUpFrame() {
    
            initComponents();
            setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
            selectBtn.addActionListener(this);
            toggleCoin.addActionListener(this);
    
         [COLOR="Red"]   tmr = new javax.swing.Timer(200,this);
            tmr.addActionListener(this);
    [/COLOR]
    
            generatePitchReport();
                
           
    
        }
       
        
        private void initComponents() {
           // add all components
            
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new StartUpFrame().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JTabbedPane TabbedPane;
        private javax.swing.JButton coin;
        private javax.swing.JPanel ctPanel;
        private javax.swing.JSeparator jSeparator1;
        private javax.swing.JSeparator jSeparator2;
        private javax.swing.JSeparator jSeparator3;
        private javax.swing.JSeparator jSeparator4;
        private javax.swing.JPanel logPanel;
        private javax.swing.JLabel pReportLbl;
        private javax.swing.JLabel prHeadingLbl1;
        private javax.swing.JLabel selHeadingLbl;
        private javax.swing.JButton selectBtn;
       [COLOR="Red"] private javax.swing.JToggleButton toggleCoin;[/COLOR]
        private javax.swing.JLabel tossHeadingLbl;
        // End of variables declaration
    
        public void actionPerformed(ActionEvent e)
        {
    
            if(e.getActionCommand().compareTo("Proceed")==0)
            {
                this.setDefaultCloseOperation(javax.swing.JFrame.HIDE_ON_CLOSE);
                selectBtn.setEnabled(false);
                this.setVisible(false);
                sf = new SelectionFrame(this);
            }
    
            if(e.getSource()==toggleCoin)
            {
                if(toggleCoin.getText().compareTo("Heads")==0)
                    toggleCoin.setText("Tails");
                else
                    toggleCoin.setText("Heads");
    
               [COLOR="Red"] tmr.start();[/COLOR]
            }
    
            [COLOR="Red"]if(e.getSource()==tmr)
            {
                JOptionPane.showMessageDialog(null,"Timer test");
                tmr.stop();
                
            }[/COLOR]
    
        }
    Sorry, I only speak machine language. Yes or a No?:confused:

  7. #7
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    From your previous posting you stated:

    If the frame isn't visible then the EDT has started yet which may (or may not) be a problem as has already been suggested.

    That is why you need to post a SSCCE.
    I'm not entirely sure what you mean by EDT, but if you mean the timer kicks in before the frame is visible, I changed the code so the timer starts once a button is clicked as per the above.
    Sorry, I only speak machine language. Yes or a No?:confused:

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please re-read the link on how to create an SSCCE as yours isn't. No one can compile or run your code. No we don't want to see the whole program but rather a small distillation of it. The link will explain all.

  9. #9
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default

    Strange, the below SSCCE tested correctly in a new Textpad document but fails in NetBeans. Any reason for that?
    Click 'Heads' button on JFrame.

    Java Code:
    import java.util.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class StartUpFrame extends javax.swing.JFrame implements ActionListener
    {
    
    
        private javax.swing.Timer tmr;
    
        /** Creates new form StartUpFrame */
        public StartUpFrame() {
    
            initComponents();
            setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
            selectBtn.addActionListener(this);
            toggleCoin.addActionListener(this);
    
            tmr = new javax.swing.Timer(200,this);
            tmr.addActionListener(this);
    
    
    
    
    
    
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            TabbedPane = new javax.swing.JTabbedPane();
            ctPanel = new javax.swing.JPanel();
            tossHeadingLbl = new javax.swing.JLabel();
            jSeparator3 = new javax.swing.JSeparator();
            pReportLbl = new javax.swing.JLabel();
            jSeparator2 = new javax.swing.JSeparator();
            selectBtn = new javax.swing.JButton();
            jSeparator4 = new javax.swing.JSeparator();
            prHeadingLbl1 = new javax.swing.JLabel();
            selHeadingLbl = new javax.swing.JLabel();
            toggleCoin = new javax.swing.JToggleButton();
            coin = new javax.swing.JButton();
            logPanel = new javax.swing.JPanel();
            jSeparator1 = new javax.swing.JSeparator();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setBounds(new java.awt.Rectangle(200, 200, 400, 400));
    
            TabbedPane.setBackground(new java.awt.Color(204, 204, 255));
    
            tossHeadingLbl.setFont(new java.awt.Font("Arial", 1, 14));
            tossHeadingLbl.setText("Coin Toss:");
            tossHeadingLbl.setAlignmentX(1.0F);
            tossHeadingLbl.setAlignmentY(1.0F);
            tossHeadingLbl.setAutoscrolls(true);
    
            jSeparator3.setAlignmentY(0.1F);
    
            pReportLbl.setFont(new java.awt.Font("MS Reference Sans Serif", 0, 12));
            pReportLbl.setForeground(new java.awt.Color(0, 0, 102));
            pReportLbl.setText("The pitch is even with good bounce");
    
            selectBtn.setFont(new java.awt.Font("Arial", 1, 12));
            selectBtn.setForeground(new java.awt.Color(0, 0, 102));
            selectBtn.setText("Proceed");
    
            prHeadingLbl1.setFont(new java.awt.Font("Arial", 1, 14));
            prHeadingLbl1.setText("Pitch Report:");
            prHeadingLbl1.setAlignmentX(1.0F);
            prHeadingLbl1.setAlignmentY(1.0F);
            prHeadingLbl1.setAutoscrolls(true);
    
            selHeadingLbl.setFont(new java.awt.Font("Arial", 1, 14));
            selHeadingLbl.setText("Team Selection");
            selHeadingLbl.setAlignmentX(1.0F);
            selHeadingLbl.setAlignmentY(1.0F);
            selHeadingLbl.setAutoscrolls(true);
    
            toggleCoin.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
            toggleCoin.setForeground(new java.awt.Color(0, 0, 102));
            toggleCoin.setText("Heads");
    
            coin.setBackground(new java.awt.Color(204, 204, 204));
            coin.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
            coin.setForeground(new java.awt.Color(0, 0, 102));
    
            javax.swing.GroupLayout ctPanelLayout = new javax.swing.GroupLayout(ctPanel);
            ctPanel.setLayout(ctPanelLayout);
            ctPanelLayout.setHorizontalGroup(
                ctPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(ctPanelLayout.createSequentialGroup()
                    .addGroup(ctPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(ctPanelLayout.createSequentialGroup()
                            .addGap(20, 20, 20)
                            .addGroup(ctPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(prHeadingLbl1)
                                .addComponent(pReportLbl))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 99, Short.MAX_VALUE))
                        .addGroup(ctPanelLayout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(ctPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
                                .addGroup(ctPanelLayout.createSequentialGroup()
                                    .addGap(10, 10, 10)
                                    .addGroup(ctPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(selectBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(selHeadingLbl)))))
                        .addGroup(ctPanelLayout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(ctPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jSeparator4, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
                                .addGroup(ctPanelLayout.createSequentialGroup()
                                    .addGap(10, 10, 10)
                                    .addGroup(ctPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(ctPanelLayout.createSequentialGroup()
                                            .addComponent(toggleCoin)
                                            .addGap(52, 52, 52)
                                            .addComponent(coin, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(tossHeadingLbl))
                                    .addGap(88, 88, 88)))))
                    .addContainerGap())
                .addGroup(ctPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(ctPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
                        .addContainerGap()))
            );
            ctPanelLayout.setVerticalGroup(
                ctPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(ctPanelLayout.createSequentialGroup()
                    .addGap(44, 44, 44)
                    .addComponent(prHeadingLbl1)
                    .addGap(18, 18, 18)
                    .addComponent(pReportLbl)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(selHeadingLbl)
                    .addGap(20, 20, 20)
                    .addComponent(selectBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(ctPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(ctPanelLayout.createSequentialGroup()
                            .addComponent(tossHeadingLbl)
                            .addGap(27, 27, 27)
                            .addComponent(toggleCoin, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(coin, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(89, Short.MAX_VALUE))
                .addGroup(ctPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(ctPanelLayout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(360, Short.MAX_VALUE)))
            );
    
            TabbedPane.addTab("Coin Toss", ctPanel);
    
            javax.swing.GroupLayout logPanelLayout = new javax.swing.GroupLayout(logPanel);
            logPanel.setLayout(logPanelLayout);
            logPanelLayout.setHorizontalGroup(
                logPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(logPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
                    .addContainerGap())
            );
            logPanelLayout.setVerticalGroup(
                logPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(logPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(382, Short.MAX_VALUE))
            );
    
            TabbedPane.addTab("Log Standings", logPanel);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(TabbedPane)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(TabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new StartUpFrame().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JTabbedPane TabbedPane;
        private javax.swing.JButton coin;
        private javax.swing.JPanel ctPanel;
        private javax.swing.JSeparator jSeparator1;
        private javax.swing.JSeparator jSeparator2;
        private javax.swing.JSeparator jSeparator3;
        private javax.swing.JSeparator jSeparator4;
        private javax.swing.JPanel logPanel;
        private javax.swing.JLabel pReportLbl;
        private javax.swing.JLabel prHeadingLbl1;
        private javax.swing.JLabel selHeadingLbl;
        private javax.swing.JButton selectBtn;
        private javax.swing.JToggleButton toggleCoin;
        private javax.swing.JLabel tossHeadingLbl;
        // End of variables declaration
    
    
    
        public void actionPerformed(ActionEvent e)
        {
    
    
            if(e.getSource()==toggleCoin)
    		        {
    		            if(toggleCoin.getText().compareTo("Heads")==0)
    		                toggleCoin.setText("Tails");
    		            else
    		                toggleCoin.setText("Heads");
    
    		            tmr.start();
            }
    
            if(e.getSource()==tmr)
            {
                JOptionPane.showMessageDialog(null,"Timer test");
                tmr.stop();
    
            }
    
        }
    
    
    }
    Sorry, I only speak machine language. Yes or a No?:confused:

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by BiteMuncher View Post
    Strange, the below SSCCE tested correctly in a new Textpad document but fails in NetBeans. Any reason for that?
    Click 'Heads' button on JFrame.
    Please define "fails". Please give as much information about the problem as possible. Error messages? Exceptions? Which line(s)? etc...

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, 90% of your code is unnecessary to show your problem and are distracting to us. This will slow down our ability to analyze your code and may decrease your chances of getting decent help. Again, please read the SSCCE definition for why this matters and how to avoid this.

    For example here's a small bit of code that shows use of a Swing Timer started on button press that's much shorter than yours:

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class StartUpFrame2 {
    
       protected static final int TIMER_DELAY = 1000;
       private static final int COUNT_MAX = 10;
    
       private static void createAndShowUI() {
          final JTextArea tArea = new JTextArea(20, 20);
          tArea.setFocusable(false);
          
          final JButton coinTossButton = new JButton("Coin Toss");
          coinTossButton.addActionListener(new ActionListener() {
             
             public void actionPerformed(ActionEvent e) {
                coinTossButton.setEnabled(false);
                new Timer(TIMER_DELAY, new ActionListener() {
                   int count = 0;
                   public void actionPerformed(ActionEvent e) {
                      if (count >= COUNT_MAX) {
                         ((Timer)e.getSource()).stop();
                         coinTossButton.setEnabled(true);
                      }
                      tArea.append("Count: " + count + "\n");
                      count++;
                   }
                }).start(); 
             }
          });
    
          JPanel panel = new JPanel();
          panel.add(coinTossButton);
          panel.add(new JScrollPane(tArea));
          
          JFrame frame = new JFrame("StartUpFrame2");
          frame.getContentPane().add(panel);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    Last edited by Fubarable; 04-19-2011 at 11:18 PM.

  12. #12
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default

    I think I found the problem but I still don't understand why it occurs.
    The above form was created by 'new File' > Swing GUI Forms > JFrame Form
    If I create a regular new class: 'new File' > Java > Java Class, and paste the code from the first form, it works.
    Why is that?
    Sorry, I only speak machine language. Yes or a No?:confused:

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by BiteMuncher View Post
    I think I found the problem but I still don't understand why it occurs.
    The above form was created by 'new File' > Swing GUI Forms > JFrame Form
    If I create a regular new class: 'new File' > Java > Java Class, and paste the code from the first form, it works.
    Why is that?
    You still haven't told us what the current problem is.:confused::confused:
    You still haven't indicated which line(s) of code are causing the problems. :confused::confused:

  14. #14
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    We don't use IDE's to generate code. We like to be in control of the code that is created so we know what the code does. I don't know what your IDE does nor do I care because I would rather spend time learning Java instead of the IDE. Then my knowledge will work in any environment because I'm not dependent on the IDE.

Similar Threads

  1. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-04-2010, 11:15 PM
  2. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  3. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  4. Replies: 4
    Last Post: 04-22-2009, 11:02 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •