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

  • Home
  • SEARCH
  • 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 6623555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:32:46+00:00 2026-05-25T21:32:46+00:00

I am supposed to be creating a recursive function within Java that prints out

  • 0

I am supposed to be creating a recursive function within Java that prints out all possible colors from a list of colors. E.G.{r, b, g ; r, g, b ; g, r, b ; g, b, r} etc…

I believe I had it figured out and my code is below. Unfortunatly I continue to recieve a null pointer exception within the base case of the recursion function and it never runs. I’ve included a test within my application test class to show that the list of colors is in fact created. I am unsure as to what is causing my error, or where I have erred in my code.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SequentialPrint {
    private List colors;
    private List prefix;

    public SequentialPrint(List colors) {
        this.colors = colors;

    }

    public void printAllSequences(List colors) {
        int prefixCount = 0;
        int colorCount = 0;
        List prefix = new ArrayList();
        if (colors.isEmpty() || prefixCount == colors.size()) { //Base Case
            System.out.print("All Sequences Printed");
        }
        else {
            Object color = colors.remove(0);
            prefix.add(color); //add first color from colors list.
            prefixCount++; //increases prefix counter
            while (prefixCount <= colors.size() + 1) { //prints first rotation of colors
                System.out.println(prefix);
                System.out.print(colors);
                while (colorCount < colors.size() - 1) { //rotates list and prints colors, until entire list has been rotated once.
                    Collections.rotate(colors, 1);
                    System.out.println(prefix);
                    System.out.print(colors);
                }
            }
        }

    }

}



import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Cash
 *
 */
public class SequentialPrintDemonstration {
    private SequentialPrint colorSequence;
    private List colorsList;
    private List prefixList;

    /**
     * @param args
     */
    public SequentialPrintDemonstration() {

        List colorsList = new ArrayList();

        colorsList.add("blue");
        colorsList.add("green");
        colorsList.add("red");
        colorsList.add("yellow");
        colorSequence = new SequentialPrint(colorsList);
        System.out.println(colorsList);


    }

    public void execute() {
        this.colorSequence.printAllSequences(colorsList);
    }

}
  • 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-25T21:32:47+00:00Added an answer on May 25, 2026 at 9:32 pm

    Your class has a private data member named prefix, which you don’t initialize in the constructor:

    public class SequentialPrint {
        private List colors;
        private List prefix;
    
        public SequentialPrint(List colors){
            this.colors = colors;  // what if the array you pass in is null?
            // why not initialize prefix here?  it's null if you don't.
        }
    

    Then you have a method that declares a local variable List named prefix:

    public void printAllSequences(List colors){
        int prefixCount = 0;
        int colorCount = 0;
        List prefix = new ArrayList();  // this one shadows the private data member
    

    Do you mean to use the private data member here?

    Why do you pass in colors? How is that related to the private data member?

    I don’t understand this code after a quick glance. Do you?

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

Sidebar

Related Questions

I'm creating a generic list class that has a member of type Array(Array of
I am creating a simple reporting program using java and iReport (from jasper), which
I have a factory that is supposed to create objects that inherit from class
I am creating an application that is supposed to start a service which should
I'm creating an NPAPI plugin that isn't supposed to have a UI (for use
I'm creating a program counter that is supposed to use only unsigned numbers. I
I'm creating a wrapper for a Minecraft Server that is supposed to take new
I am creating a class that implements the composite pattern; the class is supposed
I am creating a SMS app the following code is supposed to: check if
Suppose that a process is creating a mutex in shared memory and locking it

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.