Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4602084
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:55:39+00:00 2026-05-21T23:55:39+00:00

My project is a Phonebook that is using BSTree<E> . Each node of the

  • 0

My project is a Phonebook that is using BSTree<E>. Each node of the tree is BTNode<E>. In the main class, I replace E with Pair class, which has (String name, String number), when I define the nodes.

I have the following comparator class to compare between 2 E types:

import java.util.Comparator;

public class BTNodeComparator<E> implements Comparator<E>
{
    public int compare(E a, E b) throws ClassCastException
    {
        return ((Comparable<E>) a).compareTo(b);
    }
}

and I use it in BSTree<E>.

Now, when I run the program and it comes to the comparator, it gives me errors at the comparator because now it compares between two Pairs

How can I solve this issue? What I want is to compare between the names of the two pairs ?

Sorry for my bad explanation. My English is weak.

=========================

Edit:

@Tim: After trying your solution it gives me this error:

java.lang.NullPointerException
    at Pair.compareTo(Pair.java:36) // @ return name.compareTo(pair.getName());
    at Pair.compareTo(Pair.java:2)  // @ public class Pair implements Comparable<Pair>
    at BTNodeComparator.compare(BTNodeComparator.java:24) // @ return (a.compareTo(b));
    at BTNodeComparator.compare(BTNodeComparator.java:20) // @ public class BTNodeComparator<E extends Comparable<E>> implements Comparator<E>
    at BSTree.search(BSTree.java:285)
    at BSTree.insert(BSTree.java:300)
    at PhoneBook.main(PhoneBook.java:25)

BTW, I decleared BTNodeComparator in BSTree as follows:

protected Comparator<E> c = new BTNodeComparator();
if (c.compare(target, cursor.getElement()) < 0) cursor = cursor.getLeft();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:55:40+00:00Added an answer on May 21, 2026 at 11:55 pm

    The only possible failure I could see is ClassCastException.
    To fix your code you will need to specify your Pair class like this:

    class Pair implements Comparable<Pair> {
        String name;
        String number;
    
        //some implementation stuff...
    
        @Override
        public int compareTo(Pair o) {
            return name.compareTo(o.name);
        }
    }
    

    In general, if your Comparator implementation relies on the generic type implementing Comparable then you should specify it like this:

    public class BTNodeComparator<E extends Comparable<E>> implements Comparator<E> {
        public int compare(final E a, final E b) {
            return a.compareTo(b);
        }
    }
    

    Doing so just gives you compile time safety without changing the semantics of how the Comparator works.

    Based on your comment, I’m guessing you are embedding your BTNodeComparator into the BSTree class as a nested inner-class, which means you probably can’t make that change without changing the type definition in the BSTree declaration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So recently I have been working on phonebook project that uses Binary Search Tree.
My project has a file foo that I've been using and checking in with
My project is currently using a svn repository which gains several hundred new revisions
Project#1 has some interfaces and classes that project#2 references. Now I want to use
Project Background: I am writing a map tile overlay class for java that can
Project: http://design.vitalbmx.com/fave/news.html When I click Add to favorites button (under main pic), in Chrome
My project has both client and server components in the same solution file. I
The Project's Web section (under project properties in VS2008) has a list of debuggers:
Project Requirements: I need to build a web service that will receive chunks of
Project CompetitionServer.exe raised exception class ESQLiteException with message 'Error executing SQL. Error [1]: SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.