-1

このフォーム フィールドを jsp で処理したい:

<div class="control-group">

                                                <!-- Text input-->
                                                <label class="control-label" for="input01">Email:</label>
                                                <div class="controls">
                                                    <input name="email" placeholder="email"
                                                        class="input-xlarge" type="text"
                                                        value="<%=request.getParameter("email")%>">
                                                </div>
                                            </div>
                                            <div class="control-group">
                                                <!-- Text input-->
                                                <label class="control-label" for="input01">Password:</label>
                                                <div class="controls">
                                                    <input name="password" placeholder="password"
                                                        class="input-xlarge" type="text"
                                                        value="<%=request.getParameter("password")%>">
                                                </div>
                                            </div>

[送信] を押すと、

NullPointerException

それは私のサーブレットメソッドです:

@Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        logger.log(Level.INFO, "Creating User!!!");
        logger.info("Request: " +  req.toString());
        PrintWriter out = resp.getWriter();//Here I get the null Pointer exception

        String email = req.getParameter("email");
        String password = req.getParameter("password");
        logger.info(email + password);
        try {
            user.insert(email, password);
            Log.info("Inserted: " + email + "    " + password);
        } catch (Exception e) {
            String msg = DAOUser.getErrorMessage(e);
            out.print(msg);
        }
    }

.

このヌルポインタ例外を修正するには?

アップデート

私のサーブレット:

public class DAOServletUser extends HttpServlet {

    private static final long serialVersionUID = 6820994892755862282L;

    private static final Logger logger = Logger.getLogger(DAOServletUser.class.getCanonicalName());
    /**
     * Get the entities in JSON format.
     */

    public DAOServletUser() {
        super();
    }

    public IDAOUser user;

    /**
     * Create the entity and persist it.
     */
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        logger.log(Level.INFO, "Creating User!!!");
        logger.info("Request: " +  req.toString());

        if(resp==null) {
            System.out.println("Respond is NULL!!!");
        }

        PrintWriter out = resp.getWriter();//Here I get the null Pointer exception



        String email = req.getParameter("email");

        if(email==null) {
            System.out.println("email is null");
        } else {
            System.out.println("email is NOT null");
        }
        String password = req.getParameter("password");
        if(password==null) {
            System.out.println("password is null");
        } else {
            System.out.println("password is NOT null");
        }
        logger.info(email + "::" + password);
        try {
            user.insert(email, password);
            Log.info("Inserted: " + email + "    " + password);
        } catch (Exception e) {
            String msg = DAOUser.getErrorMessage(e);
            out.print(msg);
        }
    }
4

1 に答える 1

2

私はIDAOUserインターフェースであると仮定しています。Java でインターフェイスを実装するには、次のimplementsようにクラス レベルでキーワードを使用します。

public class DaoUserImpl implements IDAOUser {
    public void insert(String email, String password) {
        // your code for inserting goes here
    }
}

サーブレットクラスでは、代わりに

public IDAOUser user;

このようにインスタンス化します(newキーワードを使用)

public IDAOUser user = new DaoUserImpl();

user.insert(email,password)ではないオブジェクトを呼び出すことができnullます。

ただし、DaoUserImpl が共有インスタンス データを使用している場合、このソリューションで発生する可能性があるマルチスレッドの問題に注意する必要があります。リクエストごとにクラスのインスタンスが 1 つ必要になる場合があります。

于 2013-03-22T23:25:50.580 に答える