Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Class Initialization & Reflection - Confusing Behaviour

0 views
Skip to first unread message

Denis Mirchandani

unread,
Dec 13, 2002, 1:58:36 PM12/13/02
to
Hello,

I want to initialize a child class from a parent whenever the child
class object is loaded from the any other class via reflection. The
steps are

Step 1) An Static method finds & loads the child class by reflection.
(AnalyzeThis Class)

String classname = "AnalyzeChild";
Class C = Class.forName(classname);
Constructor cons = C.getConstructor(null);
AnalyzeChild ac = (AnalyzeChild) cons.newInstance(null);


Step 2) This procedure causes the parent to be loaded since the child
extends the parent. (AnalyzeChild & AnalyzeParent Class)

public class AnalyzeChild extends AnalyzeParent

Step 3) In the parent a static method will find & load n number of
children, one them will be the child from which the parent was
reffered.

static{
System.out.println("Forcefully Initializing child");
AnalyzeChild ac = new AnalyzeChild();
ac.printValue("From Parent");
}


Step 4) In the child i have an static object properties which should
be initialized & ready to use. But in this Class causes an
Initialization Exception (null pointer exception when properties
object is reffered in the other static method after the constructor
was called.)

private static Properties props = new properties();


Step 5) If the properties object is forcefully initialized in
constructor of the child, everything works nice and fine. But that
shouldn't be the case i think --- please advise.


Example Code:

AnalyzeParent :
---------------

public class AnalyzeParent
{
static{
System.out.println("Forcefully Initializing child");
AnalyzeChild ac = new AnalyzeChild();
ac.printValue("From Parent");
}
}

AnalyzeChild :
--------------

import java.util.*;

public class AnalyzeChild extends AnalyzeParent
{
private static Properties props = new properties(); // doesnt work

static{
System.out.println("In Static Of Child");
// props = new Properties(); - doesnt work
}

public AnalyzeChild(){
//props = new Properties(); - works
props.put("One", new java.lang.Integer(1));
props.put("Two", new java.lang.Integer(2));
}

public static void printValue(String Value)
{
System.out.println("Value is := " + Value);
System.out.println("Value from Properties := " + props.get("One"));
System.out.println("Value from Properties := " + props.get("Two"));
}
}


AnalyzeThis :
-------------

import java.lang.reflect.*;

public class AnalyzeThis
{
public static void main(String[] args)
{
try{
String classname = "AnalyzeChild";
Class C = Class.forName(classname);
Constructor cons = C.getConstructor(null);
AnalyzeChild ac = (AnalyzeChild) cons.newInstance(null);
ac.printValue("From Caller");
}catch(Exception e){
e.printStackTrace();
}
}
}


ErrorStack :

Error occurs while referring props object.

---------- Run ----------
Forcefully Initializing child
java.lang.ExceptionInInitializerError: java.lang.NullPointerException
at AnalyzeChild.<init>(AnalyzeChild.java:14)
at AnalyzeParent.<clinit>(AnalyzeParent.java:5)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:120)
at AnalyzeThis.main(AnalyzeThis.java:9)
Exception in thread "main" Normal Termination
Output completed (6 sec consumed).


Thanks and Regards,
Denis Mirchandani.

0 new messages