Java ME, why does this code throw an exception?

J

Jeff

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);
}
 
A

Andrew Thompson

Jeff said:

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
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) {

// NEVER ignore exceptions in code that fails for
// reasons not known!
err.printStackTrace();
}
taskList = new List("My Tasks", Choice.IMPLICIT, task,
imageArray);
taskList.addCommand(exitCommand);
taskList.addCommand(okCommand);
taskList.setCommandListener(this);
display.setCurrent(taskList);

} catch (Exception e) {
// should catch the NPE (and all other exceptions)
e.printStackTrace();
}

HTH

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200706/1
 
T

The Billboard Hot 100

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
 
B

Blake Matheny

Initialize the task array properly. Something like task = new
String[3];

-Blake

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);

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top