Below is the properly functioning code that reflects the recommended changes:
Code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.rms.*;
import java.lang.*;
public class DateToStringTest extends MIDlet implements CommandListener, ItemCommandListener {
private Command exitCommand, saveCommand, cancelDisplaySavedInfoCommand;
private Display display;
private Form screen;
private Form frmDisplaySavedInfo;
private Alert alert;
private DateField dateChoiceField;
private String strDateChoice;
private Date date;
public DateToStringTest() {
// Get the display object for the MIDlet.
display = Display.getDisplay(this);
// Create the Exit, Save, and Cancel commands.
exitCommand = new Command("Exit", Command.EXIT, 2);
saveCommand = new Command("Save", Command.OK, 2);
cancelDisplaySavedInfoCommand = new Command("Cancel", Command.CANCEL, 2);
// Create the screen form.
screen = new Form("Main Screen");
// Set the Exit and Save commands for the screen.
screen.addCommand(exitCommand);
screen.addCommand(saveCommand);
screen.setCommandListener(this);
} // This ends "public DateToStringTest()".
public void startApp() {
// Create a date field for the main screen.
dateChoiceField = new DateField("Date Choice:", DateField.DATE);
dateChoiceField.setItemCommandListener(this);
screen.append(dateChoiceField);
// Set the current display to the screen.
display.setCurrent(screen);
} // This ends startApp().
public void pauseApp() {
} // This ends pauseApp().
public void destroyApp(boolean unconditional) {
} // This ends destroyApp().
public void saveCom() {
// Get the date choice that was entered on the main screen.
Date date = dateChoiceField.getDate();
if (date == null) {
System.out.println("The date choice has a null value (the date was not chosen)");
alert = new Alert("Date Wasn't Chosen", // This version does not permit continuing.
"Cannot save. Please choose a date.", null, AlertType.WARNING); // This version does not permit continuing.
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
return;
} else {
strDateChoice = date.toString();
System.out.println("The date choice is: " + strDateChoice);
}
// Create the save form for testing purposes, in order to display what would be saved.
frmDisplaySavedInfo = new Form("Saved Info");
// Set the "Cancel" command for the save form.
frmDisplaySavedInfo.addCommand(cancelDisplaySavedInfoCommand);
frmDisplaySavedInfo.setCommandListener(this);
// Display the test information.
frmDisplaySavedInfo.append(new StringItem("", "Testing\n"));
frmDisplaySavedInfo.append(new StringItem("", strDateChoice + "\n"));
// Set the current display to the save form.
display.setCurrent(frmDisplaySavedInfo);
} // This ends saveCom().
public void cancelDisplaySavedInfoCom() {
display.setCurrent(screen);
} // This ends cancelDisplaySavedInfoCom().
public void commandAction(Command c, Item item) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
} else if (c == saveCommand) {
saveCom();
} else if (c == cancelDisplaySavedInfoCommand) {
cancelDisplaySavedInfoCom();
}
} // This ends "public void commandAction(Command c, Item item)".
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
} else if (c == saveCommand) {
saveCom();
} else if (c == cancelDisplaySavedInfoCommand) {
cancelDisplaySavedInfoCom();
}
} // This ends "public void commandAction(Command c, Displayable d)".
} // This ends "public class DateToStringTest extends MIDlet implements CommandListener, ItemCommandListener".
Thank you again, skalogir.