We have written the code in a J2MEWTK and trying to run it but it's giving the following error.
startApp threw an Exception
java.lang.NullPointerException
java.lang.NullPointerException
at FirstExample.downloadPage(+159)
at FirstExample.startApp(+8)
at javax.microedition.midlet.MIDletProxy.startApp(+7)
at com.sun.midp.midlet.Scheduler.schedule(+225)
at com.sun.midp.dev.DevMIDletSuiteImpl.schedule(+7)
at com.sun.midp.Main.runLocalClass(+20)
at com.sun.midp.Main.main(+68)
The code that we have written is as follows and trying to connect with the HTTP connection to our localhost and trying to display the Welcome.html page on the microbowser(mobile). The code is running till // *****// but after that it's giving problem. Can you please look it into give me an immediate feedback.
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
/**
* An example MIDlet to fetch a page
using an HttpConnection.
*/
public class FirstExample extends MIDlet {
private Display display;
private
String url =
"http://localhost:8080/Welcome.html";
public FirstExample() {
display = Display.getDisplay(this);
}
/**
* startApp is invoked when
* the MIDlet is activated.
*/
public void startApp() {
//The specified URL is overridden
//in the descriptor
try {
downloadPage(url);
} catch(IOException e) {
// handle the exception
}
}
private void downloadPage(String url)
throws IOException {
StringBuffer b = new StringBuffer();
InputStream is = null;
HttpConnection c = null;
TextBox t = null;
try {
long len = 0 ;
int ch = 0;
c = (HttpConnection)Connector.open(url);
//*****
is = c.openInputStream();
*****//
len =c.getLength();
if( len != -1) {
// Read exactly Content-Length bytes
for(int i =0 ; i < len ; i++ )
if((ch = is.read()) != -1) {
b.append((char) ch);
}
}
else {
//Read until the connection is closed.
while ((ch = is.read()) != -1) {
len = is.available() ;
b.append((char)ch);
}
}
t = new TextBox("hello again....",
b.toString(), 1024, 0);
} finally {
is.close();
c.close();
}
display.setCurrent(t);
}
/**
* Pause, discontinue....
*/
public void pauseApp() { }
/**
* Destroy must cleanup everything.
*/
public void destroyApp(boolean unconditional) { }
}