• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

runtime error with main method

 
Dirk Kok
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Kok
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic