• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with constructors  RSS feed

 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the exact same code that I took from a tutorial and there it ran fine but I cant get it to run...


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cyran Meriel wrote:This is the exact same code that I took from a tutorial and there it ran fine but I cant get it to run...


Please TellTheDetails.

Henry
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cyran Meriel wrote:This is the exact same code that I took from a tutorial and there it ran fine but I cant get it to run...
And why you can't get it to run? To formulate a right question is a very important task to acomplish.

P.S. I managed to run it and it works fine.
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but I'm so annoyed at the moment, watching the tutorial and seeing the exact same code run fine but I can't..
Line 3: The type Machine is already defined
Line 4: The value of the field Machine.name is not used
Line 5: The value of the field Machine.code is not used

When I try to run it I get:
Exception in thread "main" java.lang.NoSuchMethodError: träning.Machine.<init>(Ljava/lang/String;)V
at träning.Constructor.main(Constructor.java:35)
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many source files do you have out of this code you posted? 1 or 2?
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure I understand but I only have the class "Constructor" which consists of the code I posted in the package "träning". I have no duplicates or something.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hang on. I see you have two classes defined in total: one is "Machine" another is "Constructor". Above that you have defined package "träning".
If all this code is located in one source file - all should work fine. But if you have 1 class "Constructor" located in a source file "Constructor.java" and another class "Machine" in another source file - then you should get the problems you were mentioned earlier.
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
problem.jpg
[Thumbnail for problem.jpg]
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm worried and have no idea whats going on.. This should not be happening, there is no fault in the code or the source file. I only have one source file. Starting to think I have to reinstal java and eclipse...
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cyran Meriel wrote:I'm worried and have no idea whats going on.. This should not be happening, there is no fault in the code or the source file. I only have one source file. Starting to think I have to reinstal java and eclipse...
No need to worry. Give a try simple thing. Create source file "Constructor.java" again and copy you code to it again. Make sure all code is in 1 file and in the package you have defined. It should work as there are no problems with code itself.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a folder called träning.
Put the Constructor.java file in it, and make sure both classes are in that file.
Navigate to the träning folder.
Compile with javac Constructor.java
Navigate to one folder “up” with cd ..
Execute with java träning.Constructor

Tell us what happens.
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Cyran Meriel wrote:I'm worried and have no idea whats going on.. This should not be happening, there is no fault in the code or the source file. I only have one source file. Starting to think I have to reinstal java and eclipse...
No need to worry. Give a try simple thing. Create source file "Constructor.java" again and copy you code to it again. Make sure all code is in 1 file and in the package you have defined. It should work as there are no problems with code itself.


Thank you! I did this and in the process I spotted another source file "App2" that I never finished and it had both Machine and Constructor in it..Would never cross my mind to check the other classes because they have different names and I totally forgot what I wrote in App2..I definitely learned something from this
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Campbell Ritchie reminded me a good thing, to advice you to step away for a while from clever IDE's.
It is better to grasp console commands, so you'd have a chance better understand what is happening behind the scenes of IDE's.

Usual advice is to use a plain text editor:
for Windows "Notepad++"
for Mac "TextWrangler"
for other unix like systems "emacs", "vim" (these could be more difficult to understand, as they have lots of commands)

And then console compiler (Campbell Ritchie already showed you how to use it).
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:And Campbell Ritchie reminded me a good thing, to advice you to step away for a while from clever IDE's.


And this is such important advice that we have a couple of FAQs to address the issue.

EditorFaq <-- click

IdeFaq <-- click
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!