![](/img/trans.png)
[英]Null Pointer when populating Grails Domain object with Oracle 10g long field
[英]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.