• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Please check this code !!!!

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
}
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I check number of dateIndex parameter?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic