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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:17:02+00:00 2026-05-21T11:17:02+00:00

I am trying to create a new BST from the intersection of 2 known

  • 0

I am trying to create a new BST from the intersection of 2 known BSTs. I am getting a NullPointerException in the intersect2 method int he second case, at the line “cur3.item.set_account_id(cur1.item.get_accountid()+ cur2.item.get_accountid());”. I know you get the error when you try to dereference the variable without initializing it but i think i am initializing it? I’m not really sure. I would appreciate the help.

public static Bst<Customer> intersect(Bst<Customer> a, Bst<Customer> b){
     return( intersect2(a.root, b.root));
 }

  public static Bst<Customer> intersect2(BTNode<Customer> cur1, BTNode<Customer> cur2){
  Bst<Customer> result = new Bst<Customer>();

// 1. both empty -> true
  if (cur1==null && cur2==null){
  result=null;
 }
// 2. both non-empty -> compare them
 else if (cur1!=null && cur2!=null) {
  BTNode<Customer> cur3 = new BTNode<Customer>();
  cur3.item.set_account_id(cur1.item.get_accountid()+ cur2.item.get_accountid());
  result.insert(cur3.item);
  intersect2(cur1.left, cur2.left);
  intersect2(cur1.right, cur2.right);
 }

// 3. one empty, one not -> false
else if (cur1==null ||cur2==null){
  BTNode<Customer> cur3 = new BTNode<Customer>();
  cur3.item=null;
  intersect2(cur1.left, cur2.left);
  intersect2(cur1.right, cur2.right);
}
 return result;
}

Here is the image of the problem: enter image description here

  • 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-21T11:17:03+00:00Added an answer on May 21, 2026 at 11:17 am

    A NullPointerException can be caused by a number of things. In your given example, cur1 and cur2 are not null, but there is no guarantee that cur1.item, cur1.item.accountId (and similarly for cur2) are not null.

    Being as you have no description for the underlying implementation, I cannot assist further.
    I can suggest that you do some of a few things:
    1.) check the implementation of your objects (if this happens EVERY time, there may be some sort of initialization problem.
    2.) Whenever you create an instance of your item, do you make sure to specify the accountId field? Try giving a default value for this field so it cannot be null. (try some sort of illegal value [eg -1, false, etc] and test for it.

    If you would post more implementation details, I (or someone) may be able to directly identify the problem.

    Regards.

    Edit:4/20@17:11
    Here’s an example of what you should be doing.

    public class Customer {  
        private int accountId;  
    
        public Customer() {  
            this.accountId = 0;  
        }  
    
        public Customer(int account_identification) {  
            this.accountId = account_identification);  
        }  
    
        //As a side note, general practice implies fields be private  
        //Use a method (hence the term 'getter' and the reciprocal, 'setter')  
        public int getId() {  
            return this.accountId;  
        }  
    
        public void setId(int replacement_account_identification) {  
            this.accountId = replacement_account_identification;  
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a new web application using PowerShell. I keep getting
I'm trying to create a new input method using Input Method Kit . The
I’m trying to create 4 new variables from a dataset in R that contains
I'm trying create new object from a module class in VBA, and I have
In my rails application I am trying to create a new user from a
Im trying to create new file on D: drive with c/c++ I found this
I am now trying to create new database manager under Fragment class. But unfortunately,
I am trying to create new Event objects to be persisted in the database
I am trying to create new rules profile in Sonar 2.9 with my checkstyle
Trying to create a new Dedicated Cache Role in Windows Azure but get the

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.