• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems with Constructors and Drawing a Car

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I was attempting to write a code which would create a car class. Then display two cars on a screen. The problem is I keep getting a couple errors.
It seems that the car constructor is not being created before the CarComponent class creates the object. But I don't really know what to do..... Thanks!

___________________________________


_________________________________________________________
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mathew, welcome to Java Ranch!

First, please use code tags when posting code samples. They make the code much easier to read. I've added them for you above.

Now, what kind of errors are you getting? Compile errors, run time exceptions, wrong behavior? On a quick scan, I see you defined your Car class with a capital C, but you've written the constructor with a lowercase c. The constructor name has to match the class name exactly, even the case. That mismatch will cause a compile error. If it still doesn't compile after you fix that, post the messages the compiler gives you and we'll try to figure it out together.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to what Greg said, don't construct new Cars in the paintComponent(...) override. Painting methods are for painting, and painting alone. You have absolutely no control over how often or how many times that method will be called.
 
Mathew Dodge
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to both of you! You're more helpful than my programming professor.... It was the lower case letter in the car constructor which was causing the error. That was something that I just wouldn't have seen! Thank you both again!
 
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic