Current Position:Home > J2ME problem with threads

J2ME problem with threads

Update:10-11Source: network consolidation
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?

The Best Answer

Advertisement
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
  • J2ME problem with threads Update:10-11

    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 -

  • Problem with Thread and InputStream Update:10-11

    Hi, I am having a problem with threads and InputStreams. I have a class which extends Thread. I have created and started four instances of this class. But only one instance finishes its' work. When I check the state of other three threads their state

  • Problem with threads in j2me Update:10-11

    im using jdk 1.6 with java wtk 2.1 ya i know.. 2.1 is a jdk1.4 source but im using souce as 1.4 during compiliation so thats not the prob this is my code package org.learn; import javax.microedition.midlet.MIDlet; import javax.microedition.lcdui.Aler

  • Problem in thread creation Update:11-30

    Hello Frnds, i am learning j2me..i am facing some problem in simple thread initilization.. the code is import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.io.*; import javax.microedition.io.*; class Process implements R

  • A problem with threads Update:10-11

    I am trying to implement some kind of a server listening for requests. The listener part of the app, is a daemon thread that listens for connections and instantiates a handling daemon thread once it gets some. However, my problem is that i must be ab

  • A problem with Threads and MMapi Update:10-11

    I am tring to execute a class based on Game canvas. The problem begin when I try to Play both a MIDI tone and to run an infinit Thread loop. The MIDI tone "Stammers". How to over come the problem? Thanks in advance Kobi See Code example below: i

  • Problem with threads within applet Update:10-11

    Hello, I got an applet, inside this applet I have a singleton, inside this singleton I have a thread. this thread is running in endless loop. he is doing something and go to sleep on and on. the problem is, when I refresh my IE6 browser I see more th

  • Problem with Threads and a static variable Update:10-11

    I have a problem with the code below. I am yet to make sure that I understand the problem. Correct me if I am wrong please. Code functionality: A timer calls SetState every second. It sets the state and sets boolean variable "changed" to true. T

  • Problem with threads running javaw Update:10-11

    Hi, Having a problem with multi thread programming using client server sockets. The program works find when starting the the application in a console using java muti.java , but when using javaw multi.java the program doesnt die and have to kill it in

  • Problem with Threads and "plase wait..."-Window Update:10-11

    Hi everyone, I have a problem that I'm not able to solve in any way... I have a time-consuming task (a file decryption) which I execute in a separate thread; I've used the SwingWorker class, like suggested by sun-tutorial, and it works right. The pro