PDA

View Full Version : Java: run time exceptions




happyElephant
Mar 1, 2007, 08:16 AM
What could be the cause of such a message:

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



robbieduncan
Mar 1, 2007, 08:22 AM
You've tried to call a method on an Object that does not support that method most likely.

happyElephant
Mar 1, 2007, 09:29 AM
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
Mar 1, 2007, 09:32 AM
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.

happyElephant
Mar 1, 2007, 09:37 AM
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
Mar 1, 2007, 09:39 AM
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?

happyElephant
Mar 1, 2007, 09:45 AM
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
Mar 1, 2007, 10:02 AM
First put each class into a seperate file.

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

lazydog
Mar 1, 2007, 10:05 AM
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

happyElephant
Mar 1, 2007, 10:10 AM
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.

happyElephant
Mar 1, 2007, 10:16 AM
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.

Eraserhead
Mar 1, 2007, 10:22 AM
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.

Seperate them all, (they need to be in the same folder though).

robbieduncan
Mar 1, 2007, 10:26 AM
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
Mar 1, 2007, 10:28 AM
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
Mar 1, 2007, 10:36 AM
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.

happyElephant
Mar 1, 2007, 10:37 AM
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 :)