• Post Reply Bookmark Topic Watch Topic
  • New Topic

cannot instantiate the the type Flower  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a very strange error in my project.
I have this class called Dataset, and in it's constructor I read a textfile (with the irisdataset in it, for those who know it)
For all 150 flowers, the sepal_length, sepal_width, petal_length, petal_width (doubles) and species (string) is read and saved in a variable.
Then I want to instantiate a Flower with these values, and add it to an ArrayList.
But when I try:
Flower flower = new Flower(sepal_length, sepal_width, petal_length, petal_width, species);
the compiler (Eclipse) gives an error and says:
Flower flower = new Flower(sepal_length, sepal_width, petal_length, petal_width, species);
The constructor of Flower is:

I hope you can help me with this

Van
 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to show how you're reading the values from the text file.

And welcome to the Ranch!
 
Koen Smit
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read the values in this way



And thanks, my friends could not help me with this, so I thought I'd give this forum a try ;)

Van
 
Ranch Hand
Posts: 36
Chrome Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quote] the compiler (Eclipse) gives an error and says:
Flower flower = new Flower(sepal_length, sepal_width, petal_length, petal_width, species); [/quote]

I'm a little confused... what is the error message?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the condition at line 33 ever true?
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that the complete code? I don't see that you're declaring the variables sepal_length, sepal_width, petal_length, petal_width, species anywhere. In which class is the method Dataset?
 
Koen Smit
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is the whole class Dataset, public Dataset(String name) is it's constructor.
I am not sure if the condition at line (line 33 in my previous post) is ever true, but I think it doesn't cause the error.
the compiler (Eclipse) gives an error and says:
Flower flower = new Flower(sepal_length, sepal_width, petal_length, petal_width, species);

I am sorry this should be:
Cannot instantiate the type Flower.
This is the whole class Flower



 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is right at the beginning of the Dataset class:
That means you're declaring a generic class, with Flower being the generic type placeholder (where you'd usually use T). You're getting a conflict between that and the actual class Flower. So when it tries to create a Flower instance, it's the wrong one, and it can't create an instance of a class that is undefined at this point.

Since there doesn't appear to be any reason for the class to be generic, just delete the <Flower> part.

(I edited your posts to make them much more readable. See UseCodeTags for details on how to do it yourself.)

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incidentally, there are much easier ways to parse the input string than that. I'd suggest looking at the String.split() method - that can split your string on a comma in a single call.
 
Koen Smit
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I read the data using the split method.
The problem seemed to be that I said public class Dataset<Flower>
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Koen Smit wrote:The problem seemed to be that I said public class Dataset<Flower>

That's right. Why did you do that? public class Dataset would work.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!