繁体   English   中英

Null 在 Oracle 10g 中上传图片时出现指针异常

[英]Null Pointer Exception while uploading images in Oracle 10g

我有这样的关系Create table ImageFile1(name varchar2(200), id number(30), Image BLOB); 成功创建了表。

但是当我尝试使用 PreparedStatement 插入数据时,我遇到了问题,因为 null 指针异常我使用的代码是..

        Connection con=null;
        System.out.println("Connection created0");
        Statement stmt=null;
        System.out.println("Connection created1");
        ResultSet rs=null;
        System.out.println("Connection created2");

        con=(Connection)session.getAttribute("connection");
        System.out.println("Connection created");



        File imgfile = new File("C:\\Users\\HP\\Pictures\\PALLU.jpg");

        System.out.println("*******");
        FileInputStream fin = new FileInputStream(imgfile);
        System.out.println("file ok");

        PreparedStatement pre = con.prepareStatement("insert into ImageFile1 values(?,?,?)");
                System.out.println("ps ok");
        pre.setString(1,"Vijay");
        pre.setInt(2,1);
        pre.setBinaryStream(3,fin,(int)imgfile.length());
        System.out.println("image problem solved");
        pre.executeUpdate();
        System.out.println("Inserting Successfully!");
        pre.close();

output 是:

Connection created0
Connection created1
Connection created2
Connection created

文件正常

java.lang.NullPointerException

请帮助摆脱这个...

你在什么时候得到 Null 指针异常? 此外,获取连接实例的行con=(Connection)session.getAttribute("connection"); ,检查以确保连接实例不是 null。 在 Session 中存储连接 object 可能不是一个好方法。 使用DriverManager class 创建连接或更好地使用连接池。 此外,使用一些文件上传库,如Apache Commons File Upload 该库有足够的文档可以使图像上传变得非常简单。

您想使用JDBCConnectionPool 有关详细信息,请参见此处

为了将来参考,当询问异常时,最好告诉我们异常发生在哪一行。 在这种特殊情况下,很明显就是这一行:

        PreparedStatement pre = con.prepareStatement("insert into ImageFile1 values(?,?,?)");

问题是你的con是 null。 所以你需要看看那个con来自哪里以及它是如何被填充的。 但在其他情况下,很难从代码中推断出问题可能是什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM