hi everyone. can any of you guide me asto how to avoid null pointer exception in the result set variable.i am using this function to get all data in mysql table in hashtable. how to i take the scenario if there was no data in the table at time of execution. ie. the first time the programe was executed with nothing in database. since the table has nothing and resultSet is is pointed to null gives null pointer exception. how can i deal with that. thanks in advance

static void readDatabase() throws Exception{
				// This will load the MySQL driver, each DB has its own driver
				// Setup the connection with the DB
				connect = DriverManager
								+ "user=sqldata&password=sqldatapw");

				resultSet = statement
				.executeQuery("select * from MYDATABASE.CUSTOMER");
		      CusList = (CustomerList)resultSet;
		      System.out.println("files reloaded.");
		 catch (Exception e) {
			throw e;
		} finally {

So much wrong with that method I don't even know where to begin...

1) don't make it static.
2) don't reference outside variables, certainly never do so without checking them for validity first.
3) use proper means to open a connection, don't put username and password into the url like that, certainly not unencrypted.
4) don't just rethrow exceptions like that
5) properly close all database entities, and in the same method you open them.
6) don't expose database entities to the outside world
7) don't think you can cast a resultset to something else

6) is what causes your NPE, but it's a symptom of a major design flaw rather than the actual problem.

if i dont put it to static i wont be able to use it in my edit dialogbox. its a big project and im a newby . making it work was the first task for me.i could correct the exceptions and other things later.right now the only probelm i am faced with is the resultset being null.it works perfect if and only if there is atleast one row in the table already.

jwenting is right, and you are wrong about not being able to use it in your dialogbox. But anyway, you can check if something is null like this:

if (resultset == null) {
   System.out.println("No records found");
} else {
   // do whatever it is you wanted to do with the resultset

(but please don't ignore jwenting's advice)

see when i first create or execute the class i need to use these function. here this is what i mean.

public class CusRec extends ViewElements

	private static Connection connect = null;
	private static Statement statement = null;
	private static PreparedStatement preparedStatement = null;
	private static ResultSet resultSet = null;
  public static void main(String[] args) throws Exception{

    try {
    } catch (Exception e) {}
    String title = "Customer ";
    JFrame main = new CusRec(title);
    Toolkit thKit = main.getToolkit();
    Dimension wndSze = thKit.getScreenSize();
    int wd = main.getWidth();
    int ht = main.getHeight();
    int x = (int)((wndSze.getWidth()/2)-(wd/2));
    int y = (int)((wndSze.getHeight()/2)-(ht/2));
  CusRec(String title) throws Exception{
    System.out.println("setting up user interface\ngathering resources.");
    Container cont = getContentPane();
    System.out.println("loading tables");

now lets say atm the database is created and there are no values in the table customer. so that mean when i do resultSet = select * from customer resultSet is still null.how do i cater on that.

oopss i guesss u guys were right. i missed the declaration thats why it was working. and umm yeah i cant cast resultSet lol. can u tell me how to read the table into my hashtable.

This article has been dead for over six months. Start a new discussion instead.