Display display = null;
String url = "http://188.2.222.253/screenshot.png";
DataInputStream is = null;
Image img= null;
try
{
HttpConnection c = (HttpConnection) Connector.open(url);
int len = (int)c.getLength();
if (len > 0)
{
is = c.openDataInputStream();
byte[] data = new byte[len];
is.readFully(data);
img = Image.createImage(data, 0, len);
Form f = new Form("Image");
GameCanvas gc = null;
Graphics g = null;
g.drawImage(img, 0, 0, 0); //NullPointerException
gc.paint(g);
display.setCurrent(gc);
}
else
{
showAlert("length is null");
}
is.close();
c.close();
}
catch (Exception e)
{
e.printStackTrace();
showAlert(e.getMessage());
}
g.drawImage(img, 0, 0, 0) 抛出 NullPointerException。这意味着 img 为空(http://docs.oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/javax/microedition/lcdui/Graphics.html)。我可以将 img 与 ImageItem 和它的 NOT null 一起使用。什么是问题?