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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:00:24+00:00 2026-06-15T16:00:24+00:00

This is for a homework assignment. The homework is not on recursion it is

  • 0

This is for a homework assignment. The homework is not on recursion it is on tree structures. I have nearly finished the assignment, but my recursive method for moving back up a tree breaks. The tree structure is given by the class below:

package lab12;

import java.io.Serializable;

public class Dog implements Serializable{

    public Dog[] children;
    public String name;

    public Dog(String name)
    {
        this.name = name;
    }

    @Override
    public String toString()
    {
        return name;
    }

} 

I’m quite certain the reason is the return null; statement in combination with my for loop. The for loop iterates over a node that doesn’t contain any children and as a result returns null. This ends the method and passes back null to my program which gives me null pointer exceptions.

I can’t remove the return null statement or it won’t compile, even though it will 100% return using the for loop.

public Dog findParent(Dog root, String name)
{
    String top = "Spot";
    if(top.equals(name))
    {
        System.out.println("No further records");
        System.out.println("Goodbye.");
        System.exit(0);
    }
    for(int i = 0; root.children != null && i < root.children.length; i++)
    {
        if(root.children[i].name.equals(name))
        {
            return root;
        }
        else
        {
            return findParent(root.children[i], name);
        }
    }
    return null; //Compiler still requires a return here.
}

I feel like this has to be a common problem with using for loops in non-void recursive methods. Is there a way to make the compiler happy and yet not have the extra return null statement?

  • 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-06-15T16:00:26+00:00Added an answer on June 15, 2026 at 4:00 pm

    Your code must not work. Because both if and else clauses will return. That causes the loop only do index 0. You should change your code like the following:

    public Dog findParent(Dog root, String name)
    {
        String top = "Spot";
        if(top.equals(name))
        {
            System.out.println("No further records");
            System.out.println("Goodbye.");
            System.exit(0);
        }
        for(int i = 0; root.children != null && i < root.children.length; i++)
        {
            if(root.children[i].name.equals(name))
            {
                return root;
            }
            else
            {
                Dog parent = findParent(root.children[i], name);
                if (parent != null) 
                     return parent;
            }
        }
        return null;
    }
    

    Now, you can see that the last “return null” is necessary.

    In most of case, compiler is smart. If it gives your warnings, you should consider what’s error with your code instead just cheat compiler to avoid warning.

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

Sidebar

Related Questions

I'm still not very good with data structures, but I have this homework assignment
So I'll admit that this is a homework assignment, but I'm not asking you
This is in relation to a homework assignment but this is not the homework
Continuing on this problem , but I'll reiterate: For a homework assignment I have
I know this sounds like a homework assignment, but it isn't. Lately I've been
(this is indirectly a part of a much larger homework assignment) I have something
I have a homework assignment to sort an array in ascending order. Obviously, this
I have a homework assignment. I'm not looking for anyone to do the work
I have a problem with my homework assignment and I do not know where
I posted a question earlier for a homework assignment but I have a new

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.