null pointer exceptions

Tennessee James Leeuwenburg t.leeuwenburg at nothing.bom.gov.au
Fri Jul 18 07:23:03 CEST 2003


On Thu, 17 Jul 2003 22:06:54 -0700, Erik Max Francis wrote:

Certainly I can catch it. However, Java does not require that it be caught
itself. 

I realise it's not great practise to leave these kinds of things in your
Java code, but I'm extending someone else's code, and don't really want to
back-correct class after class as I find these problems coming up,
especially since it doesn't cause Java any hassles.

But I might end up having to - I just thought someone might know a neat
way of getting around the problem.

Cheers,
-Tennessee

> 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 NullCaster.java
> import java.lang.*;
> 
> public class NullCaster
> {
>     public static void main(String[] args)
>     {
>         Object nullObject = null;
>         String nullString = (String) nullObject;
>         nullString.length();
>     }
> }
> max at oxygen:~/tmp% javac NullCaster.java
> 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





More information about the Python-list mailing list