• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

FT NullPointerException.

 
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic