0
我想從我的數據庫中的blob字段創建pdf。如果我沒有指定文件位置,它工作得很好:pdf生成並可讀。但是如果我指定一個文件位置,我會得到一個nullpointerexception
。PDF創建在指定位置不起作用
文件位置位於屬性文件中。
/**
* Deze methode zoekt een pdf in de database op pdfnaam
*
* @param name, de naam van de pdf
* @return maakt de pdf aan
*/
public void retrievePdf(int iddocument) {
Properties prop = new Properties();
String fileLocation = new String("");
FileOutputStream fos = null;
try {
// load a properties file
prop.load(new FileInputStream("props/config.properties"));
// get the property value and use it
fileLocation = prop.getProperty("FileLocation");
// verwijderen
System.out.println(fileLocation);
} catch (IOException ex) {
ex.printStackTrace();
}
try {
c = MySqlDAOFactory.getInstance().getConnection();
String sql = "select * from pdf where iddocument=?";
prest = c.prepareStatement(sql);
prest.setInt(1, iddocument);
rs = prest.executeQuery();
while (rs.next()) {
// create file with the filename from
// the db in the dir fileLocation
File file = new File(fileLocation, rs.getString("pdfname"));
//get the blob from the db
Blob blob = rs.getBlob("pdffile");
InputStream is = blob.getBinaryStream();
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// ...
}
byte [] buffer = new byte[(int)blob.length()];
int length = -1;
try {
while((length = is.read(buffer)) != -1) {
try {
fos.write(buffer, 0, length);
} catch (IOException e) {
// ...
}
try {
fos.flush();
fos.close();
} catch (IOException e) {
// ...
}
}
} catch (IOException e) {
// ...
}
return;
}
} catch (SQLException e) {
// ...
} finally {
MySqlConnectionFactory.getInstance().closeResultSet(rs);
MySqlConnectionFactory.getInstance().closeStatement(prest);
MySqlConnectionFactory.getInstance().closeConnection(c);
}
}
在propertyfile寫的是:FileLocation=reports
誰能給一個建議,爲什麼它好好嘗試工作?
請張貼堆棧跟蹤(其中行不北角發生?)。 – home
你確定你的屬性對象不是空的 - 跟蹤它? – home
堆棧:線程「main」中的異常java.lang.NullPointerException \t at be.leerstad.planningsdocumentgenerator.database.mysql.pdf.MysqlPdfDAO.retrievePdf(MysqlPdfDAO.java:153) –