I'm working on my first database project for school. I thought I was going along nicely, but ran into a null pointer exception and I'm not sure how to fix it in the situation I'm in. The assignment is to open and read an access database, and pull information from it to find the customer with the smallest balance.
package dbproject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MyFirstDB {
public static void main(String[] args)
{
ResultSet resultSet = null;
double totalPref = 0.0;
double lowestBalPref = 1000000;
String lowestBalCust = null;
double highestBalPref = 0.0;
String highestBalCust = null;
double totalBalance = 0.0;
ResultSet rs = null;
String convertTo = "";
double result = 0.0;
long lngCN = 0;
String query = null;
String url = "jdbc:ucanaccess://c:/cps/ms121.accdb";
try{
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection con = DriverManager.getConnection(url);
if(con!=null){
System.out.println("Connecton Successful!");
}
else{
System.out.println("Connection Unsuccesful!");
System.exit(0);
}
Statement statement = con.createStatement();
if(statement != null)
System.out.println("Statement is not null");
resultSet = statement.executeQuery("SELECT * FROM CUSTOMER where preferred = 'y'");
while(resultSet.next()){
// System.out.println(resultSet.getString("CustomerName"));
totalPref = totalPref + 1;
if(rs.getDouble("Balance") < lowestBalPref){
lowestBalCust = rs.getString("CustomerName");
// System.out.println(resultSet.getDouble("Balance"));
}
}
System.out.println("Total Preferred Customers is " + totalPref);
System.out.println("The Customer with the lowest balance is " + lowestBalCust);
if (con != null)
con.close();
}catch (Exception e)
{
e.printStackTrace();
}
}
}
These lines in particular seem to be causing the error:
if(rs.getDouble("Balance") < lowestBalPref){
lowestBalCust = rs.getString("CustomerName");