简体   繁体   English

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

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

I have a relation like this Create table ImageFile1(name varchar2(200), id number(30), Image BLOB);我有这样的关系Create table ImageFile1(name varchar2(200), id number(30), Image BLOB); which successfully created the table.成功创建了表。

But when iam trying to insert data using PreparedStatement, i got the problem as null pointer exception The code i used is..但是当我尝试使用 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();

and the output is: output 是:

Connection created0
Connection created1
Connection created2
Connection created

file ok文件正常

java.lang.NullPointerException

Please help to getrid of this...请帮助摆脱这个...

At which point are you getting Null Pointer exception?你在什么时候得到 Null 指针异常? Also, the line wherein you get the connection instance, con=(Connection)session.getAttribute("connection");此外,获取连接实例的行con=(Connection)session.getAttribute("connection"); , check to make sure the connection instance is not null. ,检查以确保连接实例不是 null。 Storing connection object in Session may not be a good approach.在 Session 中存储连接 object 可能不是一个好方法。 Either create connection using DriverManager class or better use a Connection Pool.使用DriverManager class 创建连接或更好地使用连接池。 Further, use some file uploading library like Apache Commons File Upload .此外,使用一些文件上传库,如Apache Commons File Upload There is enough documentation available for the library to make the image upload quite straightforward.该库有足够的文档可以使图像上传变得非常简单。

You want to be using a JDBCConnectionPool .您想使用JDBCConnectionPool See here for details.有关详细信息,请参见此处

For future reference, when asking about an exception it is best to tell us on what line the exception occurred.为了将来参考,当询问异常时,最好告诉我们异常发生在哪一行。 In this particular case, it is pretty clear that it's this line:在这种特殊情况下,很明显就是这一行:

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

and the problem is that your con is null.问题是你的con是 null。 So you need to look at where that con is coming from and how that gets populated.所以你需要看看那个con来自哪里以及它是如何被填充的。 But in other circumstances, it is hard to infer from the code what the problem might be.但在其他情况下,很难从代码中推断出问题可能是什么。

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

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