【问题标题】:J2ME connect localhost nullpointerexception 0J2ME 连接本地主机 nullpointerexception 0
【发布时间】:2012-09-22 02:32:48
【问题描述】:

我正在尝试连接本地主机并通过 j2me 应用程序将数据插入数据库。但是当我连接服务器时,它显示有一个 nullpointerexception 0 错误。

这是 midlet 代码

import java.io.DataOutputStream;
import java.io.InputStream;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.*;


public class Midlet_1 extends MIDlet implements CommandListener {

Display mdDisplay;
Form mForm;
StringItem messageitem;
Command exit, connectCommand;

public Midlet_1() {

    mForm = new Form("My Counter midlet");
    messageitem = new StringItem(null, "");
    exit = new Command("Exit", Command.EXIT, 0);
    connectCommand = new Command("Connect", Command.SCREEN, 0);
    mForm.append(messageitem);
    mForm.addCommand(exit);
    mForm.addCommand(connectCommand);
    mForm.setCommandListener(this);

}

public void startApp() {

    mdDisplay = Display.getDisplay(this);
    mdDisplay.setCurrent(mForm);

}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d) {
    if (c == exit) {
        notifyDestroyed();
    } else if (c == connectCommand) {
        Form waitform = new Form("Waiting");
        mdDisplay.setCurrent(waitform);
        Thread t = new Thread() {

            public void run() {
                connect();
            }
        };
        t.start();
    }
}

private void connect() {
    try {
        HttpConnection hs = null;
        InputStream in = null;
        String url = "localhost:8080/testweb/src/save";

        hs.setRequestProperty("User-Agent", "Profile/MIDP-2.0,Configuration/CLDC-2.0");
        hs.setRequestProperty("Content-Language", "en-US");
        hs.setRequestMethod(HttpConnection.POST);
        DataOutputStream ds = hs.openDataOutputStream();
        ds.writeUTF("nam56");
        ds.writeUTF("67");
        ds.writeUTF("0716522549");
        ds.flush();
        ds.close();


        in = hs.openInputStream();
        int connectlength = (int) hs.getLength();
        byte[] raw = new byte[connectlength];
        int length = in.read(raw);
//            int ch;
//            StringBuffer sb=new StringBuffer();
//            while((ch=in.read())!=-1){
//            sb.append((char)ch);
//            }
        in.close();
        hs.close();
        String s = new String(raw, 0, length);
        messageitem.setText(s);
    } catch (Exception e) {
        messageitem.setText(e.toString());
        System.out.println(e);
    }
    mdDisplay.setCurrent(mForm);
    }
}

这是 servlet 代码

 protected void processRequest(HttpServletRequest request, HttpServletResponse       response)
        throws ServletException, IOException, ClassNotFoundException, SQLException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        DataInputStream in=new DataInputStream(request.getInputStream());
        String name=in.readUTF();
        String id=in.readUTF();
        String contt=in.readUTF();

        Connection c=DBcon.setconConnection();
        Statement s=c.createStatement();
        s.executeUpdate("insert into details values('"+id+"','"+name+"''"+contt+"')");
        out.print("successfullllll");


    } finally {            
        out.close();

    }
}

请看看这个.....

【问题讨论】:

  • 你是用模拟器还是真机?
  • 你做了什么调试,到底是哪里抛出的异常等等

标签: sql database java-me localhost midp


【解决方案1】:

在您的 connect() 方法中,我可以看到您将 hs 初始化为 null,然后您调用了 setRequestProperty。尝试在调用其方法之前正确初始化 hs。

【讨论】:

    【解决方案2】:

    仅当您在与服务器相同的机器上运行模拟器时,这可能才有效。尝试将locahost 替换为127.0.0.1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-22
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多