File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java Micro Edition and the fly likes Please check this code !!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Please check this code !!!!" Watch "Please check this code !!!!" New topic
Author

Please check this code !!!!

golftel na
Greenhorn

Joined: Aug 11, 2002
Posts: 5
I can compile this code successfully. But while running after I set date&time and press for alarm.
It happens error
java.lang.IndexOutOfBoundException
at javax.microedition.lcdui.Form.delete(+25)
at Snooze3.commandAction(+145)
at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+152)
at com.sun.kvem.midp.lcdui.EmulEventHandler$EventLoop.run(+427)
Could you solve this problem?
************ This is source code *****************
import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Timer;
import java.util.TimerTask;
public class Snooze3 extends MIDlet implements ItemStateListener, CommandListener
{
private Display display; // Reference to display object
private Form fmMain; // The main form
private Command cmSnooze; // Start the timer
private Command cmReset; // Reset to current date/time
private Command cmExit;
private Command cmSelect;
private ChoiceGroup cgEvent;
private DateField dfSnoozeTime; // How long to snooze
private int dateIndex; // Index of the DateField on the Form
private Date currentTime; // Current time...changes when pressing reset
private Timer tmSnooze; // The timer - keeps track of system time
private SnoozeTimer ttSnooze; // Called by the timer
private boolean dateOK = false; // Was the user input valid?
private int choiceGroupIndex;
public Snooze3()
{
display = Display.getDisplay(this);

fmMain = new Form(" ");
cgEvent=new ChoiceGroup("What Event:", Choice.EXCLUSIVE);
cgEvent.append("Wake up", null);
cgEvent.append("Call up", null);

cmSelect = new Command("Select", Command.SCREEN, 2);
cmExit = new Command("Exit", Command.EXIT, 1);
choiceGroupIndex=fmMain.append(cgEvent);

fmMain.addCommand(cmSelect);
fmMain.addCommand(cmExit);
fmMain.setCommandListener(this);
}
public void startApp ()
{
display.setCurrent(fmMain);
}
public void pauseApp()
{ }
public void destroyApp(boolean unconditional)
{ }
public void itemStateChanged(Item item)
{
if (item == dfSnoozeTime)
{
if (dfSnoozeTime.getDate().getTime() < currentTime.getTime())
dateOK = false;
else
dateOK = true;
}
}
public void commandAction(Command c, Displayable s)
{
if (c == cmSnooze)
{
if (dateOK == false)
{
Alert al = new Alert("Unable to set alarm", "Please choose another date and time.", null, null);
al.setTimeout(Alert.FOREVER);
al.setType(AlertType.ERROR);
display.setCurrent(al);
}
else
{
tmSnooze = new Timer();
ttSnooze = new SnoozeTimer();

long amount = dfSnoozeTime.getDate().getTime() - (currentTime.getTime()+25200000);
tmSnooze.schedule(ttSnooze,amount);

fmMain.removeCommand(cmSnooze);
fmMain.removeCommand(cmReset);

fmMain.delete(dateIndex);

fmMain.setTitle("Snoozing...");
}
}
else if (c == cmReset)
{
dfSnoozeTime.setDate(currentTime = new Date());
}
else if (c == cmSelect)
{
currentTime = new Date();

dfSnoozeTime = new DateField("", DateField.DATE_TIME);
cmSnooze = new Command("Snooze", Command.SCREEN, 1);
cmReset = new Command("Reset", Command.SCREEN, 1);

dateIndex = fmMain.append(dfSnoozeTime);
fmMain.addCommand(cmSnooze);
fmMain.addCommand(cmReset);
fmMain.setCommandListener(this);
fmMain.setItemStateListener(this);
fmMain.delete(choiceGroupIndex);
fmMain.removeCommand(cmSelect);
}
else if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
}

private class SnoozeTimer extends TimerTask
{
public final void run()
{
Alert al = new Alert("Time to wake up!");
al.setTimeout(Alert.FOREVER);
al.setType(AlertType.ALARM);
AlertType.ERROR.playSound(display);
display.setCurrent(al);

cancel();
}
}
}


-
Rishi Tyagi
Ranch Hand

Joined: Feb 14, 2002
Posts: 100
Change the following part of the code in your program
long amount = dfSnoozeTime.getDate().getTime() - (currentTime.getTime()+25200000);
tmSnooze.schedule(ttSnooze,amount);
fmMain.removeCommand(cmSnooze);
fmMain.removeCommand(cmReset);
fmMain.delete(dateIndex);

with this one
long amount = dfSnoozeTime.getDate().getTime() - (currentTime.getTime()+2520000);
tmSnooze.schedule(ttSnooze,amount);
fmMain.removeCommand(cmSnooze);
fmMain.removeCommand(cmReset);
try{
fmMain.delete(dateIndex);
}catch(Exception ee){}
Hope it will not give any error. If yes then check the value of dateIndex parameter

Rishi
golftel na
Greenhorn

Joined: Aug 11, 2002
Posts: 5
How can I check number of dateIndex parameter?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please check this code !!!!