• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

cannot instantiate the the type Flower

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the condition at line 33 ever true?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I knew that guy would be trouble! Thanks tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic