0

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!

laisbsc
  • 5
  • 6
  • I would imagine that `DataSource db = SingletonInstance.getInstance();` is null. What code is at `MySqlCountryDAO.java:28` ? – Scary Wombat Apr 01 '20 at 00:18
  • Most likely `db` is `null`. – Arvind Kumar Avinash Apr 01 '20 at 00:20
  • This isn’t good practice btw. It looks like you’re studying from obsolete material. – Nathan Hughes Apr 01 '20 at 00:30
  • @ScaryWombat line 28 is `while (rs.next()) {`. It seems like the ResultSet is not being populated with the table data. I don't really understand why, though. And @ArvindKumarAvinash, I might be wrong but the way I see it the db shouldn't be null because the resultSet 'rs' is holding the result from the query executed and returning it to the method getCountries(). – laisbsc Apr 01 '20 at 00:36
  • @NathanHughes care to elaborate? – laisbsc Apr 01 '20 at 00:40
  • I am guessing that `DataSource` is not javax.sql - so no idea what your code is doing. Esp. `db.select(query); ` – Scary Wombat Apr 01 '20 at 00:40
  • @ScaryWombat it's a java.sql. I didn't wanna add more code to the post cuz it can get quite confusing. The entire thing is on GitHub, though. But well, thanks for the prompt response. And I read the post you marked mine as similar. It doesn't really answer my question. I will see what else I can try tomorrow. Have been working on this for 4 days straight and not even a sign of a light. :'( – laisbsc Apr 01 '20 at 00:43
  • `it's a java.sql` - I am confused, please show the link to the javadocs. – Scary Wombat Apr 01 '20 at 00:47
  • Sorry but I don't know what you mean by "the link to the javadocs". The thing I can offer you is the entire sourceCode. I am confused, as well. Sorry. – laisbsc Apr 01 '20 at 00:51
  • @ScaryWombat sorry I did not understand your question. Thanks for your help. Appreciate it. Do you mind telling me what's wrong about the exception? Again, thank you very much for your time. – laisbsc Apr 01 '20 at 01:42
  • In `DataSource` `private ResultSet rs = null;` , then when there is an exception `while (rs.next() ){` - **NO** it is null – Scary Wombat Apr 01 '20 at 01:49
  • @ScaryWombat yes, you're right! thanks for that. But I'm still unsure about the singleton, how would you do the implementation? – laisbsc Apr 01 '20 at 02:09
  • seek and you find https://www.baeldung.com/java-singleton – Scary Wombat Apr 01 '20 at 02:13
  • @ScaryWombat thanks a million. I read that link beforehand but it doesn't really show a different implementation from the one I chose for the singleton, though. But again, thank you very much for your time and effort. – laisbsc Apr 01 '20 at 14:10

0 Answers0