Results 1 to 7 of 7

Thread: Run time errors

  1. #1
    crabman is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default Run time errors

    Ok this is an exercise I have been given to learn about run time errors. The program I have been given has purpose mistakes so that the program compiles but doesn't run. I need to explain the error and fix it accordingly. This is how it looks

    001: public class Quote
    002: {
    003: public void Main(String args)
    004: {
    005: System.out.println("Programming is about making the stupid seem clever.");
    006: System.out.println(" - ^ - ");
    007: System.out.println( 0 / 0 );
    008: System.out.println(" = ");
    009: System.out.println(" (At least, to the dumb user!) ");
    010: }
    011: }


    The run time error says:

    Exception in thread "main" java.lang.NoSuchMethodError: main


    Does this mean the program is missing a main method? (as the code says "Main" rather than "main")

    Changes I have made to fix it:

    003: public static void main(String[] args)

    007: System.out.println(" 0 / 0 ");


    The program now works but are there any parts i have added that arn't required?
    e.g. do i need the word "static" in there? If so why?



    very new to this stuff if you didn't guess ;)


    *the correct spacing hasn't come out properly in the post, im also new to the forum ;)*

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Yep you added the required main method.
    You do need that static method there. The main method is rather special because you don't actually invoke it yourself. The guy who calls it (the JVM) wants it that way so he can find it and call it easily without needing to worry about creating an instance of your class.

  3. #3
    crabman is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default

    So what about the square brackets after "String". I know they are required because i tried the program without them. But why do they actually do? And what happens when i add the quotations on line 7? thanks for helping me

  4. #4
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Without the brackets you just have a string, with the brackets you have an array of strings. So for 'String[] args' you have an array called 'args', that is args[0], args[1] ... (Well, the size for the Main method depends on how many arguments you actually have when starting the program). For further info on arrays see

    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by pellebye View Post
    .. (Well, the size for the Main method depends on how many arguments you actually have when starting the program). ..
    You mean the size of the args array instead?

  6. #6
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Thats right!
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  7. #7
    Vio
    Vio is offline Member
    Join Date
    Oct 2009
    Location
    Belgium
    Posts
    18
    Rep Power
    0

    Default

    And the quotations make that everything between the quotes is shown literally on the screen except for escape characters.

Similar Threads

  1. Class Time - represents time of day
    By verbazon in forum New To Java
    Replies: 1
    Last Post: 04-13-2009, 02:06 AM
  2. Errors.
    By rocky in forum New To Java
    Replies: 4
    Last Post: 04-09-2009, 09:05 AM
  3. Replies: 3
    Last Post: 03-08-2009, 02:51 AM
  4. Help with Compile time errors
    By bri1547 in forum New To Java
    Replies: 2
    Last Post: 08-25-2008, 12:22 AM
  5. Replies: 21
    Last Post: 08-01-2008, 12:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •