#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Waukesha, Wisconsin
    Posts
    131
    Rep Power
    14

    Occasional Error


    Every once and a while when I first start the following program, I get the following error:

    Code:
    Exception in thread "Thread-2" java.lang.NullPointerException
            at ConvTables_GUI$getTime.run(ConvTables_GUI.java:125)
    I get by the error by exiting out of the program and go back into it and it works fine. Is there anthing we can do to prevent the error?

    Here is the code:

    Code:
    import java.awt.*; 
    import java.awt.event.*;
    import javax.swing.*; 
    import java.io.*;
    import java.util.*;
    
    public class ConvTables_GUI extends JFrame implements ActionListener
     { 
         String Current_Date = getDate(); 
         String Current_Time;
         getTime a1 = new getTime(); 
    
         JLabel Date = new JLabel(Current_Date); 	  
         JLabel Time = new JLabel(Current_Time);
    
         JMenuItem OptionA = new JMenuItem("Linear Measure");
         JMenuItem OptionB = new JMenuItem("Area"); 
         JMenuItem OptionC = new JMenuItem("Temperature"); 
         JMenuItem OptionD = new JMenuItem("Weight"); 
         JMenuItem OptionE = new JMenuItem("Volume and Liquid"); 
         JMenuItem OptionF = new JMenuItem("Speed"); 
         JMenu ConvT;
    
         public ConvTables_GUI()
          { 
    	 setLocation(50, 90);
             setSize(500, 600); 
    
    	 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	 ConvT = new JMenu("Conversion Tables " +
                               "                                             " +
                               Current_Date +
                               "                                              " +
                               Current_Time); 
    
    	 ConvT.add(OptionA);
    	 ConvT.add(OptionB);
    	 ConvT.add(OptionC);
    	 ConvT.add(OptionD);
     	 ConvT.add(OptionE);
    	 ConvT.add(OptionF);
    
    	 /* Add listeners to menu items */
    	 OptionA.addActionListener(this);
    	 OptionB.addActionListener(this);
    	 OptionC.addActionListener(this);
    	 OptionD.addActionListener(this);
    	 OptionE.addActionListener(this);
    	 OptionF.addActionListener(this);
    
     	 JMenuBar JMB = new JMenuBar();
             JMB.add(ConvT);
    	 setJMenuBar(JMB);
    	 setVisible(true);
          }
     
    	String getDate()
             {
    	  String Date;
    	  /* get current Date  */
              Calendar now = Calendar.getInstance();
              int month = now.get(Calendar.MONTH) + 1;
              int day = now.get(Calendar.DAY_OF_MONTH);
              int year = now.get(Calendar.YEAR);
    
              Date = month + "/" + day + "/" + year;
              return Date;
             } 
    
    	public class getTime extends Thread
    	 {
    	   private boolean keepGoing;
    	   String Time;
    
    	   public getTime()
    	    {
          	      keepGoing = true;
    	      start();
       	    }
    
    	   private void pause(double seconds)
    	    {
          	      try
    	       {
             	 Thread.sleep((int)(seconds*1000));
          	       } catch (InterruptedException ie)
    		 {
          		 }
    	    }
    
    	   public void run()
                {
          	       while (keepGoing)
    	        {
    	
                       Calendar c = Calendar.getInstance();
    
                       int intHour = c.get(Calendar.HOUR_OF_DAY);
    		   int intMinute = c.get(Calendar.MINUTE);
                       int intSecond = c.get(Calendar.SECOND);
    
             	   String hour = format(intHour);
                       String minute = format(intMinute);
                       String second = format(intSecond);
    
    	           Time = hour + ":" + minute + ":" + second;
    	           ConvT.setText("Conversion Tables " +
                                     "                                             " +
                                     Current_Date +
                                     "                                              " +
                                     Time); 
    	      
                       pause(1.0);
    
    	        }
                }
    
    	/* Verifies the size of an int and adds a leading 0 if */
    	/*  it is smaller than two characters (10).            */
                 String format(int i1)
    	      {
                    if (i1 < 10)
                     {
                       String s2 = "0" + i1;
                       return s2;
                     }
                       return "" + i1;
                     }
    
    	 }
    
         public void actionPerformed(ActionEvent e) 
          {
    	 try
    	  {
    	     if (e.getActionCommand().equals("Linear Measure"))
    	        {
    	           Linear_Measure_Menu_GUI menu = new Linear_Measure_Menu_GUI();
    	        }
                 if (e.getActionCommand().equals("Area"))
    	        {
                       Area_Menu_GUI menu = new Area_Menu_GUI();
    	        }
                 if (e.getActionCommand().equals("Temperature"))
    	        {
                       Temperature_Menu_GUI menu = new Temperature_Menu_GUI();
    	        }
                 if (e.getActionCommand().equals("Weight"))
    	        {
                       Weight_Menu_GUI menu = new Weight_Menu_GUI();
    	        }
    	     if (e.getActionCommand().equals("Volume and Liquid"))
    	        {
                       Volume_and_Liquid_Menu_GUI menu = new Volume_and_Liquid_Menu_GUI();
    	        }
    	     if (e.getActionCommand().equals("Speed"))
    	        {
                       Speed_Menu_GUI menu = new Speed_Menu_GUI(); 
    	        }
    	     }catch(IOException ie)
    	      {
    	      }
          }
    
    
         public static void main(String[] arguments)
          { 
             ConvTables_GUI frame = new ConvTables_GUI();
          } 
    
     }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,040
    Rep Power
    1289
    I believe the problem has to do with thread scheduling. There is no way to know when a particular thread will execute. You spin off the Thread getTime before you assign a value to ConvT. So probably what happened was that the code in the Thread that set the text of ConvT was called before ConvT was assigned to a JMenu.

    I would recommend putting an if statement in the run method

    Code:
    if (ConvT != null)
       ConvT.setText(xxx);

IMN logo majestic logo threadwatch logo seochat tools logo