NullPointerException when using Comparator. help

Discussion in 'Java' started by anonaki, Sep 6, 2003.

  1. anonaki

    anonaki Guest

    Why does the code below throw a java.lang.NullPointerException?? The
    two objects being compared obviously don't become Null references.
    any help would be greatly appreciated.

    import java.util.*;

    class test3
    {
    public static void main( String [] args )
    {
    Object obj1 = new Integer( 1 );
    Object obj2 = new Integer( 2 );

    if ( cmp.compare( obj1 ,obj2 ) == 0 )
    System.out.println( "They are Equal" );
    }

    static Comparator cmp;
    }
     
    anonaki, Sep 6, 2003
    #1
    1. Advertisements

  2. You have a reference to a Comparator, but no Comparator for it to
    refer to. You'll want to have cmp = new Comparator() or something like
    that somewhere.
    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "When a man talks dirty to a woman, that's sexual harassment. When a woman talks
    dirty to a man, that's 14.99 per minute + local telephone charges!"
    - Ruben Stiller
     
    Joona I Palaste, Sep 6, 2003
    #2
    1. Advertisements

  3. anonaki

    anonaki Guest

    I got it
    thanks.
     
    anonaki, Sep 8, 2003
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.