• 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

Typical RuntimeError ?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,
I am getting a typical RuntimeError in context of my program ? Can you get what that error is and why it is coming ?
One thing it is coming because there is wrong intialization in the static block.
CertkillerTe2.java
public class CertkillerTe2 {
static int[] a;
static { a[0] = 2;}
public static void main(String[] args) { }
}

Output :

D:\>javac CertkillerTe2.java

D:\>java CertkillerTe2
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at CertkillerTe2.<clinit>(CertkillerTe2.java:3)



 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not even intermediate issue - belongs in beginner.

Static field "a" is an array. Its default value is null. That means no array has been created.

Nothing sets "a" before an attempt is made to set a[0], hence NullPointerException.

You need to create an array and assign it to "a". For example: -

a = new int[1];
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic