File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes runtime error with main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "runtime error with main method" Watch "runtime error with main method" New topic
Author

runtime error with main method

Dirk Kok
Greenhorn

Joined: May 01, 2013
Posts: 2
hi there, ive been following the java learning path, and i came up with a (buggy that is :O) program that directly does special calculations, but ive come across a bug i cant seem to fix(this happens when executing, compiling works fine).
Source:

Runtime error(when using "java CompareNumbers"):

Help?

PS: Also the /n is not working as a line break, but i think i can figure that out later, but help is still appreciated
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Dirk Kok wrote:
Runtime error:

How are you running the application? How many parameters are you passing in? What is the actual command line you are using to launch the app?

PS: Also the /n is not working as a line break, but i think i can figure that out later, but help is still appreciated

Your slash is the wrong direction.


Steve
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Welcome to the Ranch, Dirk!

Dirk Kok wrote:



The error message is telling you exactly what's wrong. It's saying that at line 7 of CompareNumbers.java, you tried to to acces the first element (at index 0) of an array that has fewer than 1 elements--that is, an array that has length 0.

Remember, an array's indices are in the range 0..length - 1. So, for example, an empty array has nothing at any index, an array with one element has it at index 0, and an array with 4 elements has them at indices 0..3.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

One other thing jumps out at me: You're using == to check for String equality. That's wrong. When checking if two objects' contents are equal, you must always use the equals() method.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8404
    
  23

Dirk Kok wrote:Source:...

First: welcome to JavaRanch.

Second: Please DontWriteLongLines (←click). It makes your post very difficult to read. I've broken yours up this time.

Thanks

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11495
    
  16

I don't mean to pile on, but variable names like 'a' and 'b' are terrible.

Use more descriptive names. Even inputOne and inputTwo are better.

About the only time you should use a one-character name is for a counter in a loop.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Dirk Kok
Greenhorn

Joined: May 01, 2013
Posts: 2
Steve Luke wrote:
How are you running the application? How many parameters are you passing in? What is the actual command line you are using to launch the app?

For testing, Im launching it like "java CompareNumbers", which returns the error. However, when I type stuff like "java CompareNumbers help" or "java CompareNumbers
GetCenter 20 30" it says that the function is not recognized.

Jeff Verdegan wrote:
The error message is telling you exactly what's wrong. It's saying that at line 7 of CompareNumbers.java, you tried to to acces the first element (at index 0) of an array that
has fewer than 1 elements--that is, an array that has length 0.

Remember, an array's indices are in the range 0..length - 1. So, for example, an empty array has nothing at any index, an array with one element has it at index 0,
and an array with 4 elements has them at indices 0..3.

Let me clarify. The array I was accessing is args, which is the array defined in the arguments of the main method, and if I try to put a size number in it(like: String[3] args)
it returns about 40 errors when compiling.

fred rosenberger wrote:I don't mean to pile on, but variable names like 'a' and 'b' are terrible.

Updated.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Dirk Kok wrote:
Let me clarify. The array I was accessing is args, which is the array defined in the arguments of the main method, and if I try to put a size number in it(like: String[3] args)
it returns about 40 errors when compiling.


That array is no different than any other array. There's nothing special about it. It's just a parameter passed to your main method, and if it has no elements, then you can't get the first element (index 0). Just like any other array, that array's size is set when it is created, which is at some point before your method is called.

It's created by the JVM when it's launched, before it calls your main. When invoking from the command line, it contains the args provided after the class name. For isntance, if I do:


then args will have lenght 2, with args[0] being "abc" and args[1] being "def".

If you're using an IDE (which you shouldn't be doing as a beginner) and you want to use it to launch your app, then you'll have to look into your IDE's documentation, help forums, etc. to find out how to specify those args when launching your app from within the IDE.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Dirk Kok wrote:
Steve Luke wrote:
How are you running the application? How many parameters are you passing in? What is the actual command line you are using to launch the app?

For testing, Im launching it like "java CompareNumbers", which returns the error. However, when I type stuff like "java CompareNumbers help" or "java CompareNumbers
GetCenter 20 30" it says that the function is not recognized.


Copy/paste the exact, complete error message, and indicate clearly which line it's talking about.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

You have yourself written an else block:



And I presume this is what you are seeing because none of your if -else if conditions are being met. Obviously, you will get that message only.


~ Mansukh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: runtime error with main method