2013-07-28 42 views
1
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。什麼是問題?Graphics.drawImage具有良好映像的NullPointerException J2ME

回答

0

您正在設置Graphics g = null就在您撥打g.drawImage(img, 0, 0, 0)之前。這不是你的img是空的。它是你的Graphics對象。

您還在設置GameCanvas gc = null之前致電gc.paint(g) - 另一個NullPointerException

最重要的是,您無法使用此方法創建並使用GameCanvasGameCanvas是一個抽象類。所以你需要創建自己的課程,它可以擴展爲GameCanvas - 如果你想要一個GameCanvas。我也不確定是否屬於這種情況,因爲如果您只想顯示圖片,您可以輕鬆地處理表格。

試試這個:

img = Image.createImage(data, 0, len); 
Form f = new Form("Image"); 
f.append(img); 
display.setCurrent(f); 
+0

你能後的代碼,將工作?我不知道如何聲明g以及如何創建課堂。我想使用GameCanvas來全屏拖拽圖片。 – srka

相關問題