Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

cannot instantiate the type

 
Mike Lomman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Java. I use the eclipse to write the code and test it.

I have searched endlessly it seems for a solution to my issue.
I have also contacted the school for support but they are often tardy with their response(well they have been in the past).

I am creating two classes, and have an error 'cannot instantiate the type' and don't know why nor how to fix it.


The first class ParkingSpace




The second class ParkingLot





Line 27 in the  ParkingLot class gives me the "cannot instantiate the ParkingSpace type" error.

I imagine that this error is causing a few other errors further along in the code. And I have yet to complete other parts of the code.

Any assistance on this error is appreciated.


Many thanks

Mike
 
Campbell Ritchie
Marshal
Posts: 66975
255
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welome to the Ranch

You have provided lots of useful information, more than some people do, but I can't just offhand see the cause of your problem. Let's try reformatting your code because your lines are so long that I can't read them as one piece:-. . . That's a bit better,, but there are still things to improve.
Don't use so many empty lines. If you want to separate paragraphs use one empty line.
Be more consistent with where you haev spaces. Don't write a space after ( but do put a space after a < or every comma.
Only use // comments for something very short, or for commenting‑out. For anything longer, use /* comments */
Call a method returning a boolean something different; you should write isOccupied() rather than getOccupied(); Eclipse can change that for you with its refectoring capabilities.
The old Sun style guide suggests, don't use that if‑else in your lines 22‑26; Write:-Yuo will also find a section in the same style guide about line lengths.
Don&apost use tabs for indenting; get Ecipse to convert a tab to four spaces automatically.
There is something iffy about your casting those numbers to a char and I don't think it will do what you want. Don'r hard‑code 65, which is error‑prone. If you have to hard‑ode anything, use 'A'. That will give you floors starting at B, and strange floor numbers if you have more than 25 of them.
It is not obvious what ariables called s and c mean.
The > which appeared mysteriously in line 19 is probably a problem with our forum software.
Your setOccupied() method is written incorrectly; it won't do what you want.
I don't like the overloaded constructors, particularly if you pass small ints which get cast to control characters or \n or similar.

I still can't see why line 28 won't compile. I don't like the look of line 58. Nor did the compiler. You are missing () in line 65, but I still can't see how you are getting that error in line 28.
 
Mike Lomman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many Thanks for your rapid response.  shall read them carefully and take on board your valuable tips.

As far as naming the methods, in this particular instance the names have been provided, but I see your point  is useful in general

I used the overloaded constructor in ParkingSpace to see whether it solved my issue, it didn't and I forgot to remove it.

I will read through the links you have provided and continue my search.

Again many thanks for your response.

Mike
 
Campbell Ritchie
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Lomman wrote:Many Thanks . . .

That's a pleasure

in this particular instance the names have been provided, . . .

In that case you will have to use those names but the usual convention is for methods returning a boolean to be called isXXX() or hasXXX() or canXXX() or similar; isXXX() is the commonest name.
 
Carey Brown
Saloon Keeper
Posts: 6588
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When I tried to compile your code I get lots of compiler errors, the first one being the right-angle-bracket on line 21. I don't get an error on line 30 though. The creation of a new ParkingSpace in main() is totally wrong though.

I suspect your code listing is not in sync with your error message(s). I would suggest fixing as many of the other errors as you can and if you are still stuck, repost a current copy of the code with a complete cunt-and-paste of ALL of the error messages.
 
Campbell Ritchie
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the gremlin who mysteriously adds < characters to code has been at it again.
 
Mike Lomman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your helpful comments.

Yes there are other errors in my code.

The instantiation error has gone.

My guess is the way the classes were saved caused the issue, as I resaved them both and the error as I said is gone.

Thank you to the JAVA Gods, and you lovely people for your assistance.

I have copied your comments about better code writing so I can learn them.

Many thanks

Mike
 
Mike Lomman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:
When I tried to compile your code I get lots of compiler errors, the first one being the right-angle-bracket on line 21. I don't get an error on line 30 though. The creation of a new ParkingSpace in main() is totally wrong though.

I suspect your code listing is not in sync with your error message(s). I would suggest fixing as many of the other errors as you can and if you are still stuck, repost a current copy of the code with a complete cunt-and-paste of ALL of the error messages.





The only error I had was the instantiation error.
The angle bracket somehow was inserted during my post, it isn't in my original code.

Since the error vanished and I guess it was how/where the class was saved that caused it I have been unable to continue as I can no longer see a path forward.
My erroneous assumption was that the code at line 18 would fill the array with values.
However I try to print those values does not work.

I am going fishing.

I am out of ideas.
 
Carey Brown
Saloon Keeper
Posts: 6588
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:


Mike Lomman wrote:My erroneous assumption was that the code at line 18 would fill the array with values.


Line 18 allocates an array of references to ParkingSpace objects and initializes all the all array elements to be null references.
 
Carey Brown
Saloon Keeper
Posts: 6588
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took some educated guesses. This at least compiles. main() doesn't do anything useful so I don't see how you'd test it.
 
Mike Lomman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey,
              don't the following lines
 
Campbell Ritchie
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Lomman wrote:. . . don't the following lines. . .  fill the array with values???

Yes, but I think there are far better ways to do it. Don't try using 65 as a char; that is error‑prone. Use 'A' + i, but that will fall over horribly if you have more than 26 floors, as I think we have told you already.
I suggest, don't iterate the entire inner loop; iterate the first five spaces for tiny spaces and then start again from 5 and iterate the remainder of the loop for large spaces. Alternatively, reduce lines 7‑11 to forCompactCars = s < 5;
Put some spaces between operators (as suggested here ยง1.2; page may need scrolling down); lines 13‑14 are very difficult to read.

[edit]Change link to formatting suggestions.
 
Those are the largest trousers in the world! Especially when next to this ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!