sql - J2ME 连接本地主机 nullpointerexception 0

标签 sql database java-me localhost midp

我正在尝试连接本地主机并通过 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();

    }
}

请检查一下......

最佳答案

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

关于sql - J2ME 连接本地主机 nullpointerexception 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12669953/

相关文章:

java - SQL 语句在空表上返回一个结果集

mysql - sql基于ANDing多个详细记录选择主记录

mysql - 根据条件连接两个表以及第二个表中的记录数

mysql - 这个数据库看起来不错吗?我正常化了吗?

java-me - 检测是否有触摸设备

每天按实例汇总的 SQL 查询

mysql - 错误 1064 : SQL syntax

mysql - 根据情况使用不同的MySQL数据库

java - 打开我手机的消息INBOX表单midlet

java - 适用于所有手机型号的 J2ME 通用 api