Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

1365281

Suspended
Original poster
Oct 27, 2006
82
0
What could be the cause of such a message:

java.lang.NoSuchMethodError: mainException in thread "main" ?
 

1365281

Suspended
Original poster
Oct 27, 2006
82
0
You've tried to call a method on an Object that does not support that method most likely.


Would you, please, mind checking the code?


class TwoDShape{
private double width;
private double height;

double getWidth(){return width;}
double getHeight(){return height;}
void setWidth(double w){width=w;}
void setHeight(double h){height=h;}
void showDim(){
System.out.println("Width and height are "+width+" and "+height);

}
}
class Triangle extends TwoDShape{
private String style;

Triangle(String s, double w, double h){
setWidth(w);
setHeight(h);
style=s;
}
double area(){
return getWidth()*getHeight()/2;

}
void showStyle(){
System.out.println("Triangle is "+style);
}
}
class Shapes{
public static void main(String args[]){
Triangle t1=new Triangle("isosceles", 4.0, 4.0);
Triangle t2=new Triangle ("right", 8.0, 12.0);

System.out.println("Info for t1: ");
t1.showStyle();
t2.showDim();
System.out.println("Area is "+t1.area());
System.out.println();

System.out.println("Info for t2: ");
t2.showStyle();
t1.showDim();
System.out.println("Area is "+ t2.area());
}
}
 

hsvmoon

macrumors newbie
Jul 31, 2006
24
0
Huntsville Al
check for mistakes in casts

check your code for casting that is not correct. you might create an object cast it into a variable that is a super class to the object and then call a method only the child class has.

looking at your code make sure that you are giving the java command the correct class.

java Shapes

also none of the classes are inner class and as such should be in seperate java files.
 

1365281

Suspended
Original poster
Oct 27, 2006
82
0
check your code for casting that is not correct. you might create an object cast it into a variable that is a super class to the object and then call a method only the child class has.

:) I've just started learning inheritance. First day today. Your advise sounds a bit complicated. In how many steps should I be able to do this?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
The code looks OK as far as I can see by eye: I'm not about to compile and fix it especially as this looks like a very basic homework assignment. A very important part of programming is being able to debug your code.

Can you copy and paste exactly including the spacing (which is important as I believe you have removed a space or new line in your original post) the command you issue in the terminal and the entire response?
 

1365281

Suspended
Original poster
Oct 27, 2006
82
0
The code looks OK as far as I can see by eye: I'm not about to compile and fix it especially as this looks like a very basic homework assignment. A very important part of programming is being able to debug your code.

Can you copy and paste exactly including the spacing (which is important as I believe you have removed a space or new line in your original post) the command you issue in the terminal and the entire response?

No, it's not a homework. I'm studying on my own.

Don't laugh, but I don't know such a basic thing like how to copy the code with all the spaces. I'd like to learn it. What do I do?
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
First put each class into a seperate file.

Then put import className; at the top to import the other class files.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
You don't have a main() function. Well you do but the class TwoDShape needs a main(). When you run your program Java will look for a main() function in the main class. The main class is the one that has the same name as the program. Hence the error message you are getting.

b e n
 

1365281

Suspended
Original poster
Oct 27, 2006
82
0
First put each class into a seperate file.

Then put import className; at the top to import the other class files.

I separated class Shapes from the rest of two classes. I can compile them separately. However as soon as I type import class-name ,-compiler indicates errors.
 

1365281

Suspended
Original poster
Oct 27, 2006
82
0
You don't have a main() function. Well you do but the class TwoDShape needs a main(). When you run your program Java will look for a main() function in the main class. The main class is the one that has the same name as the program. Hence the error message you are getting.

b e n

I will check if that's the case.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I separated class Shapes from the rest of two classes. I can compile them separately. However as soon as I type import class-name ,-compiler indicates errors.

If they are in the same folder you don't need to import them. As they are not in a package you can't import them as you can only import package.something...
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Sorry happyElephant, I was assuming your file is called TwoDShape.java

Looking at your code again I think the problem is is that your file should be called Shapes.java. What did you call it?

b e n
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
If they are in the same folder you don't need to import them. As they are not in a package you can't import them as you can only import package.something...

RobbieDuncan is almost certainly right, the only Java I have with multiple files is in a package thanks to Eclipse.
 

1365281

Suspended
Original poster
Oct 27, 2006
82
0
Sorry happyElephant, I was assuming your file is called TwoDShape.java

Looking at your code again I think the problem is is that your file should be called Shapes.java. What did you call it?

b e n
It's called : TwoDShape. I changed the filename to "Shapes"&it worked!!!

thank you!!!


and thanks everybody who tried to help me :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.