Java: run time exceptions

Discussion in 'Mac Programming' started by happyElephant, Mar 1, 2007.

  1. macrumors member

    Joined:
    Oct 27, 2006
    #1
    What could be the cause of such a message:

    java.lang.NoSuchMethodError: mainException in thread "main" ?
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You've tried to call a method on an Object that does not support that method most likely.
     
  3. thread starter macrumors member

    Joined:
    Oct 27, 2006
    #3

    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());
    }
    }
     
  4. macrumors newbie

    Joined:
    Jul 31, 2006
    Location:
    Huntsville Al
    #4
    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.
     
  5. thread starter macrumors member

    Joined:
    Oct 27, 2006
    #5
    :) 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?
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    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?
     
  7. thread starter macrumors member

    Joined:
    Oct 27, 2006
    #7
    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?
     
  8. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #8
    First put each class into a seperate file.

    Then put import className; at the top to import the other class files.
     
  9. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #9
    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
     
  10. thread starter macrumors member

    Joined:
    Oct 27, 2006
    #10
    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.
     
  11. thread starter macrumors member

    Joined:
    Oct 27, 2006
    #11
    I will check if that's the case.
     
  12. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #12
    Seperate them all, (they need to be in the same folder though).
     
  13. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #13
    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...
     
  14. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #14
    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
     
  15. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #15
    RobbieDuncan is almost certainly right, the only Java I have with multiple files is in a package thanks to Eclipse.
     
  16. thread starter macrumors member

    Joined:
    Oct 27, 2006
    #16
    It's called : TwoDShape. I changed the filename to "Shapes"&it worked!!!

    thank you!!!


    and thanks everybody who tried to help me :)
     

Share This Page