0

我正在从我的数据库中向注册学生发送一封电子邮件,并获取用户的电子邮件地址。并检查我数据库中的电子邮件,我正在使用 smtp 服务器向用户发送一封关于他的 prn(即 id)和密码的电子邮件。我有 1 个 servlet 和一个模型类和 1 个 jsp。我附上了代码的屏幕截图。谢谢,这是一个非常小的问题。请帮忙。我是学生。提前致谢。:) 我只收到 1 个错误,即 NullPointerException

package com.student.connection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Transport;
import javax.activation.*;

public class Mailer {

private Connection conn = null;
private PreparedStatement ps = null;
private Statement stmt = null;
private ResultSet rs = null;

public void send(String emailaddress) throws SQLException, ClassNotFoundException {
    int prn = 0;
    final String username = "12030142084@sicsr.ac.in";
    final String password = "mypassword";
    String email = new String();
    String pass = new String();

    System.out.println("Test");
    String query = "select prn,email,password from studentdetails where email=" + emailaddress;

    conn = DBConnection.createDBConnection();
    stmt = conn.createStatement();
    rs = stmt.executeQuery(query);

    while (rs.next()) {
        prn = rs.getInt(1);
        email = rs.getString("email");
        pass = rs.getString("password");
    }

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
    try {

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("12030142084@sicsr.ac.in"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(email));
        message.setSubject("Password Recover");
        message.setText("Your Prn is " + prn + "You Password is " + pass);

        Transport.send(message);

        System.out.println("Email Sent Succsessfully");

    } catch (MessagingException e) {
        //throw new RuntimeException(e);
    } finally {
        conn.close();
    }
}

}


package com.student.controller;

public class ContactServlet extends HttpServlet {

private Mailer mailer;

@Override
public void init() {
    String hostname = "smtp.gmail.com";
    int port = 587;
    String username = "12030142084@sicsr.ac.in";
    String password = "mypassword";

}

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        /* TODO output your page here. You may use following sample code. */
        String emailaddress = request.getParameter("emailadd");
        // Do some validations and then send mail:

        String msg = "";
        try {
            msg = "Mail Successfully Sent!!";
            System.out.println("Check");

            mailer.send("emailaddress"); // here is the error
            System.out.println("Check2");

            request.setAttribute("message", "Mail succesfully sent!");
            request.getRequestDispatcher("/WEB-INF/recoverpassword.jsp").forward(request, response);
            response.sendRedirect("index.jsp?error=" + msg);
        } catch (Exception e) {
            response.sendRedirect("recoverpassword.jsp" + msg);
            throw new ServletException("Mailer failed", e);
        }
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet ContactServlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet ContactServlet at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    } finally {
        out.close();
    }
}


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    processRequest(request, response);
}


@Override
public String getServletInfo() {
    return "Short description";
}

}


WARNING: StandardWrapperValve[ContactServlet]: PWC1406: Servlet.service() for servlet ContactServlet threw exception java.lang.NullPointerException
at com.student.controller.ContactServlet.processRequest(ContactServlet.java:50)
at com.student.controller.ContactServlet.doPost(ContactServlet.java:102)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)

Jsp文件是正确的,它只需要一个用户输入,id是emailadd

请有人帮忙。

4

2 回答 2

0

那是因为mailer还没有初始化。

private Mailer mailer; // you're just declaring mailer
...
mailer.send("emailaddress"); // without initialization you're trying to call a method on it.

在您的init()方法中,您需要执行以下操作:-

mailer = new Mailer(); // just an example. But you need to initialize your mailer.
于 2013-10-02T07:28:18.647 回答
0
        mailer.send("emailaddress"); // here is the error

将导致 NullPointerException 因为在您的 servlet 代码中没有您正在初始化mailer. mailer 对象被初始化为 null 并且在其上调用任何方法都会导致 NPE。确保在调用 send 或任何其他方法之前先初始化它。

于 2013-10-02T07:29:39.517 回答