itit 0 Newbie Poster

From the midlet class i am invoking a register() of Registration class. Registration implements Runnable. In register() i have created a new thread and called start(). register() returns a String value. In the run() method i have invoked the servlet that connects to the database.
The problem here is that register() returns before run() is invoked. So the cid value(value of cid is returned by register()) is set to null. After that run() is called and only then is the value of cid set. Hence a wrong message is given to the user.
is there any way of ensuring that register() does not return until run() is executed

This is my midlet

public class BMS extends MIDlet implements CommandListener
{
    private Display disp;
    private Form welcome,login,userCheck,workCheck,wrongUser,booking,sites;
    private Form registration,message;
    private Command cmdOk,cmdExit,cmdBack;
    private Ticker tckWelcome,tckMessage;
    private TextField uname,pwd,txtMessage,txtBid;
    private TextField txtName,txtCompany,txtContact;
    private TextField txtAddress,txtEmailid,txtReguname,txtRegpwd;
    private ChoiceGroup chUserType,chWork;
    private Alert altLoginFailed,altLastLogin,altRegistered,altNotEntered,altMessage;
    Registration reg;
    String cid;

    String userType[] = {"Registered","Not registered"};
    String work[] = {"Check status of your bookings","Book a billboard"};
    int loginCount = 0;

    public BMS()
    {
        welcome = new Form("Welcome");
        login = new Form("Login");
        userCheck = new Form("User Type");
        workCheck = new Form("Work");
        wrongUser = new Form("Wrong user!");
        registration = new Form("Registration Form");
        booking = new Form("Booking Form");
        message = new Form("Digital Message");
        cmdOk = new Command("OK",Command.OK,1);
        cmdExit = new Command("Exit",Command.EXIT,1);
        cmdBack = new Command("Back",Command.BACK,2);
        tckWelcome = new Ticker("WELCOME!!!");
        tckMessage = new Ticker("");
        uname = new TextField("Username : ","",20,TextField.ANY);
        pwd = new TextField("Password : ","",10,TextField.PASSWORD);
        txtName = new TextField("Name","",30,TextField.ANY);
        txtCompany = new TextField("Company","",30,TextField.ANY);
        txtAddress = new TextField("Address","",60,TextField.ANY);
        txtContact = new TextField("Contact No.","",10,TextField.PHONENUMBER);
        txtEmailid = new TextField("Email id","",20,TextField.ANY);
        txtReguname = new TextField("Username : ","",20,TextField.ANY);
        txtRegpwd = new TextField("Password : ","",10,TextField.PASSWORD);
        txtMessage = new TextField("Message","",60,TextField.ANY);
        txtBid = new TextField("Billboard id ","",2,TextField.NUMERIC);
        chUserType = new ChoiceGroup("Select user type ",Choice.EXCLUSIVE,userType,null);
        chWork = new ChoiceGroup("What do you wish to do?",Choice.EXCLUSIVE,work,null);
        altLoginFailed = new Alert("Login failed","Login failed!\nCheck username or password.",null,AlertType.ERROR);
        altLastLogin = new Alert("Last attempt","This is your last attempt to login. Please enter correctly",null,AlertType.WARNING);
        altRegistered = new Alert("Registered!","",null,AlertType.INFO);
        altNotEntered = new Alert("Error!","Enter all the fields",null,AlertType.ERROR);
        altMessage = new Alert("Message","",null,AlertType.INFO);
    }

    public void startApp()
    {        
        disp = Display.getDisplay(this);
        welcome.append("\nWelcome to BBSystems!\nWe will help you reach the masses anywhere in India");
        welcome.append("\nYou want to put up a poster or display a message, we can do it");
        welcome.append("\nMumbai, Delhi, Kolkata, Bangalore ....\nYou name it, you got it!");
        welcome.setTicker(tckWelcome);
        welcome.addCommand(cmdOk);
        welcome.addCommand(cmdExit);
        welcome.setCommandListener(this);
        userCheck.append(chUserType);
        userCheck.addCommand(cmdOk);
        userCheck.addCommand(cmdBack);
        userCheck.addCommand(cmdExit);
        userCheck.setCommandListener(this);        
        login.append(uname);
        login.append("");
        login.append(pwd);
        login.addCommand(cmdOk);
        login.addCommand(cmdBack);
        login.addCommand(cmdExit);
        login.setCommandListener(this);
        altLoginFailed.addCommand(cmdOk);
        altLoginFailed.setTimeout(Alert.FOREVER);
        altLoginFailed.setCommandListener(this);
        altLastLogin.addCommand(cmdOk);
        altLastLogin.setTimeout(Alert.FOREVER);
        altLastLogin.setCommandListener(this);
        wrongUser.append("You seem to have forgotten your username/password. Contact us at 022-23485710 or email to helpdesk@bbssytems.com");
        wrongUser.addCommand(cmdOk);
        wrongUser.setCommandListener(this);
        workCheck.append(chWork);
        workCheck.addCommand(cmdOk);        
        workCheck.addCommand(cmdExit);
        workCheck.setCommandListener(this);
        registration.append("Register yourself to get updates on the various billboard sites provided by us");
        registration.append(txtName);
        registration.append(txtCompany);
        registration.append(txtAddress);
        registration.append(txtContact);
        registration.append(txtEmailid);
        registration.append(txtReguname);        
        registration.append(txtRegpwd);
        registration.addCommand(cmdOk);
        registration.addCommand(cmdBack);
        registration.addCommand(cmdExit);
        registration.setCommandListener(this);
        altRegistered.addCommand(cmdOk);
        altRegistered.setTimeout(Alert.FOREVER);
        altRegistered.setCommandListener(this);
        altNotEntered.addCommand(cmdOk);
        altNotEntered.setTimeout(Alert.FOREVER);
        altNotEntered.setCommandListener(this);
        message.setTicker(tckMessage);
        message.append(txtMessage);
        message.append(txtBid);
        message.addCommand(cmdOk);
        message.addCommand(cmdBack);
        message.addCommand(cmdExit);
        message.setCommandListener(this);
        altMessage.setTimeout(Alert.FOREVER);
        altMessage.addCommand(cmdOk);
        altMessage.setCommandListener(this);
        //disp.setCurrent(welcome);
        disp.setCurrent(message);
    }

public void commandAction(Command c,Displayable d)
    {        
        boolean flag;
        switch(c.getCommandType())
        {
          case Command.OK:if(d == registration)
                            {
                                try
                                {
                                    reg = new Registration();
                                    //Thread t = new Thread(this);
                                    int notEntered;
                                    do
                                    {
                                        notEntered = 0;
                                        if(txtName.getString().equals("") || txtCompany.getString().equals("") || txtAddress.getString().equals(""))
                                            notEntered++;
                                        if(txtContact.getString().equals("") || txtEmailid.getString().equals("") || txtReguname.getString().equals("") || txtRegpwd.getString().equals(""))
                                            notEntered++;
                                        if(notEntered > 0)
                                            disp.setCurrent(altNotEntered);
                                    }
                                    while(notEntered > 0);
                                    cid = reg.register(txtName.getString(),txtCompany.getString(),txtAddress.getString(),txtContact.getString(),txtEmailid.getString(),txtReguname.getString(),txtRegpwd.getString());                                    
                                    System.out.println("cid is "+cid);
                                    String alertText = "You have been successfully registered. Your customer id is "+ cid + ".Please note it for further reference.";
                                    altRegistered.setString(alertText);
                                    disp.setCurrent(altRegistered);                                    
                                }
                                catch(Exception e)
                                {
                                    e.printStackTrace();
                                }
                        }
                  }
            }
}

This is the Registration class

public class Registration implements Runnable {
    //RecordStore r;
    ByteArrayOutputStream bos;
    DataOutputStream os;
    DataInputStream is;
    String name,company,address,email,uname,pwd;
    long contact;
    String cid;    
    

    public String register(String n,String c,String a,String cn,String e,String u,String p)
    {        
            name = n;
            company = c;
            address = a;
            contact = Long.parseLong(cn);
            email = e;
            uname = u;
            pwd = p;
            System.out.println("in register");
            Thread t = new Thread(this);
            t.start();
            return cid;
    }

    public void run()
    {
        System.out.println("in run");
        String url  = "http://localhost:8080/BMSDb/Registration";
        try
        {
            HttpConnection hc = (HttpConnection)(Connector.open(url));
            hc.setRequestProperty("User-Agent","Profile/MIDP-2.0, Configuration/CLDC-1.1");
            hc.setRequestProperty("Content-Language","en-US");
            hc.setRequestMethod(HttpConnection.POST);
            os = (DataOutputStream)hc.openDataOutputStream();
            os.writeUTF(name);
            os.writeUTF(company);
            os.writeUTF(address);
            os.writeLong(contact);
            os.writeUTF(email);
            os.writeUTF(uname);
            os.writeUTF(pwd);
            os.flush();
            os.close();
            is = (DataInputStream)hc.openDataInputStream();            
            int ch;
            cid = "";
            while((ch = is.read()) != -1)
                cid = cid + ((char)ch);
            //String cidStr = s.toString();
            //cid = Integer.parseInt(s);
            //cid = -1;
            System.out.println("obtained "+cid);
            
            is.close();
            
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

}

The servlet is working fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.