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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:05:51+00:00 2026-05-22T23:05:51+00:00

I have a big problem with this code and I have no idea how

  • 0

I have a big problem with this code and I have no idea how to cause it:

while(tree.find(indexreg)!=null){
        //do stuff
    }

For some reason, comparing tree.find(indexreg) with null causes a NullPointerException. Since this is a college project I have to use my own binary tree implementation instead of the one provided by Java. tree is a BinarySearchTree and indexreg is a Comparable object, that was already initialized. This is the code for find in the BinarySearchTree class:

public Comparable find(Comparable x) {
        return elementAt(find(x, root));
    }

It looks for the object in the tree and it returns null if it doesn’t find it (I don’t think you can return an empty Comparable object). I tried Googling but I didn’t find an useful answer. Does anyone know how to make this code work?

  • 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-22T23:05:51+00:00Added an answer on May 22, 2026 at 11:05 pm

    I don’t think the problem has anything to do with your Comparable.

    If the line while(tree.find(indexreg) != null) { throws a NullPointerException, it must be because tree is null. No other possibilities are credible.

    • Comparison of an object reference with null using == or != will NOT throw an NPE. So even if tree.find(...) returned a null, that can’t be the cause of this exception.

    • Passing a null value as a method argument will NOT throw an NPE. So if indexreg was null, that wouldn’t cause this exception. (An NPE could be thrown by the find method or something it calls, but the stacktrace will not show a different line in a different method as the origin of the exception.)


    (I could be misinterpreting the question. I’m assuming that the OP means “throws” when he says that the line “causes” the exception.

    Unfortunately, the OP is only posting snippets of code and hasn’t shown us the stacktrace … which is the critical piece of evidence.)

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

Sidebar

Related Questions

i have some big xslt crashing iis (StackOverflowException) when loading an XslCompiledTransform while the
I have big problem. Lets look on the code below: protected void Application_AuthenticateRequest(object sender,
I've created a form with code and I have a big problem. Every element
I have this big data-entry sort of page, a table kind of layout using
I was just writing some quick code and noticed this complier error Using the
I have a big problem with my SMTP server for sending emails. It's down
I really seem to have a big problem here. I'm using MySQL to store
I have had a few problems with log files growing too big on my
I have big issue with url-rewriting for IIS 7.0. I've written simple module for
How would you maintain the legacy applications that: Has no unit tests have big

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.