• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

FT NullPointerException.

 
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see below 3 points. Why compiler allowed point 2 and not point 1?

Point 2 leads to NPE in point 3,
Why does the compiler allowed point 2 in the first place?
I expected the compiler to give a compilation error as in point 1.


1.


Compilation error: As I expected:

Multiple markers at this line
- Cannot reference a field before it is
defined
- Cannot reference a field before it is
defined

2. But then I solved the compilation error by using FtNpeInterface.doubleFT



3. But then at rumtime, Caused by: java.lang.NullPointerException



At first, I did not see why.
But tracing down from sequential timing point of view, I see why NullPointerException occured.
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your original code, the error message says it all:

Cannot reference a field before it is defined


If I move the first field after the other two it compiles just fine.

In your second example, you still try to access the two fields, but they haven't been initialised yet. That's why you get an NPE.
 
H Paul
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob,

1. Thanks for NPE and the Field keyword. +1 for your Certs.

2. (A side note: On a piece of paper, the point 1 and point 2 are the same,
I thought it would be magically for the compiler to see point 1 as the point 2 "implicitly Forward Declaration"
That way, I don't have to worry about the order when in the future I can add any function composition.)

My Back to the future:
8.3.3. Forward References During Field Initialization
https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.3.3
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic