question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Static fields with default string values cause uncaught TypeError, since string pool is not yet initialized

See original GitHub issue

It will fail to execute with “app.js:445Uncaught TypeError: Cannot read property ‘0’ of undefined”

How to reproduce

    public final static String CONST1 = "FIRST";
    public  final static String CONST2 = "SECOND";

    boolean isTrue = (new Random().nextBoolean());

    StringBuffer buf = new StringBuffer(10);
    buf.append((isTrue ? CONST1 : CONST2)).append(" ");
    System.out.println(buf.toString());

Expanding it works

    public final static String CONST1 = "FIRST";
    public  final static String CONST2 = "SECOND";

    boolean isTrue = (new Random().nextBoolean());

    StringBuffer buf = new StringBuffer(10);
        if (isTrue)
        {
            buf.append(CONST1).append(" ");
        }
        else
        {
            buf.append(CONST2).append(" ");
        }
    System.out.println(buf.toString());

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
konsoletypercommented, Jun 25, 2016

I found the reason. Usually, javac does not put getstatic instructions in such cases, instead it can inline constants with ldc. However, for some reason (probably, a bug) javac does not inline constants in conditional expressions. CONST1 and CONST2 do not get their value via <clinit> method, instead they have default values specified. TeaVM initializes them via $rt_s, which requires string pool to be prepared. String pools are initialized after all declarations, so string pool has undefined value when static fields compute their default values. A probable solution would be to initialize string static fields after initialization of string pool.

1reaction
Wolfteincommented, Jun 25, 2016
public final class Example {
    public final static String CONST1 = "FIRST";
    public final static String CONST2 = "SECOND";

    public static void main(String[] argv) {
        boolean isTrue = (new Random().nextBoolean());

        StringBuilder buf = new StringBuilder();
        buf.append(isTrue ? CONST1 : CONST2);

        System.out.println(buf.toString());
    }
}

image

app.js:1107: oqe_Example24.CONST125 = $rt_s(0);
app.js:1108: oqe_Example24.CONST226 = $rt_s(1);

app.js:445: return $rt_stringPool_instance[index];

function oqe_Example24_main26(a) {
    var b, c;
    b = ju_Random33_nextBoolean27(ju_Random33_$init4());
    c = jl_StringBuilder113_$init4();
    if (b == 0) {
        a = oqe_Example24.CONST226;
    } else {
        a = oqe_Example24.CONST125;
    }
    jl_StringBuilder113_append28(c, a);
    jl_System20_$clinit();
    ji_PrintStream161_println29(jl_System20.out23, jl_StringBuilder113_toString30(c));
    return;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

String initialization in Java - Stack Overflow
I know that for 3rd initialization above, the the string object is initialized in the string pool and the 4th has nothing to...
Read more >
Guide to Java String Pool - Baeldung
In this quick article, we'll explore the Java String Pool — the special memory region where Strings are stored by the JVM.
Read more >
What is Java String Pool? - DigitalOcean
We know that String is a special class in java and we can create String objects using a new operator as well as...
Read more >
node-mssql | Microsoft SQL Server client for Node.js
useUTC - A boolean determining whether or not to use UTC time for values without time zone offset (default: true ). Default connection...
Read more >
What is the concept of String Pool in java? - Edureka
If the string already exists in the string pool, a reference to the pooled instance returns.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found