ArrayList <Court>无法转换为Court

时间:2019-10-29 14:26:27

标签: java arraylist incompatibletypeerror

所以我确定为什么会出现此错误。在我的CourtController类中,它返回ArrayList类型。在我的其他课程中,我正在尝试存储相同的类型。我不知道为什么他们不起作用。它们都是同一类,因此属于同一类型。唯一不同的是对象是CourtDAO。但是,由于要返回法院的数据类型,因此我没有存储该数据。

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";
    }

这是第二段代码,它告诉我类型不兼容。

1 个答案:

答案 0 :(得分:0)

getCourtInfo不返回Court,而是返回ArrayList<Court>

您可能想要arrayCourt = CourtDAO.getCourtInfo((DatabaseConnection) conn);