• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error with Multi-Dimensional Arrays  RSS feed

 
Omo Jesu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a program containing methods and i have error accessing/creating entries for a declared multidimensional array. Kindly help look into the code.



Note... this is not the complete code.... This is just the part I am having problem with the code. I will really appreciate
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that multidimensional arrays are just arrays of arrays. You didn't initialize the array elements. So they have there default value of null.
 
Omo Jesu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes... the elements being referenced to have been initialized with values. As I said earlier, i did not put in the full code. The particular line with the error runs but on running the second or the third time, it generates an error.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please TellTheDetails. What does happen and what do you expect to happen? Post errors and stacktraces. Those are very useful in solving problems.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leather Faze wrote:Yes... the elements being referenced to have been initialized with values. As I said earlier, i did not put in the full code. The particular line with the error runs but on running the second or the third time, it generates an error.


Okay, then it is impossible to say what the error is. We would need to see real running code which causes the problem, and the real error message you get. Other than that we would be just guessing at partial code. The code as is is nowhere near compilable or runnable. I tried to make it runnable but there are so many changes that need to be made I was not really able to see what the original intent was. So if you could make a short runnable example it would go a long way (see SSCCE).

And as far as:

That line of code can't 'hang' or block in any way. There has to be something else - like an exception happing which you are not showing. Make sure you do not do:

Anywhere in your code. That would catch exceptions and hide them from you, so you would have no idea what is going on. If you catch an exception then you should at very least report it (e.printStackTrace()).
 
Dhruva Mistry
Ranch Hand
Posts: 67
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leather,



i believe youi first need to initialize tum[] object with some values
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dhruva Mistry wrote:i believe youi first need to initialize tum[] object with some values
He does that at line 7.
 
Dhruva Mistry
Ranch Hand
Posts: 67
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:
Dhruva Mistry wrote:i believe youi first need to initialize tum[] object with some values
He does that at line 7.


ohh yes... line 6
 
Omo Jesu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue is that I do not want to post the whole code here cos it is a big issue... but kindly run it as I have provided it here;





And my current output is

Trunk Int Number: 3456
First Class: dfgh
Last Class: egfg
Tutor?
Option 1: Mr Ray
Option 2: My Jay
Option 3: Jack Bauer
Enter the Option Numerical Value :2
How Many Counter shall I get?: 3
Counter 1: 34
Counter 2: 56
Counter 3: 34
Trunk Month Number: 34
Do You Want To Continue (Y / N)? y
Trunk Int Number: 3455
First Class: 345
Last Class: df
Tutor?
Option 1: Mr Ray
Option 2: My Jay
Option 3: Jack Bauer
Enter the Option Numerical Value :3
How Many Counter shall I get?: 2
Counter 1: 34
Counter 2: 67
Trunk Month Number: 45
Do You Want To Continue (Y / N)? n
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at Test.tMM(Test.java:100)
at Test.calculateValues(Test.java:76)
at Test.main(Test.java:20)


And It is supposed to run correctly and then go to the output part to output all the entered values...

Kindly help
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a couple things that I noticed:

You're not really programming in an OOP style. Every method is static and you've a lot of static variables. Try to create classes and split up the functionality. That way you'll get a better overview and you're program will be easier to comprehend.

Your methods are big and do many things. That makes it hard to comprehend what the method really does. Try to split big methods into smaller methods which do only one thing.

You're using a lot of arrays with a fixed size of 20. I'm not sure where that 20 comes from but I assume that is just a random number which is big enough. If you're not sure how many objects will be needed you can use a List. Those are flexible in size.

The results won't be printed because count will still be 0 after one run.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is each time you go through the get input routine you recreate the entire got[][] array. So the got[][] array only contains the size of the last run. In your example run got is got[20][2]. You then try to loop through the values relative to the size stored in times[]. But times[0] has a value of 3, so it expects got to be at least got[1][3], which it is not.

In the method where you gather input, change

to

so that you only re-assign the inner array size for the current outer index, not for the entire got array.
 
Omo Jesu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All...

Steve, I have replaced line 57 with this code



And it worked well as expected.... I have some other errors now when trying to make the code better... But I want to try debug it first and If I do not come out with a solution, I will be back here surely... Thanks guys!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!