2

I have written the following Java Application:

import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;


public class JavaApplication6 extends MIDlet
{
private static Form clockForm=null;
private static StringItem clockItem=new StringItem("hello", "World!");
private static ClockTask task=null;
private static Timer timer=null;

static class ClockTask extends TimerTask 
{
private int count=0;
        @Override
public void run()
{
count++;
clockItem.setText("hello "+count);
}
}
public static  void JavaApplication6() throws Exception
{
clockForm=new Form("Clock");
clockItem=new StringItem("","0");
clockForm.append(clockItem);
}

    @Override
public  void startApp()
{
task=new ClockTask();
timer=new Timer();
timer.schedule(task,1000,1000);
Display.getDisplay(this).setCurrent(clockForm);}

    @Override
public void pauseApp()
{}

public void destroyApp(boolean unconditional)
{}


    public static void main(String[] args) {


        JavaApplication6 test=new JavaApplication6();
        test.startApp();



    }
}

but when I run it, it gives me the following exception on the last line of startApp();

Exception in thread "main" java.lang.NullPointerException
    at javax.microedition.lcdui.Display.<init>(Display.java:420)
    at javax.microedition.lcdui.Display.getDisplay(Display.java:447)
    at javaapplication6.JavaApplication6.startApp(JavaApplication6.java:42)
    at javaapplication6.JavaApplication6.main(JavaApplication6.java:56)
gnat
  • 6,213
  • 108
  • 53
  • 73
lonesome
  • 2,503
  • 6
  • 35
  • 61

2 Answers2

2

You created static method JavaApplication6 named as it is the constructor. But it is not. So it is not called when you say JavaApplication6 test=new JavaApplication6();. Therefore clockForm remains uninitialized, i.e. null. So, line

Display.getDisplay(this).setCurrent(clockForm);

throws NPE becuase clockForm is null at this point.

The solution is to remove static void from line public static void JavaApplication6() throws Exception. It should look like

public JavaApplication6() throws Exception

In this case it becomes constructor and everything will work.

Good luck.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • still a NPE but this time in a diff part, this time it gives the exception at **clockForm=new Form("Clock")** – lonesome May 14 '12 at 03:53
-3

You seem to be approaching things in a fundamentally wrong way.

I mean, even if you somehow manage to figure and hack through all of the null pointer exceptions, you will not get a valid MIDlet application code.

Thing is, because class JavaApplication6 extends MIDlet, the following line will throw runtime exception at any properly functioning MIDP implementation (read: at any correctly working device):

JavaApplication6 test=new JavaApplication6(); // SecurityException in runtime

This is the way how things are specified by MIDP (JSR 118) API. Above line means that application attempts to invoke MIDlet constructor bypassing the AMS, which will cause SecurityException in runtime.

This is clearly explained in MIDlet constructor javadocs, note what is stated in Throws:

protected MIDlet()

    Protected constructor for subclasses. The application management software
    is responsible for creating MIDlets and creation of MIDlets is restricted.
    MIDlets should not attempt to create other MIDlets.

Throws:
    SecurityException - unless the application management software is creating
    the MIDlet.
gnat
  • 6,213
  • 108
  • 53
  • 73
  • so you are telling me there is no way to make my application working? – lonesome May 15 '12 at 06:36
  • @user1064929 why? there are tens thousands midlets out there that work just fine. To make your one working just like thousands others, write the correct code. For a start, you can drop `main` with everything that is in there and find a way to get what you need without. Click on `midp` tag under your question to find tutorials if you're interested – gnat May 15 '12 at 06:40
  • @user1064929 where did you get that tutorial? – gnat May 16 '12 at 04:15