-1

在我将图像从服务器下载到 J2ME 电话的代码中,我在下面收到这两个错误。请帮助指导我如何删除它们。

java.lang.NullPointerException: 0

在 ServerImages.ImageMidletServlet$Test.run(ImageMidletServlet.java:102)

在 java.lang.Thread.run(),bci=11

未捕获的异常:java.lang.IllegalArgumentException

javax.microedition.lcdui.Display.setCurrent(), bci=160

  • 在 ServerImages.ImageMidletServlet$Test.showAlert(ImageMidletServlet.java:117)

在 ServerImages.ImageMidletServlet$Test.run(ImageMidletServlet.java:108)

在 java.lang.Thread.run(),bci=11

使用的代码如下

public class ImageMidletServlet extends MIDlet implements CommandListener {

    Display display = null;
    Form form = null;
    String url = "http://localhost:8080/C:/Users/HASENDE/Pictures/ase.jpg";
    Command backCommand = new Command("Back", Command.BACK, 0);
    Command submitCommand = new Command("Submit", Command.OK, 2);
    Command exitCommand = new Command("Exit", Command.STOP, 3);
    private Test test;

    public ImageMidletServlet() {}

    public void startApp() throws MIDletStateChangeException {
        display = Display.getDisplay(this);
        form = new Form("Show Image");
        form.addCommand(submitCommand);
        form.addCommand(exitCommand);
        form.setCommandListener(this);
        display.setCurrent(form);
    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}

    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            destroyApp(true);
            notifyDestroyed();
        } else if (c == backCommand) {
            display.setCurrent(form);
        } else if (c == submitCommand) {
            test = new Test(this);
            test.start();
        }
    }

    class Test implements Runnable {

        ImageMidletServlet midlet;
        private Display display;

        public Test(ImageMidletServlet midlet) {
            this.midlet = midlet;
            display = Display.getDisplay(midlet);
        }

        public void start() {
            Thread t = new Thread(this);
            t.start();
        }

        public void run() {
            DataInputStream is = null;
            StringBuffer sb = new StringBuffer();
            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");
                    ImageItem imgItem = new ImageItem("", img,
                            ImageItem.LAYOUT_NEWLINE_AFTER
                                | ImageItem.LAYOUT_CENTER, null);
                    f.append(imgItem);
                    display.setCurrent(f);
                } else {
                    showAlert("length is null");
                    ;
                }
                is.close();
                c.close();
            } catch (Exception e) {
                e.printStackTrace();
                showAlert(e.getMessage());
            }
        }

        /* Display Error On screen */
        private void showAlert(String err) {
            Alert a = new Alert("");
            a.setString(err);
            a.setTimeout(Alert.FOREVER);
            display.setCurrent(a);
        }
    };
}

然后当我执行命令提交时,我不知道为什么模拟器显示“长度为空”的选择而不是下载图像。任何帮助都非常感谢!

4

1 回答 1

0

有些服务器没有正确设置 HTTP 标头 Content-Length,因为它 c.getLength() 可能我会返回 0(零),即使您有内容要阅读。

您可以在 Connector.open 之后立即尝试 Image.createImage(c.openDataInputStream()),而不是使用缓冲区。

于 2012-04-09T21:09:57.367 回答