For Programmers: Free Programming Magazines  


Home > Archive > Java Help > November 2007 > Help with classloader and NullPointerException









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Help with classloader and NullPointerException
Casper Bang

2007-11-24, 7:14 pm

I have an application which I would like to have the ability to restart
itself and thus flush all singletons, cached resources etc. I thought I
could do this by starting a new application instance dynamically with a
new custom classloader:


if(JOptionPane.showConfirmDialog(this.getMainFrame(), "Restart?",
"Restart?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
{
ClassLoader classLoader = new CustomClassLoader();
Class app = classLoader.loadClass("ActualApplication");
Class[] classArray = { String[].class};
Object[] stringArray = { new String[]{""}};
Method method = app.getMethod("main", classArray);
method.invoke(null, stringArray);
}


However, when my ActualApplication is invoked for the second time (when
trying to restart) I get NullPointerExceptions from various library
code, most noticably java.lang.Class.forName(Class.java:169) where it
makes a call to ClassLoader.getCallerClassLoader().

I guess I am missing something here, what I don't understand is how come
its only at consecutive runs my application it fails. My
CustomClassLoader is simply:

public class CustomClassLoader extends ClassLoader
{
public CustomClassLoader()
{
super(CustomClassLoader.class.getClassLoader());
}
}

Any pointers greatly appreciated.

/Casper
Casper Bang

2007-11-24, 7:14 pm

Ah my own bad, apparently some SwingX library code requires
UIManager.setLookAndFeel to have been called.

/Casper

Casper Bang wrote:
> I have an application which I would like to have the ability to restart
> itself and thus flush all singletons, cached resources etc. I thought I
> could do this by starting a new application instance dynamically with a
> new custom classloader:
>
>
> if(JOptionPane.showConfirmDialog(this.getMainFrame(), "Restart?",
> "Restart?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
> {
> ClassLoader classLoader = new CustomClassLoader();
> Class app = classLoader.loadClass("ActualApplication");
> Class[] classArray = { String[].class};
> Object[] stringArray = { new String[]{""}};
> Method method = app.getMethod("main", classArray);
> method.invoke(null, stringArray);
> }
>
>
> However, when my ActualApplication is invoked for the second time (when
> trying to restart) I get NullPointerExceptions from various library
> code, most noticably java.lang.Class.forName(Class.java:169) where it
> makes a call to ClassLoader.getCallerClassLoader().
>
> I guess I am missing something here, what I don't understand is how come
> its only at consecutive runs my application it fails. My
> CustomClassLoader is simply:
>
> public class CustomClassLoader extends ClassLoader
> {
> public CustomClassLoader()
> {
> super(CustomClassLoader.class.getClassLoader());
> }
> }
>
> Any pointers greatly appreciated.
>
> /Casper

apm35@student.open.ac.uk

2007-11-27, 7:24 pm

On 25 Nov, 01:03, Casper Bang <cas...@jbr.dk> wrote:[color=darkred]
> Ah my own bad, apparently some SwingX library code requires
> UIManager.setLookAndFeel to have been called.
>
> /Casper
>
> Casper Bang wrote:

I realise you have solved the problem now but wouldn't this be easier
to solve by altering the script that invokes your program? I presume
there is a script to set up the CLASSPATH, supply the JVM command line
options needed (e.g memory parameters, JConsole port numbers, any
properties needed etc etc). Why not just put this invocation inside a
loop?

-Andrew Marlow
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com