×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    19

    Appending a string to a form

    Dear All,

    I am very new to JavaME programming and am trying to write a very simple program that simply appens a string to my form when a command is run. However whatever I do, nothing seems to happen when I click the soft command button. My code is as follows:

    public class HelloMIDlet extends MIDlet implements CommandListener
    {

    private Command exitCommand; // The exit command
    private Display display; // The display for this MIDlet
    private Command switchFormCommand;
    private Form mForm;
    private String messages;
    private String messages2;

    public HelloMIDlet()
    {

    display = Display.getDisplay(this);
    exitCommand = new Command("Exit", Command.EXIT, 0);
    switchFormCommand = new Command("SwitchForm", Command.OK,2);

    }

    public void startApp()
    {
    Form mForm = new Form("Change Date");

    java.util.Date now = new java.util.Date();

    DateField dateItem = new DateField("Today's date:", DateField.DATE);

    dateItem.setDate(now);

    String messages = new String ("Welcome, glad you could come.");
    String messages2 = new String ("Thank you for clicking that button");


    mForm.append(dateItem);
    mForm.append(messages);

    mForm.addCommand(exitCommand);
    mForm.addCommand(switchFormCommand);
    mForm.setCommandListener(this);

    display.setCurrent(mForm);
    }

    public void pauseApp()
    {
    }

    public void destroyApp(boolean unconditional)
    {
    }

    public void updatedisplay()
    {

    mForm.append(messages2);

    }

    public void commandAction(Command c, Displayable s)
    {

    String label = c.getLabel();

    if (c == exitCommand)
    {
    destroyApp(false);
    notifyDestroyed();
    }

    if(label.equals("SwitchForm"))
    {
    updatedisplay();

    }

    }

    }

    The CommandAction() function works and does recognise when the 'SwitchForm' command is passed to it, but the commands in the updatedisplay() function don't seem to do anything, ie nothing is added to the display.

    Do I need to issue some code in my updatedisplay() function that gets the display before issuing the form.append command?

    Thanks in advance for any help,

    Phil

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Appending a string to a form

    Quote Originally Posted by phil_mw60 View Post
    private Form mForm;
    private String messages;
    private String messages2;

    public void startApp()
    {
    Form mForm = new Form("Change Date");

    String messages = new String ("Welcome, glad you could come.");
    String messages2 = new String ("Thank you for clicking that button");
    }
    You're declaring new, local variables in your startApp() method, so you're not setting values to the instance variable. I'm surprised you're not getting NullPointerExceptions.

    Also, no need to use "new String()" like this.

    Try:

    Code:
        private static final String MESSAGES = "Welcome, glad you could come.";
        private static final String MESSAGES2 = "Thank you for clicking that button";
    
        private Form mForm;
    
        public void startApp()
        {
            mForm = new Form("Change Date");
    If you don't want to change the messages, you can make them constants. By convention, constants are named in ALL_UPPER_CASE.

    Graham.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    19

    Re: Appending a string to a form

    Hi Graham,

    Thanks for the quick reply, this has fixed the problem.

    I have made my 2 message variables constants as you suggest and also updated the line:

    form mForm = new Form("Change Date");

    to

    mForm = new Form("Change Date");

    Would this have made any difference? Or was it just a problem that the updatedisplay() method couldn't access the value I had assigned locally to Messages2?

    Thanks again

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Appending a string to a form

    Yes, the difference is HUGE. You should think seriously about getting a introductory book on Java if you don't know why, because this is very important.

    The difference is:

    Code:
    private Form form;
    
    public void startApp() {
        // This assigns a value to the non-local variable "form".
        form = new Form("My Form");
    }
    Code:
    private Form form;
    
    public void startApp() {
        // This creates a new variable called "form", with local scope, and assigns
        // it a value.  This has the effect of "hiding" the non-local variable with
        // the same name.  The non-local variable "form" will remain unchanged, with
        // its default value of null.
        Form form = new Form("My Form");
    
        // We now have two variables called "form".  We can still access the
        // non-local "form" explicitly.  The following statement makes the non-local
        // "form" refer to the same object as the local "form".
        this.form = form;
    }
    Graham.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    19

    Re: Appending a string to a form

    Hi Graham,

    Yes I see what you mean now...

    Previously my form was being setup locally in the startApp() method whereas now I am using the global form I created at the start of my class.

    I do have a book on Java basics which I intend to read to get myself back up to speed!

    Thanks again,

    Phil

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Appending a string to a form

    Quote Originally Posted by phil_mw60 View Post
    Previously my form was being setup locally in the startApp() method whereas now I am using the global form I created at the start of my class.
    This is a tricky area to understand.

    It's not the object that is local or global, it is the variable. This is an important distinction to understand, and it's easy to confuse the two concepts. Maybe you're clear on this already, but I'll outline the difference anyway.

    Each object is accessed by a reference. This is actually the place ("address") in memory where the object is stored. In most implementations of Java, you can find this information by:

    Code:
    System.identityHashCode(someObject);
    This gives you the reference as a number (an int), so you can see it. Variables that have a "Class" type hold this same information, but not in a format you can see (because Java, unlike C or C++, does not allow you to convert between references (or "pointers" as they're known in C) and integers).

    So, an object variable is a box that contains this reference, which in turn allows access to the object. Whether the variable is local or global makes no difference to the object - but it affects which parts of the program can "see" that reference.

    Let's go back to your original code again.

    Code:
    private Form mForm;
    private String messages;
    private String messages2;
    
    public void startApp()
    {
        Form mForm = new Form("Change Date");
    
        String messages = new String ("Welcome, glad you could come.");
        String messages2 = new String ("Thank you for clicking that button");
    
        // ... some other code ...
    
        display.setCurrent(mForm);
    }
    So, after startApp() returns (execution reaches the closing "}"), what has happened?

    1. No changes have been made to the non-local variables mForm, messages and messages2, because they were all hidden by local variables of the same name. These three non-local variables are all null, because that is their default value.

    2. Three objects have been created, one Form and two Strings, by three "new" expressions.

    3. The references to these three objects were stored in local variables.

    4. These local variables no longer exist, because they only exist for the duration of executing the enclosing method.

    5. So, the references to the two String objects have been lost. The variables are gone, and the references are gone with them.

    6. Because the references to the String objects are no longer stored anywhere, the two String objects are said to be unreachable. Without a reference, the objects cannot be accessed. You cannot invoke any of their methods. Unreachable objects are garbage. At some point in the future, the garbage collector will find them and release the memory they occupy, so that the memory can be used for something else. Note "at some point in the future" - they have become garbage, but they are not yet collected. These objects still occupy memory, even though they are unreachable, until the garbage collector finds them and releases the memory. The garbage collector might not run until no more memory is available, and it needs to free up some memory to create a new object.

    7. The local variable holding the reference to the Form object is lost too. However, this reference was passed to the Display object, using the setCurrent() method. The Display object has kept a copy of this reference (not a copy of the object, just a copy of the reference), so at least one reference to the Form still exists. While the variable has disappeared (taking its copy of the reference with it), the Form object is still reachable (through the copy of the reference in the Display object), so the Form does not become garbage.

    You could still access the Form by:

    Code:
    ((Form) display.getCurrent()).append("some message");
    Hope that makes some sense.

    Graham.

Similar Threads

  1. Reusing Form
    By arpit2agrawal in forum Mobile Java General
    Replies: 13
    Last Post: 2011-02-14, 05:04
  2. Extracting from a Record
    By Noor Aldoja in forum Mobile Java General
    Replies: 1
    Last Post: 2009-05-16, 12:14
  3. text alignment while appending to form
    By vigneshkrish in forum Mobile Java General
    Replies: 2
    Last Post: 2009-04-09, 14:57
  4. second form not getting key events
    By krsunil in forum Symbian User Interface
    Replies: 3
    Last Post: 2008-12-10, 10:29
  5. LOGIN: user and password to PHP form
    By sandro1972 in forum Mobile Java Networking & Messaging & Security
    Replies: 6
    Last Post: 2008-04-30, 12:31

Posting Permissions

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