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 4593702
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:43:21+00:00 2026-05-21T22:43:21+00:00

So recently I have been working on phonebook project that uses Binary Search Tree.

  • 0

So recently I have been working on “phonebook” project that uses “Binary Search Tree”.

The Phonebook is using BSTree. Each node of the tree is BTNode. 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 extends Comparable<E>> implements Comparator<E>
{
    public int compare(final E a, final E b)
    {
        return (a.compareTo(b));
    }
}

and I use it in BSTree.

Also, I define compareTo() method in Pair class:

@Override
    public int compareTo(Pair pair)
    {
        return name.compareTo(pair.getName());
    }

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 ?

This error what I got after inserting the second node (Pair) in BST:

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();

Thanks in advance for any help 🙂

  • 1 1 Answer
  • 3 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-21T22:43:22+00:00Added an answer on May 21, 2026 at 10:43 pm

    Your stacktrace indicates that your problem is that name is null in a Pair.

    .. or pair that is an argument to compareTo

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

Sidebar

Related Questions

So I have been working on this phone gap application that uses jquery-mobile. Recently,
On a rails project that I know to have been working as recently as
I have recently been working with someone on a project that is very ajax
I have been working with databases recently and before that I was developing standalone
I have a stored procedure that has been working just fine, but I recently
I have recently been working with a COM+ component that processes an input XML
I've been working on some solutions for some questions that have been recently posted
On one project we have been working on recently, we have in essence totally
Recently I have been working on a project where, among other things, we want
I have been working on a project recently where a Player can create a

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.