It is not so much the experience of learning J2ME, rather than a kind of C + + language points of attention, the reason for this is because this problem is learning j2me time to find, a whole day, although only a small mistake, but the direct consequence is the program constantly abnormal, so I wrote it, on the one hand to remind myself, Learn knowledge can not be tasted, but should ask why, not only know it but also to know why, on the other hand also tell more like my novice friends do not make the same mistake.
Let's look at a piece of code here:
/*
* Imageitemdemo.java
*
Created on July 24, 2005, PM 1:45
*/
Import Javax.microedition.midle t.*;
Import javax.microedition.lcdui.*;
/**
*
* @author Mark
* @version
*/
public class Imageitemdemo extends MIDlet implements Commandli stener{
Private Command exitcommand=new command ("Exit", command.exit,1);
private Form mainform;;
Private Imageitem Img1,img2;
public void Imageitemdemo () {
Mainform=new Form ("Imageitem Test");
Mainform.append ("show various Imageitem");
Mainform.addcommand (Exitcommand);
Mainform.setcommandlistener (this);
}
public void startApp () {
try{
img1=new imageitem (default image control,
Image.createimage ("/image.png"),
Item.layout_default,
"first picture");
Img2=new Imageitem (default image control,
Image.createimage ("/image.png"),
item.layout_2| item.layout_expand| Item.layout_newline_before,
"second picture");
}catch (Exception err) {
System.out.println ("Can not load ...");
}
MainfOrm.append (IMG1);
Mainform.append (IMG2);
Display.getdisplay (this). Setcurrent (MainForm);
}
public void Pauseapp () {
}
public void Destroyapp (Boolean unconditional) {
}
public void Comman Daction (Command c,displayable d) {
if (c==exitcommand) {
Destroyapp (false);
Notifydestroyed ();
}
}
}
This is a IMAGEITEMT demo program, I add something to the premise of the implementation of the exception caused it, the exception is as follows:
startApp threw an Exception
java.lang.NullPointerException
java.lang.NullPointerException
at ImageItemDemo.startApp(+64)
at javax.microedition.midlet.MIDletProxy.startApp(+7)
at com.sun.midp.midlet.Scheduler.schedule(+270)
at com.sun.midp.main.Main.runLocalClass(+28)
at com.sun.midp.main.Main.main(+116)
Can you find the cause of the anomaly?
First, it is determined that the null pointer exception is thrown by Startapp, where the objects used are mainly two, mainform and IMG1, img2, whereas image loading should be caught by catch (Exception err). So basically can determine is mainform produced anomaly, but the problem comes, mainform I already instantiated in the constructor, and also is the class member variable, should exist with the object, but why is it unusual?
Puzzled by the solution ....
So the Imageitemdemo () in the code moved to Startapp () inside, found incredibly realized, can be sure, that is, there is no instantiation of the mainform. Why? Is it J2ME to execute the Startapp before executing the constructor?? The answer is in the negative. STARTAPP () is just the starting point for the program to run, and the construction of objects and classes is still done by constructors. A hint, is that really a constructor?
An obvious but easily overlooked error, public void Imageitemdemo (), it is not a constructor, even the overload of a constructor is not, why? The void type is restricted. C + + learned for nearly 1 years, really did not think and did not pay attention to this problem, the direct consequence is that, class in the construction time will not execute it, then the mainform nature will not be instantiated, but still just a reference, why talk about the operation?
Change the method is very easy, the void can be removed, so a simple question, consumed me one day, the biggest feeling is not deep enough to learn, the theoretical point of view is still not clear enough, so write it as a cautionary note.