Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用jsp:usebean时出现空指针异常_Java_Jsp - Fatal编程技术网

Java 使用jsp:usebean时出现空指针异常

Java 使用jsp:usebean时出现空指针异常,java,jsp,Java,Jsp,使用由创建的对象时,我遇到空指针异常。对象已正确实例化,(运行正常)。但当我在Register类的静态方法中传递此创建的对象时,它会引发空指针异常 NewFile.htm <!DOCTYPE html> <html> <form action="NewFile.jsp"> Name<input type="text" name="textbox1"/> <br> ID<input type="text" name="tex

使用由
创建的对象时,我遇到空指针异常。对象已正确实例化,(
运行正常)。但当我在
Register
类的静态方法中传递此创建的对象时,它会引发空指针异常

NewFile.htm

    <!DOCTYPE html>
<html>
<form action="NewFile.jsp">
Name<input type="text" name="textbox1"/>
<br>
ID<input type="text" name="textbox2"/>
<br><select name="select1">
<option>India</option>
<option>France</option>
<option>Japan</option>

</select>
<br>
<input type="submit"/>

</form>
</body>
</html>
Employee.java

package com.psl;

import java.util.Arrays;

public class Employee {



    @Override
    public String toString() {
        return "Employee [name=" + name + ", id=" + id + ", country=" + country
                + "]";
    }

    private String name;
    private int id;
    private String country;


    public Employee() {
        // TODO Auto-generated constructor stub
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }


}
DBConnection.java

public class DBConnection {

private static Connection con ;
public static Connection establishConnection()
{try {

    Class.forName("com.mysql.jdbc.Driver");
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/database","root","root");
    } catch (Exception e) {
        // TODO: handle exception
    }   
return con;

}
}这是你的问题

Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/database","root","root");
} catch (Exception e) {
    // TODO: handle exception
}  
可能抛出了一个
异常
,您正在吞咽它,即没有处理它
con
保持为
null
,但返回它

DBConnection.establishConnection()
因此返回
null

你试着打电话

prepareStatement("insert into Person values (?,?,?)");
null上



代码看起来不错,但thr可能比获得
空指针异常的原因低两倍。
当您从方法
DBConnection.buildConnection()
获取
连接时,请检查该方法是否提供了正确有效的连接


另一件事可以是
Employee obj
obj
可以是
null
,当您从
.jsp
调用它时,请检查它。

让我们看看您的
建立连接
方法。在这种情况下,
obj
不能是
null
。OP正在从JSP传递
emp
,该JSP正在引用由
JspServlet
@SotiriosDelimanolis yes i knw dear创建的对象。仅提供抛出空指针exceptionOk的可能情况,那么这种情况就不可能了。是的,我已经包含了printstacktrace,现在我可以看到异常。sql jar文件没有放在web inf中,因此我得到了一个异常。@Sanjana总是注意显式处理异常。您的处理不必很激烈,但至少记录异常将大有帮助。
DBConnection.establishConnection()
prepareStatement("insert into Person values (?,?,?)");