This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 244839 - Incorrect Error Reporting On Uninitialized Object
Summary: Incorrect Error Reporting On Uninitialized Object
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Editor (show other bugs)
Version: 8.0
Hardware: PC Windows 7
: P3 normal (vote)
Assignee: Dusan Balek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-06-02 10:26 UTC by henri127
Modified: 2014-06-05 21:06 UTC (History)
0 users

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description henri127 2014-06-02 10:26:01 UTC
Product Version = NetBeans IDE 8.0 (Build 201403101706)
Operating System = Windows 7 version 6.1 running on amd64
Java; VM; Vendor = 1.8.0
Runtime = Java HotSpot(TM) 64-Bit Server VM 25.0-b70

The following code for a function inside a class reports incorrectly that variable out might not have been initialized:

    public void function() {
        String out;
        
        int num = 0;
        if ((System.currentTimeMillis() & 0x3) != 0) {

                out = "test";
                num = (int) (System.currentTimeMillis() & 0x3) + 24;
        }

        if (num != 0) {
            char c = out.charAt(0);
        }
    }
Comment 1 Jiri Prox 2014-06-03 12:14:14 UTC
This is error from javac (not Netbeans), since javac does not inspect the expressions in the conditions to determine which code will be executed.

I'm afraid we cannot do much about it at Netbeans side, since we must respect javac output.
Comment 2 henri127 2014-06-03 14:00:54 UTC
Are Netbeans and javac not made by the same company? Looks strange to me that Netbeans does not have an interest in solving an error which only occurs to me when running Netbeans.
Comment 3 Jan Lahoda 2014-06-04 13:06:50 UTC
Specifically regarding the code above, I don't think this is an error in either NetBeans or javac. The Java Language Sepcification specifies that local variables need to be definitelly assigned before it is read, and also specifies a specific way to determine the definite assignment. I am afraid that for the specific prescribed algorithm the "out" variable is not definitelly assigned at the point it is used, and hence the JLS requires that a compile time error is emitted.
Comment 4 henri127 2014-06-04 16:30:02 UTC
For the many possibilities to sub-dependencies, I can see the difficulties in making a usefull compiler, with helpfull error reporting. In this case, however, the code will not compile due to this report, while it is very obvious for a human that "out" is definitelly defined when used. Consider p.e. the following when changing the code to:

    public void function() {
        String out;
        
        int num = 0;
        if (true) {//(System.currentTimeMillis() & 0x3) != 0) {
                out = "test";
                num = (int) (System.currentTimeMillis() & 0x3) + 24;
        }

        if (num != 0) {
            char c = out.charAt(0);
        }
    }
the error is not present anymore, but in case of:

    public void function() {
        String out;
        
        int num = 0;
        if (num == 0) {//(System.currentTimeMillis() & 0x3) != 0) {
                out = "test";
                num = (int) (System.currentTimeMillis() & 0x3) + 24;
        }

        if (num != 0) {
            char c = out.charAt(0);
        }
    }
it will re-appear. So what is actually meant by this rule according local variables and by making this an error and not a warning as it was in earlier versions. 
The more serious is the report, and that is how it was encountered when the local variable is inside a switch statement. Since a local variable within a switch statement with the same name under different case labels is not allowed. While any part of a case label could 'simply' be seen just as new routine/function. 

So:
    public void function() {
       int num = 0;
       switch (num) {
            case 0:
                String temp = "";
                break;
            case 1:
                String temp = "second";
                break;
        }
   }
gives an error. But not anymore when it is replaced by:

    public void function() {
       int num = 0;
       switch (num) {
            case 0:
                doZero();
                break;
            case 1:
                doOne();
                break;
        }
    }
    public void doOne() {
        String temp = "";        
    }
    public void doZero() {
        String temp = "";        
    }
Comment 5 Jan Lahoda 2014-06-05 21:06:53 UTC
Sorry, but I don't think that "variable might not have been initialized" was ever a warning. To my knowledge, it was always a compile-time error, per JLS. I tried the code above in 1.4.2 javac, and it also produces the error. At times, to my knowledge, there are adjustments in the spec and fixes in the implementation, but offhand, I don't know about any that would affect the code from the description.

The precise algorithm that the compilers must follow is here:
http://docs.oracle.com/javase/specs/jls/se8/html/jls-16.html