简体   繁体   中英

What is this error? (Java database application)

I have the following code and I'm trying to return data from my database (itemName, itemId). Yet it gives me the following error:

Items() in Items cannot be applied to:
Expected         Actual
Parameters:      Arguments:

pool: com.sun.tools.javac.jvm.Pool     itemName (java.lang.String)
code: com.sun.tools.javac.jvm.Code itemId    (java.lang.String)
symtab: com.sun.tools.javac.code.Symtab     quantity (java.lang.String)
types: com.sun.tools.javac.code.Types     cost (java.lang.String)

Here is my code:

public List<Items> getItemList() throws SQLException{
    try {
        Statement statement = connection.createStatement();
        ResultSet results = statement.executeQuery("SELECT * FROM Items");
        {

            List<Items> itemsList = new ArrayList<>();

            while (results.next()) {
                String itemName = String.valueOf(results.getString("item_name"));
                String itemId = String.valueOf(results.getString("item_id"));
                Items items = new Items(itemName, itemId); // where the error is
                itemsList.add(items);
            }

            return itemsList;
        }

    } catch(Exception e){
        e.printStackTrace();
    }

    return null;
}

Is it because the types are incompatible (object v. string)? If so, String.valueOf... incorrect?

EDIT

public class Items {

// Constructor
public Items(String itemName, String itemId){
    setItemName(itemName);
    setItemId(itemId);
}

// itemName
private final StringProperty itemName = new SimpleStringProperty(this,"itemName");

public StringProperty itemNameProperty(){
    return itemName;
}

public final String getItemName(){
    return itemNameProperty().get();
}

public final void setItemName(String itemName){
    itemNameProperty().set(itemName);
}

// itemId
private final StringProperty itemId = new SimpleStringProperty(this,"itemId");

public StringProperty itemIdProperty(){
    return itemId;
}

public final String getItemId(){
    return itemIdProperty().get();
}

public final void setItemId(String itemId){
    itemIdProperty().set(itemId);
}


}

That looks like an issue that is caused by importing an Items class from the wrong package. If you have an import statement of the form

import some.package.name.Items ;

near the top of the class containing your first block of code, then remove that line.

If the class containing the first block of code is in a different package to your Items class, then you need to add in a line

import the.correct.package.Items ;

in place of the incorrect import statement. Replace the.correct.package with the name of the package containing your Items class (in Items.java , look at the first line of code, which is a package statement defining which package contains Items ).

For some background information, I recommend Meaning of the import statement in a Java file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM