Results 1 to 3 of 3

Thread: java.lang.NullPointerException

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Angry java.lang.NullPointerException

    Arghhhhh. Can anyone see what i am doing wrong. The static format method takes a String and an Object[] as arguements. public static String format(String pattern, Object[] arguements) I have to create a Long object and put that into an Object array to pass to the format method but i keep getting a NullPointerException.
    Any ideas why?

    Sun should have just overloaded the format method to take various datatypes instead of an Object[] array.
    Code:
    class TimeTracker extends TimerTask{
       private Timer timer;
       private Long l;
       private TimeTrackerTest t;
       private long millis;
       Object[] o; 
    
      public TimeTracker(JTextField jtf){
       this.t = t;
       o = new Object[1];
       timer = new Timer(true); 
     }
    
     public void run(){
       try{
         millis = scheduledExecutionTime();
         l = new Long(millis); 
         o[0] = l; 
      
         t.jtf.setText(MessageFormat.format("{0}",o));
         }catch(Exception e){System.err.println(e);}
      }
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    That's not the problem. The NullPointerException is thrown because you try to call a method of t, which is uninitialized:
    t.jtf.setText(...

    You think you're initializing it here:
    this.t = t;
    but you aren't, there is no t.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    yup that was to problem. I was passing the wrong thing into the constructor. Must have been tired. At least im getting a clean compile now. Thanks for the help.
    Code:
    class TimeTracker extends TimerTask{
       private Timer timer;
       private Long l;
       private TimeTrackerTest t;
       private long millis;
       Object[] o; 
    
      public TimeTracker(TimeTrackerTest t){
       this.t = t;
       o = new Object[1];
       timer = new Timer(true); 
     }
    
     public void run(){
       try{
         millis = scheduledExecutionTime();
         l = new Long(millis); 
         o[0] = l; 
      
         t.jtf.setText(MessageFormat.format("{0}",o));
         }catch(Exception e){System.err.println(e);}
      }
    }

Posting Permissions

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



Click Here to Expand Forum to Full Width