• Post Reply Bookmark Topic Watch Topic
  • New Topic

local variable not initialized  RSS feed

 
Nazma Panjwani
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading input from a file that has following information:
line 1 = numbers of integers in array,
line 2 = elements in array1,
line 3 = elements in array2.
These lines constitute a test case. There are 1000 test cases in the input file.
So basically, I read the length of arrays, populate the arrays by reading from the file.

The code is below ( I have not included reading input code):


Array2 is populated using the same as above code. However when I use the following code:




I get " local variable vector1 and vector2 have not been initialized error". But both arrays have been initialized in the if{} block. Is it because initialization was local to if block? How do I get around this problem?
 
Tim Cooke
Marshal
Posts: 4050
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 'vector1' array is only initialised when the if condition is satisfied. What if the if condition is not satisfied? The array will not be initialised. This is what the compiler is telling you.

Initialise the variables outside of the if block.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!