mardi 31 mars 2020

Can't implement DAO design pattern in Java application [duplicate]

I'm a newbie in Java and I'm trying to design an application that has methods to fetch data from a database getCountries(), a method that finds countries in the table by searching for an input ID value findCountryByCode(int codeID) and finally, a method that saves new countries to the table called saveCountry(Country countries). I am asked to use all the good practices of OOP and design patterns in this exercise.

I have several classes managing the data encapsulation but I got stuck trying to debug some output errors. The class that connects to the database MySqlCountryDAO establishes a connection but not only doesn't return any table values on the terminal, instead, I'm prompted with a "java.lang.NullPointerException". I did my research but could not find out why this error appears there. Would anyone be able to have a look at this class and give me a light, please? Here is the code for the class that connects to the DataSource class [i.e. the database connector].

public class MySqlCountryDAO implements CountryDAO {

DataSource db = SingletonInstance.getInstance();

/**
 * Returns an ArrayList of all the Countries in the database
 * @returns arrayList of country Objects
 */
@Override
public ArrayList<Country> getCountries() {

    ArrayList<Country> countries = new ArrayList<Country>();
    String query = "SELECT * FROM country"; // MySQL query
    ResultSet rs = db.select(query); // catch the ResultSet and place the result of the query in the result set

    int code = 0;
    String name = "";
    Continent continent = null;
    long surfaceArea = 0;
    String headOfState = "";
    Country c = null;

    // loop over the resultSet to fill ArrayList w results
    try {
        while (rs.next()) {
            code = rs.getInt(1); // don't quite get it why starts at 1
            name = rs.getString(2);
            continent = Continent.valueOf(rs.getString(3));
            surfaceArea = rs.getLong(4);
            headOfState = rs.getString(5);

            //builder pattern to be implemented here
            c = new Country(code, name, continent, surfaceArea, headOfState); //new instance of Country class
            countries.add(c);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return countries;
}

}}

And the main class:

public class Main{

public static void main (String[] args){
    /**
     * instance of the client connecting to the DataSource class
     */
    MySqlCountryDAO dao = new MySqlCountryDAO(); //instance of dao

    ArrayList<Country> countries = dao.getCountries();
    for (Country c : countries){
        System.out.println(c);
    }
}
}

Below, the output returned when I run the code. SQL Exception: java.lang.NullPointerException Exception in thread "main" java.lang.NullPointerException at MySqlCountryDAO.getCountries(MySqlCountryDAO.java:28) at Main.main(Main.java:11)

I don't know what is causing it nor how to debug it. And there are a lot of other features I want to implement on it but I can't test them until the main app runs. Would really appreciate inputs to fix the problem and welcome suggestions to improve the program's structure as well.

Link for GitHub with the complete project is here.

Thanks a million!

Aucun commentaire:

Enregistrer un commentaire