Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: TimerFun

  1. #1
    Junior Member KTJW88's Avatar
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default TimerFun

    Error Message when I run a program

    Exception in thread "main" java.lang.NullPointerException
    	at TimerFun.GUIMaker(TimerFun.java:86)
    	at TimerFun.<init>(TimerFun.java:226)
    	at TimerFun.main(TimerFun.java:231)

    MY java code

    import java.awt.*; 
    import java.awt.event.*;  
    import javax.swing.*; 
    import java.io.*;
    import javax.swing.Timer.*; 
    import java.util.*; 
    import java.text.SimpleDateFormat; 
     
    public class TimerFun extends Thread implements ActionListener 
    {
       //Data Types (Attibrutes)
       private JPanel jp1, jp2, jp3, jp4, jp5,
                      jp6, jp7, jp8, rainbowColor,
                      pbjp;
       private JLabel jlDate; 
       private JFrame gui = new JFrame("Kevin T. Whetstone's FUN with Timers & Threads");
       private JProgressBar jpb = new JProgressBar();  
     
       //GUI maker to make coding easier
       public void GUIMaker()
       {
          //JFrame objects
          gui.setLayout(new BorderLayout());
          gui.setSize(350,350);
          gui.setLocationRelativeTo(null);
          gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          gui.setVisible(true);
     
          //JMenuBar objects
          JMenuBar jmb = new JMenuBar(); 
          JMenu jmFile = new JMenu("File");
          JMenu jmHelp = new JMenu("Help");
          JMenuItem jmiExit = new JMenuItem("Exit");
          JMenuItem jmiAbout = new JMenuItem("About");
     
          //adding JMenuBar objects to the JFrame, gui
          jmFile.add(jmiExit); 
          jmHelp.add(jmiAbout); 
          jmb.add(jmFile); 
          jmb.add(jmHelp); 
          gui.setJMenuBar(jmb);
     
          //Mnemonic objects
          jmFile.setMnemonic(KeyEvent.VK_F);
          jmHelp.setMnemonic(KeyEvent.VK_H);
          jmiExit.setMnemonic(KeyEvent.VK_X);
          jmiAbout.setMnemonic(KeyEvent.VK_A);
     
          //Adding ActionListener
          jmiExit.addActionListener(this); 
          jmiAbout.addActionListener(this);
     
          //Date & Time
          jlDate = new JLabel();
          Time t1 = new Time(jlDate);
          java.util.Timer t2 = new java.util.Timer();
          t2.scheduleAtFixedRate(new Time(jlDate), 10, 10);
          gui.add(jlDate, BorderLayout.NORTH);
     
          //Adding Progress Bars 
          pbjp = new JPanel();
          jpb = new JProgressBar();
    	   pbjp.add(new JLabel("Progress Bar: "));
    		progressBar pbInner = new progressBar();
    		pbjp.add(jpb);
    		jpb.setOrientation(JProgressBar.HORIZONTAL);	
    		jpb.setMinimum(0);										
    		jpb.setMaximum(100);									
    		jpb.setValue(0);										
    		jpb.setStringPainted( true );		
          jpb.setIndeterminate(false);				
    		gui.add(pbjp, BorderLayout.SOUTH);
          jpb.setString("Three seconds  before starting" );
          try
          { 
             Thread.sleep(3000); 
          }
    		catch(InterruptedException ie)
          { 	
             ie.printStackTrace();	
          }
    		   pbInner.start();
     
          //Rainbow Color & Timer Color    
          rainbowColor = new JPanel(new GridLayout(7,0));
          jp1.setBackground(Color.red); 
          rainbowColor.add(jp1); 
          jp2.setBackground(Color.orange); 
          rainbowColor.add(jp2);
          jp3.setBackground(Color.yellow); 
          rainbowColor.add(jp3);
          jp4.setBackground(Color.green); 
          rainbowColor.add(jp4);
          jp5.setBackground(Color.blue); 
          rainbowColor.add(jp5);
          jp6.setBackground(new Color(70,0,130)); 
          rainbowColor.add(jp6);
          jp7.setBackground(new Color(238,130,238)); 
          rainbowColor.add(jp7);
          gui.add(rainbowColor, BorderLayout.CENTER); 
          java.util.Timer c = new java.util.Timer(); 
          c.scheduleAtFixedRate(new ColorBar(jp1,jp2,jp3,jp4,jp5,jp6,jp7,jp8), 2000, 500); 
     
       }
     
       //Progress Bar with Thread
       protected class progressBar extends Thread 
       {
    		public void run() 
          {
    			jpb.setString(null);
    			jpb.setStringPainted(true);
    			jpb.setIndeterminate(false);
     
             int count = 0;
     
             for(count =0; count<=100; count++)
             {
    				jpb.setValue(count);
     
                try
                { 
                   Thread.sleep(50); 
                }
    				catch(InterruptedException ie) 
                {
    					ie.printStackTrace();
    				}
    			}
     
    			jpb.setStringPainted(true);
    			jpb.setString("Cylons Scanning");
     
    			try
             { 	
                Thread.sleep(3000); 
             }
    			catch(InterruptedException ie)
             {
    				ie.printStackTrace();
    			}
     			   jpb.setIndeterminate(true); 
     
    		}
     
    	}
     
       //ColorBars for background color with timer
       class ColorBar extends TimerTask
       {
          JPanel jp1, jp2, jp3, jp4,
                 jp5, jp6, jp7, jp8; 
     
          public ColorBar(JPanel jp1, JPanel jp2, JPanel jp3, JPanel jp4, JPanel jp5, JPanel jp6, JPanel jp7, JPanel jp8)
          {
             this.jp1 = jp1;
             this.jp2 = jp2;
             this.jp3 = jp3;
             this.jp4 = jp4;
             this.jp5 = jp5;
             this.jp6 = jp6;
             this.jp7 = jp7;
             this.jp8 = jp8;     
          } 
          public void run()
          {
             jp8.setBackground(jp1.getBackground());   
             jp1.setBackground(jp2.getBackground());
             jp2.setBackground(jp3.getBackground());  
             jp3.setBackground(jp4.getBackground());
             jp4.setBackground(jp5.getBackground());
             jp5.setBackground(jp6.getBackground());
             jp6.setBackground(jp7.getBackground());
             jp7.setBackground(jp8.getBackground()); 
          }     
       }
     
       //Time with Date, time (hour, min, second)
       class Time extends TimerTask
       {
          JLabel jlDate;
          SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
     
          public Time(JLabel jlDate)
          {
             this.jlDate = jlDate;
          }
     
          public void run()
          {
             Calendar curCal = Calendar.getInstance();
             Date curTime = curCal.getTime();
             jlDate.setText(sdf.format(curTime));
             jlDate.setFont(new Font("Arial", Font.BOLD, 20)); 
             jlDate.setForeground(Color.red);
             jlDate.setHorizontalAlignment(0);   
          } 
       }
     
       public void actionPerformed(ActionEvent e)
       {
          Object select = e.getActionCommand();
     
     
          if(select == "Exit")
          {
             try
             {
                Thread.sleep(2000); 
                System.exit(0);
             }
             catch(InterruptedException ie)
             {
                ie.printStackTrace();
      			}
          }
          else if(select == "About")
          {
             String stringFormat = String.format("Fun with Timers and Threads\nby: Kevin T. Whetstone\nFebruary 19, 2014");
             JOptionPane.showMessageDialog(null, stringFormat); 
          }
       }
     
       public TimerFun()
       {
          GUIMaker();
       }
     
       public static void main(String[] args)
       {
          new TimerFun();
       }
     
    }

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: TimerFun

    Welcome! Thank you for taking the time to learn to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    The JPanels, jp1, jp2, etc., are never initialized, so that when jp1 is first used on line 86 (look at that line as indicated by the error message/stack trace) it is null.

    You often hear the phrase "Swing is not thread safe." That means that using a Threads with Swing is not a good idea, and sleeping a thread in a Swing app is an excellent way to demonstrate why "Swing is not thread safe.".

  3. #3
    Junior Member KTJW88's Avatar
    Join Date
    Feb 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: TimerFun

    Awesome, and didn't know about it. It is working like a charm now.

    Thank you for your help,

    Kevin

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: TimerFun

    Glad you solved the problem.

    I believe the following:

    "Warning: Swing is not thread safe. For more information see Swing's Threading Policy."

    is on every javax.swing API page. You don't have to memorize the API of every Java SE class you use, but you should give each one a look, and when you see something interesting (like maybe that warning), you should dig a bit deeper.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    KTJW88 (March 1st, 2014)