This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginner Question About Java Coding  RSS feed

 
I. Ron Nie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I was trying do an exercise that requires I create a class, with variables for height, length, and depth, and then write an application that would display those variables.

So I wrote a class --

and then wrote a script --


And when I tried to compile it, I kept getting error messages saying there was a problem with the "show.Attributes" part.

Can anyone tell me what I'm doing wrong? Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, and welcome.

a couple of things...

Please post the exact text of the error - that makes it MUCH easier for us to help you.

Second, when posting java source, please use the 'code' tags. there are little buttons that do things like Bold, Italic, etc. One of them drops in tags you can paste your java code between, which preserves the formatting, making it easier for others to read. I'll go ahead and put them in this post for you.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the problem with your Dresser class is that you close it too soon. the curly bracket '}' on line 5 ends the class definition, so your showAttributes() method is not contained in any class - which is not allowed.

At the very least, you need to get rid of that bracket on line 5.
 
I. Ron Nie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. Sorry about the unformated code. I'll fix that in future texts.

I didn't include the error messages because there were three different ones. But I see what you are saying about the closed bracket, and I'll fix that and see what I get, and report back.

In future posts I'll include error messages properly.

I appreciate your help with my learning efforts. Ron
 
I. Ron Nie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. So I removed te offending close bracket in class Dresser. DresserApp compiled OK, but when I ran the script, I got the following error:

"Exception in thread main java.lang.NoClassDefFoundError: Dresser
"Caused by: java.lang.ClassNotFoundException: DresserApp
"at java.lang...
"at java.net...
"at java.security...
etc... for 3 more lines, then
"Could not find the main class: DresserApp

And yes, class Dresser, and DresserApp have been saved as .java files in the same folder in which I run the command prompt.

Any ideas? Thanks. jmp
 
Jim Newton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I notice that the 'not found' class is DresserApp, yet your code has a class called DresserAp ...
 
Mahi Kumawat
Greenhorn
Posts: 2
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rename DresserAp with DresserApp
 
I. Ron Nie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. Here is the code that is giving me error messages



And the error messages are as above:
"Exception in thread "main" java.lang.NoClassDefFoundError: DresserApp
"Caused by java.lang.ClassNotFoundException: DresserApp
"at java.net.URLClassLoader$1.run(URLClassLoader.java;202)
" etc, etc for three more lines
"Could not find the main class: DresserApp. Program will exit."

So it seems to be telling me it can't find the class Dresser (code displayed earlier in thread). And as I mentioned, the class Dresser and DresserApp are both in the same folder in which I am running the Command Prompt.

Any ideas? Thanks in advance.


Nida Brek wrote:I notice that the 'not found' class is DresserApp, yet your code has a class called DresserAp ...
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 1 has a typo: "DreserApp". Is that the cause, or just a transcription error?
 
I. Ron Nie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the name of the code.

In the first version I posted originally, I called it "DresserAp" and saved it as a file called "DresserAp.java"

In the second version I posted, I called it "DresserApp" and saved it as a file called "DresserApp.java" (after deleting the file "DresserAp.java")

The quoted error messages I get occur after typing "java DresserApp" in the command prompt.

Matthew Brown wrote:Line 1 has a typo: "DreserApp". Is that the cause, or just a transcription error?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I. Ron Nie wrote:In the second version I posted, I called it "DresserApp" and saved it as a file called "DresserApp.java" (after deleting the file "DresserAp.java")

No you didn't. In the second version you posted, you called it "DreserApp" - single "s". Is that error in the real file?
 
I. Ron Nie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. you guys are sharp!! I completely missed that!

I fix it and see what happens.

Thanks again. Ron.

Matthew Brown wrote:
I. Ron Nie wrote:In the second version I posted, I called it "DresserApp" and saved it as a file called "DresserApp.java" (after deleting the file "DresserAp.java")

No you didn't. In the second version you posted, you called it "DreserApp" - single "s". Is that error in the real file?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!