Java ME
The code below compiles but it crashes during execution. Look for "<---
crash here" in the code, that tells where the crash occur. The error is a
NullPointerException. The problem is that I don't know what causing the
exception. When I don't know what's causing the error, it is difficult to
fix it.
Any suggestions what may cause this exception?
public class HelloWorld extends MIDlet implements CommandListener {
private String[] task;
public void commandAction(Command c, Displayable d) {
if (c == okCommand) {
task[0] = "xxxxxx"; <--- crash here
task[1] = "ddddddddd";
task[2] = "eeeeeeeeee";
try {
Image iconGreen = Image.createImage("/icon-green.png");
Image iconRed = Image.createImage("/icon-red.png");
imageArray[0] = iconGreen;
imageArray[1] = iconRed;
imageArray[2] = iconGreen;
} catch (java.io.IOException err) {
}
taskList = new List("My Tasks", Choice.IMPLICIT, task,
imageArray);
taskList.addCommand(exitCommand);
taskList.addCommand(okCommand);
taskList.setCommandListener(this);
display.setCurrent(taskList);
}
Andrew Thompson - 10 Jun 2007 08:33 GMT
>Java ME
I am not experienced with J2ME, but experience with
J2SE suggests a number of things that might be done
to trace the problem..
>public class HelloWorld extends MIDlet implements CommandListener {
>private String[] task;
>
>public void commandAction(Command c, Displayable d) {
try {
> if (c == okCommand) {
> task[0] = "xxxxxx"; <--- crash here
[quoted text clipped - 7 lines]
> imageArray[2] = iconGreen;
> } catch (java.io.IOException err) {
// NEVER ignore exceptions in code that fails for
// reasons not known!
err.printStackTrace();
> }
> taskList = new List("My Tasks", Choice.IMPLICIT, task,
[quoted text clipped - 3 lines]
> taskList.setCommandListener(this);
> display.setCurrent(taskList);
} catch (Exception e) {
// should catch the NPE (and all other exceptions)
e.printStackTrace();
}
>}
HTH

Signature
Andrew Thompson
http://www.athompson.info/andrew/
The Billboard Hot 100 - 10 Jun 2007 08:33 GMT
hey there Jeff,
once an array variable has been declared, memory may be allocated to
it. It can be done with the "new" operator, that allocates memory for
the String object.
public class HelloWorld {
private String[] task = new String[10]; <---- you need to initilize
the array here with "new" operator
public HelloWorld(){
commandAction("Test");
}
public void commandAction(String c) {
if (c == "Test") {
task[0] = "xxxxxx"; <----- now it does not trow an
exception
task[1] = "ddddddddd";
task[2] = "eeeeeeeeee";
}
}
public static void main(String args[]){
new HelloWorld();
}
}
thanks
Emre SIMTAY
Blake Matheny - 10 Jun 2007 08:40 GMT
Initialize the task array properly. Something like task = new
String[3];
-Blake
> Java ME
>
[quoted text clipped - 30 lines]
>
> }
--
Blake Matheny
bmatheny@mobocracy.net
http://mobocracy.net