Author: mingjava
Original address: http://www.j2medev.com/Article/ShowArticle.asp? ArticleID = 1117
When using netbeans and eclipse for the development of j2s, image path problems often occur, causing the creation of image objects in the program to fail, and the image object is not used to determine whether it is not null, therefore, a null pointer Exception error occurs. For example, the following program code
Package com. j2medev. test;
Import java. Io. ioexception;
Import javax. microedition. lcdui. display;
Import javax. microedition. lcdui. form;
Import javax. microedition. lcdui. image;
Import javax. microedition. MIDlet. MIDlet;
Import javax. microedition. MIDlet. midletstatechangeexception;
Public class helloworld extends MIDlet {
Private display = NULL;
Protected void Startapp () throws midletstatechangeexception {
// Todo auto-generated method stub
Display = display. getdisplay (this );
Form = new form ("image path ");
Image IMG = NULL;
Try {
IMG = image. createimage ("/leaf.png ");
} Catch (ioexception ex ){
Ex. printstacktrace ();
}
Form. append (IMG );
Display. setcurrent (form );
}
Protected void pauseapp (){
// Todo auto-generated method stub
}
Protected void destroyapp (Boolean arg0) throws midletstatechangeexception {
// Todo auto-generated method stub
}
}
Under normal circumstances, the running program should display a small image in a form, as shown below:
If the settings in netbeans or eclipse are incorrect, such errors often occur. The running errors in eclipse are printed out here.
Running by storing the root temp. defacolorcolorphone1134966399357
Java. Io. ioexception
At javax. microedition. lcdui. immutableimage. getimagefromstream (+ 15)
At javax. microedition. lcdui. immutableimage. <init> (+ 20)
At javax. microedition. lcdui. image. createimage (+ 8)
At com. j2medev. Test. helloworld. Startapp (+ 25)
At javax. microedition. MIDlet. midletproxy. Startapp (+ 7)
At com. Sun. MIDP. MIDlet. schedle. Schedule (+ 270)
At com. Sun. MIDP. Main. Main. runlocalclass (+ 28)
At com. Sun. MIDP. Main (+ 116)
Startapp threw an exception
Java. Lang. nullpointerexception
Java. Lang. nullpointerexception
At javax. microedition. lcdui. Form. append (+ 11)
At com. j2medev. Test. helloworld. Startapp (+ 39)
At javax. microedition. MIDlet. midletproxy. Startapp (+ 7)
At com. Sun. MIDP. MIDlet. schedle. Schedule (+ 270)
At com. Sun. MIDP. Main. Main. runlocalclass (+ 28)
At com. Sun. MIDP. Main (+ 116)
Execution completed.
The key to solving the above problems is to be familiar with the development environments netbeans and S, which are summarized as follows:
1) eclipse
For eclipse, you need to create a new source folder under the project. Note that it is not a folder, and then put the image in this directory. We usually call this directory res, which can be used not only for pictures but also for audio files and other resources. As shown in:
In general, the source folder is added to the Java build path of the project by default, so there will be no such problem as the image cannot be found.
2) netbeans
Netbeans is similar to eclipse. You can configure all attributes and configurations related to J2EE in "attributes. First, create a directory res under the project, right-click the project, and select "properties ". Add the newly created directory res to "generate" -- "database and resource", as shown in: