Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Java ME, why does this code throw an exception?

Thread view: 
Jeff - 10 Jun 2007 08:11 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.