运行时发现异常:Java.lang.IllegalArgumentException

caoduan1983 2008-05-30 07:14:51
我编写了一个表现生命节律曲线程序,编译没有问题,运行发现异常,报错提示:
Unable to create MIDlet BiorhymMIDletjava.lang.lllegalArgumentException
程序如下:
1.BiorhythmMIDlet.java
import java.io.*;
import java.util.Date;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;



public final class BiorhythmMIDlet extends MIDlet
implements CommandListener //,ItemStateListener
{


public BiorhythmMIDlet()
{
super();
canvas = new BiorhythmCanvas();
canvas.addCommand(exitCmd);
canvas.addCommand(showBirthCmd);
canvas.setCommandListener(this);

form = new BiorhythmForm("生日");
form.addCommand(birthOKCmd);
form.addCommand(birthCancelCmd);
form.setCommandListener(this);
}

public void startApp()
{


readDate();
if( date == null)
{
Display.getDisplay(this).setCurrent(form);
}
else
{
canvas.setBirthday(date);

Display.getDisplay(this).setCurrent(canvas);
}
}

public void pauseApp()
{
}

public void destroyApp(boolean flag)
{
}

public void commandAction(Command c,Displayable d)
{
if (c== exitCmd)
{
notifyDestroyed();
}
else if ( c == showBirthCmd )
{
form.setDate(date);
Display.getDisplay(this).setCurrent(form);
}
else if ( c== birthOKCmd)
{

date = form.getDate();
if (date == null) return;
canvas.setBirthday(date);
saveDate();

Display.getDisplay(this).setCurrent(canvas);
}
else if (c == birthCancelCmd)
{
Display.getDisplay(this).setCurrent(canvas);
}
}

private void saveDate()
{
byte abyte0[];
if (date == null)
return;
abyte0 = null;
try
{
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);
dataoutputstream.writeLong(date.getTime());
abyte0 = bytearrayoutputstream.toByteArray();
dataoutputstream.close();
}
catch (IOException ioexception)
{
System.err.println(ioexception);
}
if (abyte0 == null)
return;
try{
RecordStore recordstore = RecordStore.openRecordStore("Biorhythm",true);
if (recordstore == null)
return;


int cnt = recordstore.getNumRecords();
if ( cnt == 0)
{
recordstore.addRecord(abyte0,0,abyte0.length);
}
else
{
recordstore.setRecord(1,abyte0,0,abyte0.length);
}

recordstore.closeRecordStore();

} catch (RecordStoreException recordstoreexception)
{
System.err.println(recordstoreexception);
}
}


private void readDate()
{
byte abyte0[] = null;
try
{
RecordStore recordstore = RecordStore.openRecordStore("Biorhythm",false);
if (recordstore.getNumRecords() !=0)
abyte0 = recordstore.getRecord(1);

recordstore.closeRecordStore();

}
catch (RecordStoreException recordstoreexception)
{
System.err.println(recordstoreexception);
}
if (abyte0 !=null)
try
{
DataInputStream datainputstream = new DataInputStream(new ByteArrayInputStream(abyte0));
date = new Date (datainputstream.readLong());
datainputstream.close();
}
catch(IOException ioexception)
{
System.err.println(ioexception);
}
}

private Date date;
private BiorhythmCanvas canvas;
private BiorhythmForm form;


private Command exitCmd = new Command("Exit",Command.EXIT,1);
private Command showBirthCmd = new Command("Birth",Command.SCREEN,2);
private Command birthOKCmd = new Command("OK",Command.EXIT,1);
private Command birthCancelCmd = new Command("Cancel",Command.SCREEN,1);


}
...全文
18244 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuanyu113 2011-07-03
  • 打赏
  • 举报
回复
不过这个异常是什么捏!
zengcity 2008-07-03
  • 打赏
  • 举报
回复
BiorhythmForm
y = new TextField("年"," ", 4, TextField.NUMERIC);
m = new TextField("月"," ", 2, TextField.NUMERIC);
d = new TextField("日"," ", 2, TextField.NUMERIC);
改成:
y = new TextField("年","", 4, TextField.NUMERIC);
m = new TextField("月","", 2, TextField.NUMERIC);
d = new TextField("日","", 2, TextField.NUMERIC);

输入类型是数字型,你却默认设置了一个空格,所以出错.
Acylas 2008-06-02
  • 打赏
  • 举报
回复
应该不是setDate方法,看下面两个异常,19行不是调用setField这个方法吗?
传递的参数是否正确?
at javax.microedition.lcdui.TextField.setField. <init>(+168)
at BiorhythmForm. <init>(+19)
bobor_2008 2008-06-02
  • 打赏
  • 举报
回复
没有仔细就看看了你我个标题.
Java.lang.IllegalArgumentException

你既然有这个异常那就应该多看看那的算术运算出问题了.
caoduan1983 2008-05-31
  • 打赏
  • 举报
回复
补充异常信息:Unable to create MIDlet BiorhymMIDlet java.lang.lllegalArgumentException
at javax.microedition.lcdui.TextField.setChars(+150)
at javax.microedition.lcdui.TextField.setString(+27)
at javax.microedition.lcdui.TextField.setField.<init>(+168)
at BiorhythmForm.<init>(+19)
at BiorhythmMIDlet.<init>(+117)
at java.lang.Class.runCustomCode(+0)
at com.sun.midp.midlet.MIDletState.createMIDlet(+34)
at com.sun.midp.midlet.Selector.run(+22)
Execution completed.
望指教!
hchmy 2008-05-31
  • 打赏
  • 举报
回复
太长了,不好判断楼主那里代码出问题了。 呵呵。

建议楼主通过 打标记 的方式的 debug吧。
Acylas 2008-05-31
  • 打赏
  • 举报
回复
异常信息太少了,
肯定某些方法传递的参数不符合
caoduan1983 2008-05-31
  • 打赏
  • 举报
回复
我还是发现不了错误,请问可以说的详细点吗?
caoduan1983 2008-05-31
  • 打赏
  • 举报
回复
这个程序的第19行是:public void setDate(Date date) ,请问具体怎么改呢?
Acylas 2008-05-31
  • 打赏
  • 举报
回复
at BiorhythmForm. <init>(+19)
看看BiorhythmForm这个类的第19行,参数设置不当
Shine_Panda 2008-05-30
  • 打赏
  • 举报
回复
仔细检查 BiorhythmMIDlet 这个了类的构造方法中的参数.
Brokenfango 2008-05-30
  • 打赏
  • 举报
回复
太长了
caoduan1983 2008-05-30
  • 打赏
  • 举报
回复
程序太长,还有两个贴在这里了。
2.BiorhythmForm.java
import java.util.Calendar;
import java.util.Date;
import javax.microedition.lcdui.*;

class BiorhythmForm extends Form
{
public BiorhythmForm(String str)
{

super(str);
y = new TextField("年"," ", 4, TextField.NUMERIC);
m = new TextField("月"," ", 2, TextField.NUMERIC);
d = new TextField("日"," ", 2, TextField.NUMERIC);
append(y);
append(m);
append(d);
}

public void setDate(Date date)
{
if(date != null);
{

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
y.setString(Integer.toString(calendar.get(Calendar.YEAR)));
m.setString(Integer.toString(calendar.get(Calendar.MONTH) + 1));
d.setString(Integer.toString(calendar.get(Calendar.DATE)));
}
}

public Date getDate()
{
try{

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.YEAR,Integer.parseInt(y.getString()));
calendar.set(Calendar.MONTH,Integer.parseInt(m.getString()) - 1);
calendar.set(Calendar.DATE,Integer.parseInt(d.getString()));
return calendar.getTime();
}catch (Exception e )
{
return null;
}
}


protected TextField y;
protected TextField m;
protected TextField d;


}
3.BiorhythmCanvas.java
import java.util.Date;
import javax.microedition.lcdui.*;
class BiorhythmCanvas extends Canvas
{

Date today = new Date();
Date birthday = new Date ( today.getTime() -100*msperday );
Date leftday = new Date ( today .getTime() -days/2*msperday);

public static int days = 22;
public static long msperday = 24L*60*60*1000L;


public void setBirthday ( Date birthday )
{

this.birthday = birthday;
if( birthday !=null )
repaint();

}

public void paint(Graphics g)
{
if( birthday == null ) return;

int kx = getWidth() /days;
int h = getHeight() /2;
g.setGrayScale(255);
g.fillRect(0,0,getWidth(),getHeight());
g.setGrayScale(0);
g.drawLine(0,h,getWidth(),h);

int todayX = (int) (((today.getTime() - leftday.getTime())/msperday)*kx);
g.drawLine(todayX, 0, todayX ,h*2);

for( int i=0; i<=days;i++)
{
g.drawLine( i*kx,h,i*kx,h+3 );
}

Font font = Font.getDefaultFont();

g.setColor( 0xFF0000);
g.drawString("生理",todayX+3, 3,
Graphics.TOP|Graphics.LEFT);
drawCurve(phy,g);

g.setColor( 0x007700);
g.drawString("情绪",todayX+3, 3+font.getHeight(),
Graphics.TOP|Graphics.LEFT);
drawCurve(emt,g);


g.setColor( 0x0000FF);
g.drawString("智力",todayX+3, 3+2*font.getHeight(),
Graphics.TOP|Graphics.LEFT);
drawCurve(intell,g);

}


public void keyPressed(int keyCode)
{
if(keyCode == KEY_NUM4 || getGameAction(keyCode) == LEFT)
{
leftday = new Date ( leftday.getTime() - msperday );
repaint();
}

else if (keyCode == KEY_NUM6 || getGameAction(keyCode) == RIGHT)
{
leftday = new Date ( leftday.getTime() + msperday );
repaint();
}
}

private void drawCurve(short [] data,Graphics g)
{


int kx = getWidth()/days;
int h = getHeight()/2;

int diff = (int)((leftday.getTime() - birthday .getTime())/msperday);
for(int i=0; i<=days; i++)
{
long val1 = data [(diff+i) % data.length];
long val2= data [(diff+i+1) % data.length];

g.drawLine(
i * kx,(int)(h-val1*h/1000),
(i+1)*kx,(int)(h-val2*h/1000)
);
}

}


private static final short phy[] = {
0,270,520,731,888,979,998,942,817,631,
398,136,-136,-398,-631,-817,-942,-998,-979,-888,-731,-520,-270
};
private static final short emt[] = {
0,223,434,623,782,901,975,1000,975,901,782,623,434,223,0,-223,-434,-623,-782,-901,
-975,-1000,-975,-901,-782,-623,-434,-223
};
private static final short intell[] = {
0,189,372,541,690,815,910,972,999,990,
945,866,756,618,458,282,95,-95,-282,-458,
-618,-756,-866,-945,-990,-999,-972,-910,-815,-690,
-541,-372,-189
};
}

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧