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" ?
 
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());
}
}
 
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.
 
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?
 
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?
 
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?
 
First put each class into a seperate file.

Then put import className; at the top to import the other class files.
 
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
 
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.
 
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.
 
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...
 
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
 
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.
 
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.