File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "NullPointerException" Watch "NullPointerException" New topic
Author

NullPointerException

Ron Rea
Greenhorn

Joined: Jul 03, 2001
Posts: 23
The following code is giving me a NullPointerException and I cannot figure out why. Comments in the code point to where the error appears to happen (watched while running through the debugger). Anyone have any ideas:
try
{
while(companyProfileResultSet.next())
{
ResultSet rolloutResultSet = null;

if (companyProfileResultSet.equals(null))
{
cat.debug("companyResultSet = null");
}

//*********This is executed without a problem **********
String hpAccountStr = companyProfileResultSet.getString("HPAccount");

//***This line appears to cause a NullPointerException
//(goes to Catch) ****************
if (hpAccountStr.equals(null))
{
cat.debug("stop here");
}

String companyName = " ";
String phoneBusiness = " ";

if ((!(hpAccountStr.equals("")))
& !(hpAccountStr.equals("No HPAccount")))
{
//Set up and select from the Rollout table.
StringBuffer rolloutBuf = new StringBuffer();
rolloutBuf.append("SELECT company, phonebusiness ");
rolloutBuf.append("FROM Rollout ");
rolloutBuf.append("WHERE cbn = '" + hpAccountStr + "'");
sqlRolloutStatement = rolloutBuf.toString();

// Read the Rollout table
rolloutStatement.executeQuery(sqlRolloutStatement);

// Move the results into a Rollout results set
rolloutResultSet = rolloutStatement.getResultSet();

if(rolloutResultSet.next())
{
companyName = checkSingleQuote(rolloutResultSet.getString("company"));
phoneBusiness = checkSingleQuote(rolloutResultSet.getString("phonebusiness"));
}
else
{
//No matching Rollout row was found
companyName = null;
phoneBusiness = null;
}

}


// Insert data into the Partner table
StringBuffer insertBuf = new StringBuffer();

String hpOfficeNumberStr = companyProfileResultSet.getString("HPOfficeNumber");
cat.debug("Office Number: " + hpOfficeNumberStr);
String vendorIdStr = companyProfileResultSet.getString("CnyVendorId");
String companyContactNameStr = checkSingleQuote(companyProfileResultSet.getString("Company
int companyTypeInt = mapCustomerType(companyProfileResultSet.getString("CompanyType"));
String hppaStr = companyProfileResultSet.getString("HPPA");
String companyCurrencyStr = companyProfileResultSet.getString("CompanyCurrency");
if (!(companyCurrencyStr.equals(null)))
{companyCurrencyStr = companyCurrencyStr.substring(0, 4);}
String companyCountryStr = companyProfileResultSet.getString("CompanyCountry");
if (!(companyCountryStr.equals(null)))
{companyCountryStr = companyCountryStr.substring(0, 3);}

insertBuf.append("INSERT INTO partner ");
insertBuf.append("(hp_office_number, vendor_id, contact_name, ");
insertBuf.append("customer_number, currency_code, company_name, contact_phone, ");
insertBuf.append("country_code, customer_type_id, administrator_id, ");
insertBuf.append("hp_purchase_agreement_code, created_date, region_id)");
insertBuf.append(" VALUES ('");
insertBuf.append(hpOfficeNumberStr + "', '");
insertBuf.append(vendorIdStr + "', '");
insertBuf.append(companyContactNameStr + "', '");
insertBuf.append(hpAccountStr + "', '");
insertBuf.append(companyCurrencyStr + "', '");
insertBuf.append(companyName + "', '");
insertBuf.append(phoneBusiness + "', '");
insertBuf.append(companyCountryStr + "', '");
insertBuf.append(companyTypeInt + "', ");
insertBuf.append("0, '");
insertBuf.append(hppaStr + "', ");
insertBuf.append("date(), ");
insertBuf.append("2) ");

sqlInsertStatement = insertBuf.toString();
// Insert into the Partner table.
insertStatement.executeUpdate(sqlInsertStatement);


}
//Does not get executed
cat.debug("Done*****");
}
catch (SQLException sqlException)
{
throw new ConvertWrefException("SQL Exception on convertPartner" + sqlException.toString());
}
//**This is where the NullPointerException takes place**
catch(Exception exception)
{
cat.error("error inserting Partner;" + exception.toString());
}
Mohamed Yousuff
Ranch Hand

Joined: Jun 23, 2001
Posts: 73

The bug seems in this part of your code
if (hpAccountStr.equals(null))
{
cat.debug("stop here");
}
It should be actually
if ( hpAccountStr == null ){
...
}

------------------
Ron Rea
Greenhorn

Joined: Jul 03, 2001
Posts: 23
Changed the code to ". . . == null" like you suggested, worked great! Thanks somuch for your help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException