views:

120

answers:

6

Actually i am developing an app for blackberry.Its like a test,that consists of 10 question,& at the end of the test,result is being displayed.Nw i want to add a timer to my application,so that when the user is taking a test,timer count will b there.i hav written one code,bt it is throwing null pointer exception,kindly help

Timer t=new Timer();

        TimerTask tt=new TimerTask() {

        public void run() {

            UiApplication.getUiApplication();
            synchronized (Application.getEventLock()) {

         currentTime = System.currentTimeMillis();
         long diff = currentTime - startTime;
         long min = diff / 60000;
         long sec = (diff % 60000) / 1000;
         String minStr = new Long(min).toString();
         String secStr = new Long(sec).toString();


         if (min < 10)
         minStr = "0" + minStr;
         if (sec < 10)
          secStr = "0" + secStr;

         TimerCount= new LabelField(12 + ":" + 14){
            protected void paint(Graphics g) {
                g.setColor(Color.WHITE);
                super.paint(g);
            }
         };


        TimerCount.setFont(myFont(16));

        add(TimerCount);
            }
        }

        };
        t.scheduleAtFixedRate(tt, 1000, 1000);
A: 

You could use either a java.util.Timer and a java.util.TimerTask. To update the display you will have to hold the event lock:

synchronize (UiApplication.getUiApplication().getEventLock()) {
   // update display
}

Or you could use the net.rim.device.api.system.RealTimeClockListener.

You should be able to develop the rest of the code by reading the documentation for the appropriate classes.

Richard
BTW you have nine other questions with answers. I'm sure one of them must be worth accepting.
Richard
@Richard Can u plz write me a sample code kindly.....
Arunabha Dutta Choudhury
@Richard Can u kindly write dwn a sample code for me?
Arunabha Dutta Choudhury
A: 

My code is here

Timer t=new Timer();

        TimerTask tt=new TimerTask() {

        public void run() {



            synchronized (Application.getEventLock()) {
         currentTime = System.currentTimeMillis();
         System.out.println("Time is..."+ currentTime);
         startTime   = System.currentTimeMillis();
         System.out.println("Time is..."+ startTime);
          diff = currentTime - startTime;
        System.out.println("Time is..."+ diff);
             min = (currentTime/ 60000);
        System.out.println("Min is..."+ min);
          sec = (currentTime % 60000) / 1000;
        System.out.println("Sec is..."+ sec);
          minStr = new Long(min).toString();
        System.out.println("MinString is..."+ minStr);
          secStr = new Long(sec).toString();
        System.out.println("SecString is..."+ secStr);
         if (min < 10){
         minStr = 0 + minStr;
         }
         if (sec < 10){
          secStr = 0 + secStr;

            }
  TimerCount= new LabelField(minStr + ":" + secStr){

            protected void paint(Graphics g) {
                g.setColor(Color.WHITE);
                super.paint(g);
            }
         };


        TimerCount.setFont(myFont(16));

        add(TimerCount);

            }

        }

        };
        t.scheduleAtFixedRate(tt,1000, 1000);
Arunabha Dutta Choudhury
plz anyone help me.
Arunabha Dutta Choudhury
Edit your question and post it there, not as an answer.
Michael B.
You seriously need to read the faq. Don't create new answers when all you need to do is edit your question.
Will
A: 

Here is a working sample code, based on what you have already done so far :

public class AppScreen extends MainScreen {

 private LabelField timerCount;

 public AppScreen() {
  timerCount = new LabelField(12 + ":" + 14);
  this.add(timerCount);
  Timer t = new Timer();

  TimerTask tt = new TimerTask() {
   long currentTime;
   long startTime = System.currentTimeMillis();
   public void run() {
    currentTime = System.currentTimeMillis();
    long diff = currentTime - startTime;
    long min = diff / 60000;
    long sec = (diff % 60000) / 1000;
    String minStr = new Long(min).toString();
    String secStr = new Long(sec).toString();

    if (min < 10)
     minStr = "0" + minStr;
    if (sec < 10)
     secStr = "0" + secStr;

    synchronized (UiApplication.getEventLock()) {
     timerCount.setText(String.valueOf(minStr + ":" + secStr));
    }
   }
  };
  t.scheduleAtFixedRate(tt, 1000, 1000);
 }
}
Michael B.
You may want to use a DateField with style property Field.READONLY and a SimpleDateFormat("hh:mm") instead of the LabelField, which will save quite a bit of code.
Richard
@Richard like I said, I used his code, I didn't bother simplyfing the Time stuff.
Michael B.
@Micheal B No cirticizim intended. Just another option the OP should look into. I was going to post some code but you beat me to it. It is always difficult to give sample code to broad questions. That's why I usually stay away from the "can you write my program for me" kind of questions. But that's just my personal choice.
Richard
Arunabha Dutta Choudhury
@Arunabha Dutta Choudhury do whatever you want with the code, yes you can use static if you need to do so, or Singleton Class.
Michael B.
Arunabha Dutta Choudhury
A: 

Thanx everyone for your help,its working now,but the problem which i am facing now is that "Actually when the first Screen is displayed,a question is there & four answer to choose from.Here at the top the timer is being displayed,& its continously incrementing(which is what i want)but as soon as i choose a answer,the control goes to another page,where the reason for the answer is being displayed & some message is displayed,in that page also we are calling the page which has the timer code TopManger a=new TopManager().So what is happening now is that,everytime i select an answer,& it goes to the answer screen,the timer again gets resets to 00:00,which i dont want,i want the time to keep on incrementing,even when the answer screen is dispalyed.

Arunabha Dutta Choudhury
A: 

In my application,at the top,a timer is being displayed which keeps on increasing,the First screen of my app shows one question with four answer.So whenever i click one answer,it goes to the next screen,& the timer is reset to 00:00.Actually the control is going from my current page to another page,where we r calling the class that contains the code for timer again,ie Topmanager t=new Topmanager();,but i want the timer not to being reset to zero,but rather keep on increasing,even when we move from one screen to another.I tried to make the label field TimerCount as static,but it doesnt helped much,can anyone hlpe me,here is the code

                     Timer t=new Timer();
         TimerTask tt=new TimerTask() {
        long startTime = System.currentTimeMillis();
         public void run() {

        currentTime = System.currentTimeMillis();
        diff = currentTime - startTime;
       min = ( diff/ 60000);
         sec = ( diff%60000) / 1000;
         minStr = new Long(min).toString();
      secStr = new Long(sec).toString();
        if (min < 10){
         minStr = "0" + minStr;
        }
       if (sec < 10){
        secStr = "0" + secStr;
     }
    synchronized (Application.getEventLock()) {
            TimerCount.setText(String.valueOf(minStr + ":" + secStr));
           }
         }

        };
        t.scheduleAtFixedRate(tt,1000, 1000);

        TimerCount = new LabelField("00" + ":" + "00"){
            protected void paint(Graphics g) {
            g.setColor(Color.WHITE);
            super.paint(g);
             }
           };                 
          TimerCount.setFont(myFont(16));
          add(TimerCount);
Arunabha Dutta Choudhury
A: 

In my application,one timer is dispayed at the top of the screen.The code is written in a class called TopManger,it displays a timer in 00:00,which keeps on incrementing.Here is the code

Timer t=new Timer(); TimerTask tt=new TimerTask() { long startTime = System.currentTimeMillis(); public void run() {

        currentTime = System.currentTimeMillis();
        diff = currentTime - startTime;
       min = ( diff/ 60000);
         sec = ( diff%60000) / 1000;
         minStr = new Long(min).toString();
         System.out.println("abccccccc"+minStr);
      secStr = new Long(sec).toString();
      System.out.println("xyzzzzzzzzz"+secStr);
        if (min < 10){
         minStr = "0" + minStr;
        }
       if (sec < 10){
        secStr = "0" + secStr;
     }
    synchronized (Application.getEventLock()) {
            TimerCount.setText(String.valueOf(minStr + ":" + secStr));

           }
         }

        };
        t.scheduleAtFixedRate(tt,1, 1);
        TimerCount = new LabelField("00" + ":" + "00"){
            protected void paint(Graphics g) {
            g.setColor(Color.WHITE);
            super.paint(g);
             }
           };                 
          TimerCount.setFont(myFont(16));
          add(TimerCount);

So in the home screen one question is being displayed with four answers to choose from.Nw when we click a particular answer,then another screen is being displayed,but the timer gets reset to 00::00,& start to increment from fresh,I want it not to be reset,when the screen changes,it should keep on incrementing.Actually when we select a particular answer,the in the corresponding page we are calling Topmanger t=new TopManger() again,for this reason may be everytime it goes to answer screen,it starts frm fresh & gets reset to 00:00,i tried to make it static,but didnt work,can any help me ocvercome this problem.

Arunabha Dutta Choudhury