-2

So I am ensure of why I am getting this error. In my courtController class it is returning ArrayList type. Along with in my other class I am trying to store this same type. I do not know why they won't work. They are both the same class therefore of the same type. The only thing different is the object being CourtDAO. But I am not storing this as I am returning a datatype of Court.

public static ArrayList<Court> getCourtInfo(DatabaseConnection databaseConnection) {
        ArrayList<Court> court = new ArrayList();

        PreparedStatement ps = null;
        String sql = null;
        Connection conn = null;

        try {
            conn = ConnectionUtils.getConnection(databaseConnection);

            sql = "SELECT * FROM `court` order by courtType";

            ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                // It is possible to get the columns via name
                // also possible to get the columns via the column number
                // which starts at 1

                // e.g. resultSet.getString(2);
                Court courtInfo = new Court();
                courtInfo.setCourtNumber(rs.getInt("courtNumber"));
                courtInfo.setCourtName(rs.getString("courtName"));
                courtInfo.setCourtType(rs.getInt("courtType"));
                System.out.println("Found court info=" + courtInfo);
                court.add(courtInfo);
            }
        } catch (Exception e) {
            String errorMessage = e.getMessage();
            e.printStackTrace();
        } finally {
            DbUtils.close(ps, conn);
        }

        return court;

    }
@RequestMapping("/court/export")
    public String CourtWriteToFile() {
        try {
            //Create the path directory
            Files.createDirectories(Paths.get(PATH));
        } catch (IOException ex) {
            Logger.getLogger(CourtController.class.getName()).log(Level.SEVERE, null, ex);
        }
        ArrayList<Court> arrayCourt = new ArrayList();

        CourtDAO newCourt = new CourtDAO();
        ***//This is what is giving me the error of incompatible types***
        arrayCourt.add(CourtDAO.getCourtInfo((DatabaseConnection) conn));


        //Also write to the file when a new camper was added!!
        BufferedWriter bw = null;
        FileWriter fw = null;

        try {
            fw = new FileWriter(FILE_NAME, true);
            bw = new BufferedWriter(fw);

            String jsonString = getJson(newCourt);
            //bjm 20190917 Adding line break 
            bw.write(jsonString + System.lineSeparator());

            System.out.println("Done");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }

                if (fw != null) {
                    fw.close();
                }

            } catch (IOException ex) {

                ex.printStackTrace();

            }

        }
        return "users/list";
    }

It is the second piece of code in which it is telling me that there is incompatible types.

T.Cahill
  • 43
  • 5
  • what does CourtDAO.getCourtInfo() return? It must return type Court – Robb Oct 29 '19 at 14:29
  • 3
    `getCourtInfo()` returns a *list*. You need to assign the results to a list. `arrayCourt = CourtDAO.getCourtInfo();` – markspace Oct 29 '19 at 14:30
  • 1
    `new ArrayList()` – Don't do that, it's a [raw type](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). – MC Emperor Oct 29 '19 at 16:30

1 Answers1

0

getCourtInfo doesn't return Court, it returns ArrayList<Court>.

You probably want arrayCourt = CourtDAO.getCourtInfo((DatabaseConnection) conn);

Max Vollmer
  • 8,412
  • 9
  • 28
  • 43