I am running the scheduler a cron job scheduler, which is running sql query on server and generate a report in excel format, for first time cron job is working fine when job trigger next it throws null pointer exception.
@Component
public class ToleranceReport {
Email email = new Email();
private static final Logger LOGGER = LoggerFactory.getLogger(ToleranceReport.class);
private String path = Constants.PATH + "Tolerance_Alert_Report.xlsx";
SXSSFWorkbook xlsxWorkbook5 = new SXSSFWorkbook();
SXSSFSheet xlsSheet5 = xlsxWorkbook5.createSheet();
int rowIndex5 = 1;
@Scheduled(cron = "30 40 8 * 1-12 4,5,6")
public void tolerance_Generator() throws IOException, SQLException, ClassNotFoundException, MessagingException {
File Tolerancefile = ResourceUtils.getFile("classpath:GE_Files/ToleranceReport/R1.txt");
String TolerancefirstPart = new String(Files.readAllBytes(Tolerancefile.toPath()));
// System.out.println(TolerancefirstPart);
try {
LOGGER.info("Starting Tolerance Report");
Class.forName("oracle.jdbc.driver.OracleDriver");
// step2 create the connection object
Connection con5 = DriverManager.getConnection(
"*****", "****", "*****");
{
LOGGER.info("Connected");
Statement stmt5 = con5.createStatement();
LOGGER.info("Executing");
// step4 execute query
ResultSet rs5 = stmt5.executeQuery(TolerancefirstPart);
ResultSetMetaData colInfo5 = rs5.getMetaData();
ArrayList<String> colNames5 = new ArrayList<>();
SXSSFRow titleRow5 = xlsSheet5.createRow(rowIndex5++);
for (int i = 1; i <= colInfo5.getColumnCount(); i++) {
colNames5.add(colInfo5.getColumnName(i));
titleRow5.createCell((int) (i - 1)).setCellValue(new XSSFRichTextString(colInfo5.getColumnName(i)));
xlsSheet5.setColumnWidth((int) (i - 1), (int) 16000);
}
while (rs5.next()) {
SXSSFRow dataRow5 = xlsSheet5.createRow(rowIndex5++);
int colIndex5 = 0;
for (String colName5 : colNames5) {
dataRow5.createCell(colIndex5++).setCellValue(new XSSFRichTextString(rs5.getString(colName5)));
}
}
FileOutputStream fo = new FileOutputStream(path);
xlsxWorkbook5.write(fo);
LOGGER.info("Exported Successfully");
rs5.close();
stmt5.close();
fo.close();
con5.close();
xlsxWorkbook5.close();
LOGGER.info("Closed");
}
email.sendMail(path, Constants.TOLERANCE_TO, Constants.CC, Constants.TOLERANCE_SUBJECT, Constants.EMAIL_CONTENT);
FileDelete.deletefileData(path);
} catch (IOException | SQLException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Error on this error this line SXSSFRow titleRow5 = xlsSheet5.createRow(rowIndex5++);
java.lang.NullPointerException
at com.ge.report.reports1st.FoodWaste.foodWaste(FoodWaste.java:313)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Could you please look into this and let me know why it is throwing, once it ran without any issues