-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
I'm using the following piece of code:
==
for (int i = 1; (curr_line = input_file.readLine()) != null; i++) {
if (i == 1) {
line_length_ref = curr_line.length();
}
else {
if (curr_line.length() != line_length_ref) {
System.err.println("Input file must contain same number of columns through rows.");
System.exit(1);
}
} // some other things
k = i;
}
if (k != line_length_ref) {
// do some things
}
==
The compiler tells me that
* line_length_ref might not have been initialized at L. 6 (numbering
from this first line of code). However, if the else {} block is
launched at some time, line_length_ref will have already been
initialized, so I don't see where's the problem on an algorithmic POV,
* k might not have been initialized.
Do you have any workaround?
- --
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- --
Remember. If something can go wrong, it will.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>
iEYEARECAAYFAk2MqtsACgkQM0LLzLt8MhyTWACbBvbEygnJj+oGdHuwD8jVogoz
G+gAnR7Z4yiMZQSjvxtwjQnljV4HfBqG
=MuGy
-----END PGP SIGNATURE-----
|
|
0
|
|
|
|
Reply
|
Merciadri
|
3/25/2011 2:46:52 PM |
|
Merciadri Luca wrote:
> I'm using the following piece of code:
>
> ==
> for (int i = 1; (curr_line = input_file.readLine()) != null; i++) {
> if (i == 1) {
> line_length_ref = curr_line.length();
> }
> else {
> if (curr_line.length() != line_length_ref) {
> System.err.println("Input file must contain same number
> of columns through rows."); System.exit(1);
> }
> } // some other things
> k = i;
> }
>
> if (k != line_length_ref) {
> // do some things
> }
> ==
> The compiler tells me that
> * line_length_ref might not have been initialized at L. 6 (numbering
> from this first line of code). However, if the else {} block is
> launched at some time, line_length_ref will have already been
> initialized, so I don't see where's the problem on an algorithmic POV,
You're using line_length_ref outside the loop, but the compiler can't
know if the loop will ever run. If you feed it an empty file, the loop
doesn't even run once becuase readLine() will return null immediately.
> * k might not have been initialized.
Same problem.
> Do you have any workaround?
> - --
You appear to want to read the file while there are still lines to
read. Logically, a while loop would seem more appropriate here:
rowsRead = 0;
lineLengthRef = 0;
while ( currentLine = inputFile.readLine() != null )
if ( lineLengthRef == 0 ) // then this is the first line read
{
lineLengthRef = currentLine.length();
}
else if ( currentLine.length() != lineLengthRef )
{
// error
}
// some other things
rowsRead++;
}
if ( rowsRead != lineLengthRef )
{
// other things
}
I took the liberty of using some more descriptive variable names and
using the camel-casing typcially used in Java.
By giving the variables initial values before entering the loop, you
know they'll always contain something. I'll leave it to you to
determine if the "other things" in the conditional after the loop make
sense for 0 rows with 0 lineLengthRef.
Alex
|
|
0
|
|
|
|
Reply
|
Alex
|
3/25/2011 3:59:29 PM
|
|
On 03/25/2011 10:46 AM, Merciadri Luca wrote:
> The compiler tells me that
> * line_length_ref might not have been initialized at L. 6 (numbering
> from this first line of code). However, if the else {} block is
> launched at some time, line_length_ref will have already been
> initialized, so I don't see where's the problem on an algorithmic POV,
When Java determines initialization semantics, it takes a very
conservative approach, since the full problem is actually provably
impossible to compute.
The compiler doesn't know that the variable i will have had to be 1 at
some point in time. All it knows is that the variable has definitely
been introduced, since the initialization of i precedes the loop on all
paths through the control flow graph. To be conservative, it will assume
that the if statement will never have been executed when it enters the
else statement, hence the potential uninitialization werror.
> * k might not have been initialized.
This is a case where your analysis is wrong, and it's not that the
compiler isn't smart enough. What if the loop is never executed, i.e.,
the file is empty?
> Do you have any workaround?
Initialize the values to some default value first, like -1 or 0, when
you declare them.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
|
|
0
|
|
|
|
Reply
|
Joshua
|
3/25/2011 4:34:10 PM
|
|
On 03/25/2011 11:59 AM, Alex Mentis wrote:
> Merciadri Luca wrote:
>> I'm using the following piece of code:
>>
>> ==
>> for (int i = 1; (curr_line = input_file.readLine()) != null; i++) {
>> if (i == 1) {
>> line_length_ref = curr_line.length();
>> }
>> else {
>> if (curr_line.length() != line_length_ref) {
>> System.err.println("Input file must contain same number
>> of columns through rows."); System.exit(1);
>> }
>> } // some other things
>> k = i;
>> }
>>
>> if (k != line_length_ref) {
>> // do some things
>> }
>> ==
>> The compiler tells me that
>> * line_length_ref might not have been initialized at L. 6 (numbering
That's because you didn't guarantee the initialization in your code. The
message is to alert you to that fact.
>> from this first line of code). However, if the else {} block is
>> launched at some time, line_length_ref will have already been
>> initialized, so I don't see where's the problem on an algorithmic POV,
The compiler doesn't have your special run-time knowledge. It doesn't know
what the value of 'i' will be at runtime when it does the analysis of line
"6", so it has to issue the error.
You also use the 'line_length_ref' after the loop in a way that even you
cannot know will be safe.
RTFM for what the rules actually are.
> You're using line_length_ref outside the loop, but the compiler can't
> know if the loop will ever run. If you feed it an empty file, the loop
> doesn't even run once becuase readLine() will return null immediately.
>
>> * k might not have been initialized.
>
> Same problem.
>
>> Do you have any workaround?
Yes, make sure the variable is definitely assigned and fix your bug.
....
> I took the liberty of using some more descriptive variable names and
> using the camel-casing typcially used in Java.
Thus setting a good example for the OP about the Java conventions.
> By giving the variables initial values before entering the loop, you
> know they'll always contain something. I'll leave it to you to
That's one way, not the only way.
> determine if the "other things" in the conditional after the loop make
> sense for 0 rows with 0 lineLengthRef.
The rules for "definitely assigned" and "definitely unassigned" are spelled
out explicitly in the Java Language Specification (JLS). Those are the rules
the compiler must obey, not yours.
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
|
|
0
|
|
|
|
Reply
|
Lew
|
3/25/2011 5:03:38 PM
|
|
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> writes:
> Hi,
>
> I'm using the following piece of code:
>
> ==
> for (int i = 1; (curr_line = input_file.readLine()) != null; i++) {
> if (i == 1) {
> line_length_ref = curr_line.length();
> }
> else {
> if (curr_line.length() != line_length_ref) {
> System.err.println("Input file must contain same number of columns through rows.");
> System.exit(1);
> }
> } // some other things
> k = i;
> }
>
> if (k != line_length_ref) {
> // do some things
> }
> ==
> The compiler tells me that
> * line_length_ref might not have been initialized at L. 6 (numbering
> from this first line of code). However, if the else {} block is
> launched at some time, line_length_ref will have already been
> initialized, so I don't see where's the problem on an algorithmic POV,
> * k might not have been initialized.
>
> Do you have any workaround?
Thanks all for your precious answers.
- --
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- --
If you don't buy a ticket, you can't win the raffle.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>
iEYEARECAAYFAk2M2UUACgkQM0LLzLt8MhzHGgCfed2VeTis8n2XiCU8clIfZok+
6UcAoIEgrL3mgcDoKUr/jojIy/+Z1dhN
=wRL8
-----END PGP SIGNATURE-----
|
|
0
|
|
|
|
Reply
|
Merciadri
|
3/25/2011 6:04:54 PM
|
|
On Fri, 25 Mar 2011 15:46:52 +0100, Merciadri Luca
<Luca.Merciadri@student.ulg.ac.be> wrote, quoted or indirectly quoted
someone who said :
>The compiler tells me that
>* line_length_ref might not have been initialized at L. 6 (numbering
>from this first line of code). However, if the else {} block is
>launched at some time, line_length_ref will have already been
>initialized, so I don't see where's the problem on an algorithmic POV,
>* k might not have been initialized.
>
>Do you have any workaround?
In general the compiler uses some rough and ready conservative logic
to decide if something might not get initialised. You simply have to
humour it. Usually it is correct. You forgot to consider the
possibility of exceptions being thrown unexpectedly.
I did not look at the details of precisely why in your case. Your way
of formatting code makes analysing it painful. Please have a look at
some of the code examples on my site to see how to indent to make code
readable.
--
Roedy Green Canadian Mind Products
http://mindprod.com
If you think it’s expensive to hire a professional to do the job, wait until you hire an amateur.
~ Red Adair
|
|
0
|
|
|
|
Reply
|
Roedy
|
3/25/2011 10:04:21 PM
|
|
Roedy Green <see_website@mindprod.com.invalid> wrote in
news:b64qo6dif9cjoob5cnv098lv8gf5b7ub4u@4ax.com:
> On Fri, 25 Mar 2011 15:46:52 +0100, Merciadri Luca
> <Luca.Merciadri@student.ulg.ac.be> wrote, quoted or indirectly quoted
> someone who said :
>
>>The compiler tells me that
>>* line_length_ref might not have been initialized at L. 6 (numbering
>>from this first line of code). However, if the else {} block is
>>launched at some time, line_length_ref will have already been
>>initialized, so I don't see where's the problem on an algorithmic POV,
>>* k might not have been initialized.
>>
>>Do you have any workaround?
>
> In general the compiler uses some rough and ready conservative logic
> to decide if something might not get initialised. You simply have to
> humour it. Usually it is correct. You forgot to consider the
> possibility of exceptions being thrown unexpectedly.
>
<snip>
And to answer your question about a workaround...
Somewhere (not shown), you have:
int line_length_ref;
Replace this with:
int line_length_ref = 0;
to appease the compiler.
You did not show all of your code or provide an SSCCE so I cannot verify
this, but it is likely to work.
|
|
0
|
|
|
|
Reply
|
Ian
|
3/28/2011 4:01:48 PM
|
|
Ian Shef wrote:
> Roedy Green wrote:
>> Merciadri Luca wrote, quoted or indirectly quoted someone who said :
>
>>> The compiler tells me that
>>> * line_length_ref might not have been initialized at L. 6 (numbering
>>> from this first line of code). However, if the else {} block is
>>> launched at some time, line_length_ref will have already been
>>> initialized, so I don't see where's the problem on an algorithmic POV,
>>> * k might not have been initialized.
>>>
>>> Do you have any workaround?
>
>> In general the compiler uses some rough and ready conservative logic
>> to decide if something might not get initialised. You simply have to
>> humour it. =A0Usually it is correct. You forgot to consider the
>> possibility of exceptions being thrown unexpectedly.
>
> <snip>
>
> And to answer your question about a workaround...
>
> Somewhere (not shown), you have:
>
> =A0 =A0 int line_length_ref;
>
> Replace this with:
>
> =A0 =A0 int line_length_ref =3D 0;
>
> to appease the compiler.
>
> You did not show all of your code or provide an SSCCE so I cannot verify
> this, but it is likely to work.
>
Or just ensure that the variable is definitely assigned through each
code path.
--
Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
3/28/2011 5:42:52 PM
|
|
Lew <lew@lewscanon.com> wrote in news:0481a2e4-7b97-4f0d-83b6-7c6c0ba5209e@
34g2000pru.googlegroups.com:
> Ian Shef wrote:
>> Roedy Green wrote:
>>> Merciadri Luca wrote, quoted or indirectly quoted someone who said :
>>
>>>> The compiler tells me that
>>>> * line_length_ref might not have been initialized at L. 6 (numbering
>>>> from this first line of code). However, if the else {} block is
>>>> launched at some time, line_length_ref will have already been
>>>> initialized, so I don't see where's the problem on an algorithmic POV,
>>>> * k might not have been initialized.
>>>>
>>>> Do you have any workaround?
>>
>
>>> In general the compiler uses some rough and ready conservative logic
>>> to decide if something might not get initialised. You simply have to
>>> humour it. �Usually it is correct. You forgot to consider the
>>> possibility of exceptions being thrown unexpectedly.
>>
>
>> <snip>
>>
>> And to answer your question about a workaround...
>>
>> Somewhere (not shown), you have:
>>
>> � � int line_length_ref;
>>
>> Replace this with:
>>
>> � � int line_length_ref = 0;
>>
>> to appease the compiler.
>>
>> You did not show all of your code or provide an SSCCE so I cannot verify
>> this, but it is likely to work.
>>
>
> Or just ensure that the variable is definitely assigned through each
> code path.
I hope that this is some form of humor. The Java compiler is doing this
check but, based on the volume and nature of the questions from the OP in
comp.java.lang.help lately, delving into the rules of definite assignment may
be excessive in this phase of the OP's learning.
For those that are ready to learn, definite assignment is covered in Chapter
16 (yes, the entire chapter, all 25 pages!) of the JLS (third edition).
>
> --
> Lew
>
|
|
0
|
|
|
|
Reply
|
Ian
|
3/28/2011 7:52:00 PM
|
|
|
8 Replies
249 Views
(page loaded in 0.147 seconds)
Similiar Articles: comp.lang.java.help`Might not have been initialized' is here wrong 8 139 (3/25/2011 2:46:52 PM) -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I'm using the following piece of code ... How to tell if an exception has currently been thrown and is being ...Well, what's wrong with the catch itself? If you want to ... Normally, only place where you do not have an anonymous ... That said (and, we're off-topic now), you are in the ... code speed moving from fortran 77 compiler to f2003 compiler ...... written, there would not have been ... maybe arrays ?) were not initialized to ... Do you think this might have changed in the past 20 years? Now, the situation might ... Static const integral data members can be initialized? - comp.lang ...We have been getting away with not defining static const integers ... actually inlines it, then it might not ... effect: the definition might not be necessary now, but ... File cannot be opened by FMS 5.5 for hosting - comp.databases ...What could be wrong ... but it has been noticed only now. We have only ... what the problem might be, please ? Sometimes you get this if you've saved a clone and have not ... Swing uses bad features - comp.lang.java.gui... initialized = true > For example, here's one ... NOT to call any public methods that might be ... would have been nice if > > all of the instance variables were initialized ... How to model a High Impedance Fault model with Matlab simulink ...Matlab is not allowing me to model that HIF model. I have no idea where is my mistake. I might be using the wrong component. Here is the ... what toolbox you have been ... ASCII or Unicode - comp.databases.filemakerI could be incorrect in that, so test. Lynn Allen ... I have been trying to get a solid block, in the ... So the solid block you want might be Alt-2588. Or not, since actual ... Explanation needed for const int "error: variably modified ... at ...> What on earth is wrong with using #define as I have used it ... is that the value will not be changed *once the object has been initialized*. ... :) Something else that you might ... glCopyTexSubImage2D - comp.graphics.api.openglBTW: This might happen under Windows, too. The ... make sure the pbuffer has been initialized if ... bits for ALPHA channel > > why 16 and not 8? If I use 16 here I ... Incorrect "variable might not have been initialized"... javac gave me a "variable might not have been initialized ... Incorrect "variable might not have been initialized" ... * The Ultimate DBMS is here! ... Variable might not have been initialized?What's Wrong With My Code? Variable might not have been initialized? ... >> REGISTER NOW TO START POSTING . Members have full ... Re: Variable might not have been initialized 7/24/2012 1:17:26 AM
|