Home>

I am a beginner.

When I execute the following code in my reference book with eclipse,
I got the following error message, and I investigated various causes on the Internet, but I'm having trouble.

Why is the itemDAO class returning and writing the returned item type ArrayList items as "return items;"?

Thanks for your professor.

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
This method must return a result of type ArrayList

at ItemDAO.findByMinimumPrice(ItemDAO.java:9)
at Main.main(Main.java:5)

import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
        System.out.println("Display item list of 1 yen or more");
        ArrayList<Item>items = ItemDAO.findByMinimumPrice(1);
        for(Item item :items) {
            System.out.printf("%10s%4d%4d", item.getName(), item.getPrice(), item.getWeight());
        }
    }
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class ItemDAO {
    public static ArrayList<Item>findByMinimumPrice(int i) {
        try {
            Class.forName("org.h2.Driver");
        }catch(ClassNotFoundException e) {
            e.printStackTrace();
        }
        Connection con = null;
        try {
            con = DriverManager.getConnection("jdbc:h2:~/example", "sa", "");
            PreparedStatement pstmt = con.prepareStatement
                    ("SELECT * FROM ITEMS WHERE PRICE >?");
            pstmt.setInt(1, i);
            ResultSet rs = pstmt.executeQuery();
            // Prepare an ArrayList to put Items here
            ArrayList<Item>items = new ArrayList<Item>();
            while(rs.next()) {// process all rows of result table one by one
                Item item = new Item();
                item.setName(rs.getString("NAME"));/* One line of information */
                item.setPrice(rs.getInt("PRICE"));/* Convert to instance */
                item.setWeight(rs.getInt("WEGIHT"));
                items.add(item);/* Add instance to ArrayList */
            }
            rs.close();
            pstmt.close();
            return items;/* Finally return ArrayList */
        } catch(SQLException e) {
            e.printStackTrace();
        } finally {
            if(con != null) {
                try {
                    con.close();
                } catch(SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
public class Item {
    private String name;
    private int price;
    private int weight;
    public String getName() {
        return this.name;
    }
    public int getPrice() {
        return this.price;
    }
    public int getWeight() {
        return this.weight;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public void setWeight(int weight) {
        this.weight= weight;
    }
}
  • Answer # 1

    If a SQLException occurs,return items;I will not pass.

  • Answer # 2

    As already written,return items;The problem is that there are routes that do not pass.

    In Java, any method that has a return value must return the return value.
    For example, if you writeaBut0If:returnWithout passing through
    The method ends. In JAVA, such code will be rejected as an error.

    if(a<0){
      return 0;
    }

    In this case,tryIf an exception occurs in a block, skip the rest of the block,
    catch()OrfinallyProcessing will be skipped inside the block.
    For that reason,tryAt the end of the blockreturn items;Will be skipped and will not be executed.
    The JAVA compiler detects such a situation,return items;May not run, and
    I am giving an error.
    Even if an exception occursreturnPlease review so that you can return.