-1

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


}
Blasanka
  • 21,001
  • 12
  • 102
  • 104
purpleman
  • 15
  • 1
  • 7
  • 3
    What are the expected types of the arguments of constructor? – Jean-Baptiste Yunès Jun 13 '17 at 13:54
  • public Items(String itemName, String itemId) – purpleman Jun 13 '17 at 13:56
  • 1
    Not sure where come that error format. so I will guess from what I understand from it that it expect 4 arguments, not two and seems to be other type than `String`, are you importing the good `Items` class ? – AxelH Jun 13 '17 at 14:05
  • try adding items to your creation of the array list like List itemsList = new ArrayList<>(Items); – Jason V Jun 13 '17 at 14:06
  • Why did you use `String.valueOf()`? What are the data type of table columns? – Blasanka Jun 13 '17 at 14:08
  • 2
    @JasonD what are you expecting from that ? Blasanka, indeed not necessary but not important either, it will just assure this is a String not in the pool – AxelH Jun 13 '17 at 14:08
  • 2
    am I the only one who has never seen a stacktrace in that fashion? – kekolab Jun 13 '17 at 14:09
  • @Gianluca I am going over tutorials, then trying to write it myself without assistance so it's definitely possible I messed up... – purpleman Jun 13 '17 at 14:15
  • @AxelH What do you mean by "importing the good Items class"? – purpleman Jun 13 '17 at 14:15
  • I think @AxelH right. You need to overload the `Items` constructor. – Blasanka Jun 13 '17 at 14:19
  • 1
    @purpleman, I don't think so. It must be your implementation of the JVM. Anyway, I don't ask what it is as you're going through tutorials. Anyway, some thoughts, some of which have already been pointed out by others: 1. It seems you are invoking the constructor with the wrong arguments, but, again, that stacktrace is quite wierd. Can you post the complete stacktrace? 2. String.valueOf is useless as ResultSet.getString already produces a string – kekolab Jun 13 '17 at 14:21
  • @Blasanka How do you do that? (newb) – purpleman Jun 13 '17 at 14:21
  • @purpleman If you can provide `Items` class implementation, we can make sure what's happening. – Blasanka Jun 13 '17 at 14:24
  • @Blasanka Edited the post – purpleman Jun 13 '17 at 14:28
  • @purpleman According to my knowledge `StringProperty` use in JavaFX. Is your program JavaFX? – Blasanka Jun 13 '17 at 14:47
  • @purpleman read [this](https://stackoverflow.com/questions/28060633/difference-between-simplestringproperty-and-stringproperty) and [this](https://stackoverflow.com/questions/31266498/when-to-use-stringproperty-over-string). – Blasanka Jun 13 '17 at 14:49
  • @Blasanka I created the classes first in Java (tutorial for inventory management) then I wanted to add an interface to it so I looked at tutorials for JavaFX.... and added that material to my existing classes (modifiying a bit). As I said, I'm a newb and a bit in over my head... :/ – purpleman Jun 13 '17 at 14:52
  • 1
    Can you check your imports? Have you accidentally imported the wrong `Items` class (in the class containing the first block of code)? – James_D Jun 13 '17 at 15:30
  • @James_D Is it this? import com.sun.tools.javac.jvm.Items THIS SOLVED IT! – purpleman Jun 13 '17 at 15:35
  • Yes, that line is the problem. – James_D Jun 13 '17 at 15:38
  • @James_D Could you explain to me like a five year old the reasoning behind this? – purpleman Jun 13 '17 at 15:39
  • @purpleman Added an answer. You can google "packages and imports in Java" for a basic explanation. A quick scan of [this one](https://www.tutorialspoint.com/java/java_packages.htm) suggests it may be good. – James_D Jun 13 '17 at 15:42
  • 1
    @Blasanka it has nothing to do with an overloading... he just import the wrong package. So indeed, the class don't find the constructor expected – AxelH Jun 13 '17 at 15:46
  • 1
    @purpleman the answer is exactly what I meant by a bad import of class ´Items´. – AxelH Jun 13 '17 at 15:47

1 Answers1

2

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

James_D
  • 201,275
  • 16
  • 291
  • 322
  • Didn't get the confirmation before I had to leave. It was my guess too. About the package, note that the statement could be missing if it is in the unnamed/default package. Then, it can't be imported. – AxelH Jun 13 '17 at 15:50
  • @AxelH Yes; sorry, didn't see your original comment. I was going to mention default packages too, but decided I didn't want to make it more complex... :). (It would seem unlikely that the OP put the first class in a named package and the `Items` class in the default package, which would be the only scenario where that was relevant, I think.) – James_D Jun 13 '17 at 15:53
  • I couldn't figure out it. It was a problem to me also! nice catch! – Blasanka Jun 13 '17 at 15:56
  • No need to apologize, I am cool with it ;) there is some good post asking how to import classes from that package, a link to one would remove the complexity but let you add that information before someone get that problem. But not sure this is really required here. – AxelH Jun 13 '17 at 15:57