Uderc programming article aggregator sites

J2ME problem with threads

Tags:
Answers: Have 6 answers
Advertisement
Hi all,
I would like to ask you for a help. I need to write a small program at my university. I started to write a midlet which function would be to countdown time for sports activities. I woul like to start a new thread - the one that counts down - and at the same time make the main thread sleep. After the "countdown" thread finishes, the main thread wakes up and waits for user input. The problem is that when the "countdown" thread finishes his work, I've got Uncaught exception java/lang/NullPointerException. error and the midlet halts.
Below you can find the code
import java.lang.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class intervals extends MIDlet implements CommandListener
public Display ekran;
private SweepCanvas sweeper;
private Form rundy;
private TextField round0, round1, round2, round3, round4, round5, round6, round7, round8;
private long czas,x;
private Command exitCommand;
private Command addRound;
private Command delRound;
private Command start;
private TextField repeat;
private Form odliczanie;
private Alert ostrz;
Licznik thread;
String test;
StringItem test1;
int parz,i,j,k;
static int l;
int ilrund;
int ilpowt;
Item sec;
long sec1;
public intervals()
    rundy = new Form("Interwały sportowe");
    exitCommand = new Command("Wyjście", Command.EXIT, 2);
    addRound = new Command("Dodaj","Dodaj rundę", Command.ITEM,1);
    delRound = new Command("Usuń","Usuń ostatnią rundę", Command.ITEM,1);
    start = new Command("Start", Command.ITEM,1);
    odliczanie = new Form("Odliczanie");
    TextField dodaj(TextField kolej)
        kolej=new TextField("Podaj czas (s) rundy "+parz,null, 4, TextField.NUMERIC);//stworzenie nowej instancji do wybierania czasu trwania rundy
        if(rundy.size()==0)
            rundy.insert(rundy.size(),kolej);
            else
                rundy.insert(rundy.size()-1, kolej);
        return kolej;
    void odliczanie(TextField round)
        monitor m=new monitor();
        k=Integer.parseInt(round.getString());
        ekran.setCurrent(odliczanie);
        thread=new Licznik(k,odliczanie);
        thread.start();
        ekran.setCurrent(rundy);
public void startApp()// throws MIDletStateChangeException
    rundy.deleteAll();
    repeat = new TextField("Podaj ilość powtórzeń",null,1,TextField.NUMERIC);
    rundy.addCommand(addRound);
    rundy.addCommand(exitCommand);
    rundy.setCommandListener(this);
    Canvas obrazek = new MyCanvas();
    ekran = Display.getDisplay(this);
    ekran.setCurrent(obrazek);
    czas=System.currentTimeMillis();
    while (System.currentTimeMillis()<czas+1000)
        continue;
    ekran.setCurrent(rundy);
public void pauseApp()
public void destroyApp(boolean unconditional)
    notifyDestroyed();
public void commandAction(Command c, Displayable s)
    if (c == exitCommand)
        destroyApp(false);
        notifyDestroyed();
    else if(c==addRound)
        if(rundy.size()==0)//Sprawdzenie ilo&#347;ci elementów w celu poprawnego wy&#347;wietlania liczby rund w formie
            parz=1;
            else
            parz=rundy.size();
        switch(parz)
            case 1:
                round0=dodaj(round0);break;
            case 2:
                round1=dodaj(round1);break;
            case 3:
               round2= dodaj(round2);break;
            case 4:
                round3=dodaj(round3);break;
            case 5:
                round4=dodaj(round4);break;
            default:
                ostrz=new Alert("Uwaga","Maksymalna liczba rund wynosi 9", null, AlertType.INFO);
                ostrz.setTimeout(3000);
                ekran.setCurrent(ostrz);
        if(rundy.size()==1)
            rundy.append(repeat);
            rundy.addCommand(start);
        rundy.addCommand(delRound);
    else if(c==delRound)
        if(rundy.size()!=0)
            rundy.delete(rundy.size()-2);
            if (rundy.size()==1)
                rundy.deleteAll();
            if(rundy.size()==0)
                rundy.removeCommand(delRound);
                rundy.removeCommand(start);
    else if(c==start)
        ilrund=rundy.size()-1;
        if(this.repeat.size()>0)
            ilpowt=Integer.parseInt(this.repeat.getString());
        ekran = Display.getDisplay(this);
        for (i=1; i<=ilpowt;i++)
            odliczanie= new Form("Odliczanie");
             for (j=0;j<ilrund;j++)
                switch(j)
                     case 0:
                         odliczanie(round0);
                         break;
                     case 1:
                         odliczanie(round1);
                         break;
                     case 2:
                         odliczanie(round2);
                         break;
                     case 3:
                         odliczanie(round3);
                         break;
                     case 4:
                         odliczanie(round4);
                         break;
                     case 5:
                         odliczanie(round5);
                         break;
                     case 6:
                         odliczanie(round6);
                         break;
                     case 7:
                         odliczanie(round7);
                         break;
                     case 8:
                         odliczanie(round8);
                         break;
class Licznik extends Thread
    int czas1,k;
    Form forma;
    monitor m;
    public Licznik(int k,Form formap)
        czas1=k;
        forma=formap;
    public synchronized void run()
        while(czas1>0)
            forma.deleteAll();
            forma.append("Czas pozosta&#322;y (s): "+czas1);
            try{Thread.sleep(1000);} catch(InterruptedException e){e.printStackTrace();}
            czas1--;
        if(czas1<=0)
            m.put();
    }and monitor class
public class monitor
boolean busy=false;
synchronized void get()
    if(!busy)
        try
            wait();
        }catch(InterruptedException e){e.printStackTrace();}
    notify();
synchronized void put()
    if(busy)
        try
        wait();
        }catch(InterruptedException e){e.printStackTrace();}
    busy=true;
    notify();
}Can anybody help me with this?
Advertisement
The best answer: Groovemaker,
Your Licznik class has a member m of type monitor, which has not been instantiated (in other words is null) hence, when calling m.put() you get NullPointerException. Please also mind, that using Thread.sleep(1000) is not an accurate way of measuring time.
If I may, please use recommended for Java class naming conventions - some of your names use lower case, while other don't which is confusing to the reader.
Daniel