null pointer exceptions

Erik Max Francis max at
Fri Jul 18 07:06:54 CEST 2003

Tennessee James Leeuwenburg wrote:

> I have a class which includes adding an ImageIcon. If the required
> graphic
> resource isn't present, there is a NullPointerException. Java doesn't
> care
> - the straight Java program handles it internally and gets on with
> life.
> But when I include it from Python, it explodes.

A java.lang.NullPointerException is just an exception like anything
else.  Can't you just catch it?

max at oxygen:~/tmp% cat
import java.lang.*;

public class NullCaster
    public static void main(String[] args)
        Object nullObject = null;
        String nullString = (String) nullObject;
max at oxygen:~/tmp% javac
max at oxygen:~/tmp% jython
Jython 2.1 on java1.4.1 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> import java.lang
>>> import NullCaster
>>> try:
...  NullCaster.main([])
... except java.lang.NullPointerException, e:
...  print 'oops:', e 
oops: java.lang.NullPointerException

   Erik Max Francis && max at &&
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ Wretches hang that jurymen may dine.
\__/  Alexander Pope

More information about the Python-list mailing list