Click to See Complete Forum and Search --> : getting NullPointerException, but not sure how or why


jrthor2
10-20-2010, 01:22 PM
I have the below code, with System.out.println for debugging, and when the code runs, it is only getting to #2 of my System.out.println statements, then gets a NullPointerException on the line in bold (the finally clause). How can it be getting this nullpointer when it only got to #2 of my debugging statements? It didn't even try to make a connection to my database yet. Could someone help me work this out?

private static void compareNexgenData(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
Class.forName(databaseDriver).newInstance();

String spreadsheetNexgenLastName = null;
int spreadsheetCustomerNumber = 0;
String spreadsheetNameMatch = null;
String databaseNameValue = null;
String databaseLastName = null;
Connection connection = null;
PreparedStatement preparedStatement = null;

try {
File file = new File(args[3]);
System.out.println("1");
InputStream inputStream = new FileInputStream(file);
System.out.println("2: " + file.toString());
Workbook wb = WorkbookFactory.create(inputStream);
System.out.println("3");
Sheet sheet = wb.getSheetAt(0);
System.out.println("4");

connection = DriverManager.getConnection(databaseConnectionString, args[1], args[2]);
System.out.println("5");
preparedStatement = connection.prepareStatement("select VALUE from xxx where CUSTOMER_NBR = ? and ATTRIBUTE_CODE = 'NAME'");
System.out.println("6");

for (Row row : sheet) {
System.out.println("7");
Cell spreadsheetNameMatchCell = row.getCell(8);
System.out.println("8");
spreadsheetNameMatch = spreadsheetNameMatchCell.getRichStringCellValue().getString().toLowerCase().trim();
System.out.println("spreadsheetNameMatch: " + spreadsheetNameMatch);

if (spreadsheetNameMatch.contains("no")) {
Cell spreadsheetCustomerNumberCell = row.getCell(0);
spreadsheetCustomerNumber = Integer.valueOf(spreadsheetCustomerNumberCell.getRichStringCellValue().getString());
System.out.println("*****spreadsheetCustomerNumber: " + spreadsheetCustomerNumber + "*****");

Cell spreadsheetNexgenLastNameCell = row.getCell(6);
spreadsheetNexgenLastName = spreadsheetNexgenLastNameCell.getRichStringCellValue().getString().toLowerCase().trim();
System.out.println("*****spreadsheetNexgenLastName: " + spreadsheetNexgenLastName + "*****");

preparedStatement.setInt(1,spreadsheetCustomerNumber);

ResultSet resultSet = preparedStatement.executeQuery();

if (resultSet != null) {
while (resultSet.next()) {
databaseNameValue = resultSet.getString("VALUE").toLowerCase();
System.out.println("*****databaseValue: " + databaseNameValue + "*****");

//get the last name from the returned database column value
if (databaseNameValue.indexOf("lastname") > 0) {
int lastNameStartIndex = databaseNameValue.lastIndexOf("lastname");
databaseLastName = databaseNameValue.substring(lastNameStartIndex + 9).toLowerCase().trim();
}
System.out.println("*****databaseLastName: " + databaseLastName + "*****");
}
//Compare databaseNameValue last name to spreadsheetLastName
if (spreadsheetNexgenLastName.equalsIgnoreCase(databaseLastName)) {
System.out.println("Customer Number " + spreadsheetCustomerNumber + ": Data entered matches Nexgen");
System.out.println("\n");
Cell newCell = row.getCell(9);
newCell.setCellValue("Data entered matches Nexgen");
} else {
System.out.println("Customer Number " + spreadsheetCustomerNumber + ": Data entered DOES NOT matches Nexgen");
System.out.println("\n");
Cell newCell = row.getCell(9);
newCell.setCellValue("Data entered DOES NOT matches Nexgen");
}
} else {
System.out.println("resultSet is null");
}
}
}
inputStream.close();
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream(file + ".new");
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException fnfe) {
System.out.println("FileNotFoundException");
fnfe.printStackTrace();
} catch (SQLException sqle) {
System.out.println("SQLException selecting row for customer number: " + spreadsheetCustomerNumber);
sqle.printStackTrace();
} catch (InvalidFormatException ife) {
System.out.println("InvalidFormatException");
ife.printStackTrace();
} catch (IOException ioe) {
System.out.println("IOException");
ioe.printStackTrace();
} finally {
connection.close();
}
}

Thanks

jrthor2
10-20-2010, 02:41 PM
I think i got this fixed. I was missing a .jar file in my project.