No subject
Sat Nov 12 19:29:10 PST 2005
class, and several constructors of other classes that constitute upper
inheritance hierarchyes. This class inherits from these classes using extends.
cDPI and threadDeEstaClase are general static variables (not local to "abrir").
Method probarEstaClase() is a non-static initialization method that is fired
before the thread is started.
When I run this program, first ConfiguracionProcesadorDeImagenes is started
invoking its "abrir" method, and everything goes fine.
But when ConfiguracionProcesadorDeImagenes exits its run method and
LaIniciacion class is started (when its "abrir" method is invoked using
reflection) I get a InvocationTargetException. Both getTargetException() and
printStackTrace() show that there has been a NullPointerException thrown by
"abrir" this time.
Why does "abrir" throw a NullPointer Exception?
How can this be avoided?
Note: "abrir" must be static or no satisfactory behavior of the program is
obtained.
Thanks a lot in advance,
JG.-
Here is the code of two classes that are also threads and perform the reflection
procedure (I have already included this code in a previous message):
//AdministradorDeReflexion.java
import java.util.*;
import java.lang.reflect.*;
/**
@version 2.0
@author Jose Gengler
@author jgengler at iname.com
*/
public class AdministradorDeReflexion extends Thread
implements Observer
{
public String stringClaseAnterior = new String("1");
public String stringClaseActual = new String("2");
public String stringClaseSiguiente = new String("3");
private boolean booleanPrimerUso = true;
static int count;
ActivadorDeClases activadorDeLaClaseActual;
boolean booleanCerrandoElProcesadorActual = false;
public AdministradorDeReflexion()
{
}
void iniciarNuevoProcesador(String stringNuevaClase1)
{
System.gc();
System.out.println(" " + stringClaseActual);
System.out.println(" " + stringClaseSiguiente);
stringClaseActual = stringNuevaClase1;
stringClaseSiguiente = stringClaseActual;
activadorDeLaClaseActual = null;
activadorDeLaClaseActual = new ActivadorDeClases(stringClaseActual);
activadorDeLaClaseActual.start();
while(stringClaseActual.equals(stringClaseSiguiente))
{
//count++;
//System.out.println(" " + count + " " + booleanCerrandoElProcesadorActual);
booleanCerrandoElProcesadorActual =
((Boolean)(activadorDeLaClaseActual.averiguarElValorDeUnaVariable("booleanCerrandoEstaClase"))).booleanValue();
//System.out.println("" + booleanCerrandoElProcesadorActual);
if(booleanCerrandoElProcesadorActual)
{
stringClaseActual = (String)(activadorDeLaClaseActual.averiguarElValorDeUnaVariable("stringClaseActual"));
stringClaseAnterior = (String)(activadorDeLaClaseActual.averiguarElValorDeUnaVariable("stringClaseAnterior"));
stringClaseSiguiente = (String)(activadorDeLaClaseActual.averiguarElValorDeUnaVariable("stringClaseFutura"));
activadorDeLaClaseActual.cerrarActivadorDeClases();
}
}
}
public void update(Observable Procesador1, Object arg)
{
}
public void run()
{
while(true)
{
if(booleanPrimerUso)
{
iniciarNuevoProcesador("LaIniciacion");
booleanPrimerUso = false;
}
else
if(!(stringClaseActual.equals(stringClaseSiguiente)))
{
iniciarNuevoProcesador(stringClaseSiguiente);
}
}
}
public static void main(String[] args)
{
AdministradorDeReflexion aDR = new
AdministradorDeReflexion();
aDR.start();
}
}
********************
//ActivadorDeClases.java
import java.util.*;
import java.lang.reflect.*;
/**
@version 2.0
@author Jose Gengler
@author jgengler at iname.com
*/
public class ActivadorDeClases
extends Thread
// implements Observer
{
String stringNombreDeLaClase = new String();
public Class newClass;
int count;
boolean booleanCerrarActivadorDeClases = false;
boolean booleanIniciandoUnaNuevaClase = true;
public ActivadorDeClases(String stringNombreDeLaClase1)
{
stringNombreDeLaClase = stringNombreDeLaClase1;
try
{
newClass = Class.forName(stringNombreDeLaClase);
} catch(ClassNotFoundException e) {System.out.println(e);}
}
public void invocarUnMetodo(String stringNombreDelMetodo1)
{
try
{
Method theMethod;
Class[] parameterTypes;
parameterTypes = new Class[0];
theMethod = newClass.getMethod(stringNombreDelMetodo1, parameterTypes);
System.gc();
System.out.println("" + theMethod);
theMethod.invoke(null, new Object[0]);
} catch(NoSuchMethodException e) {System.out.println(e);}
catch(IllegalAccessException e) {System.out.println(e);}
catch(InvocationTargetException e) { System.out.println(e);
System.out.println(e.getTargetException());
e.printStackTrace();
}
}
public Object averiguarElValorDeUnaVariable(String stringNombreDeLaVariable1)
{
Object o = new Object();
Field fieldParaAveriguarValoresDeVariables;
try {
fieldParaAveriguarValoresDeVariables = newClass.getField(stringNombreDeLaVariable1);
o = fieldParaAveriguarValoresDeVariables.get(o);
} catch (NoSuchFieldException e) {System.out.println(e); }
catch (SecurityException e) {System.out.println(e); }
catch (IllegalAccessException e) {System.out.println(e); }
return(o);
}
public void cerrarEstaClase()
{
Object o = new Object();
Field fieldParaAveriguarValoresDeVariables;
try
{
fieldParaAveriguarValoresDeVariables = newClass.getField("booleanCerrandoEstaClase");
boolean booleanFalso = false;
Boolean objetoBoolean = new Boolean(booleanFalso);
fieldParaAveriguarValoresDeVariables.set(o, objetoBoolean);
} catch (NoSuchFieldException e) {System.out.println(e); }
catch (SecurityException e) {System.out.println(e); }
catch (IllegalAccessException e) {System.out.println(e); }
}
public void run()
{
while(true)
{
//count++;
//System.out.println(" " + count);
if(booleanIniciandoUnaNuevaClase)
{
System.out.println("" + booleanIniciandoUnaNuevaClase);
invocarUnMetodo("abrir");
booleanIniciandoUnaNuevaClase = false;
}
if(booleanCerrarActivadorDeClases)
{
cerrarEstaClase();
return;
}
}
}
public void cerrarActivadorDeClases()
{
booleanCerrarActivadorDeClases = true;
}
}
---
To unsubscribe, mail advanced-java-unsubscribe at xcf.berkeley.edu
To get help, mail advanced-java-help at xcf.berkeley.edu
More information about the Advanced-java
mailing list