NullPointerException when using Comparator. help

A

anonaki

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;
}
 
J

Joona I Palaste

anonaki said:
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.

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.
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;
}

--
/-- Joona Palaste ([email protected]) ---------------------------\
| 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
 
A

anonaki

I got it
thanks.
Joona I Palaste said:
anonaki said:
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.

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.
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;
}
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top