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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:10:19+00:00 2026-05-26T09:10:19+00:00

Trying to write algorithm for subsetSum… It should find all possible subsets of a

  • 0

Trying to write algorithm for subsetSum… It should find all possible subsets of a given vector, then find which ones add up to a target value. However, I keep getting nullpointerexceptions, and a few other errors. Can someone help me out? I’m in a tight spot, brain is hardly functioning. Much appreciated.
Thanks.

java.lang.NullPointerException
at Sumation.subsetSum(Sumation.java:78)
at Sumation.main(Sumation.java:110)

Line 78 is line of first for loop in subsetSum method.

/* Ruben Martinez
 * CS 210 Data Structures
 * Program requires no paramters at main method call;
 * instead, a JOptionPane asks for the ints the user
 * wishes to search. These must be seperated by commas,
 * e.g. 50,40,30 or 35,45,55. Program then asks for a 
 * target int to find. Program searches for target
 * and returns combinations that add up to the target.
 */

import java.util.Vector; 
import javax.swing.*;

public class Sumation
{
    static int[] array;
    static int target;
    static Vector<Integer> subsets;
    static Vector<Integer> set;
    static Vector<Vector<Integer>> outer;

    public Sumation() {
        //insert integers into array
        String defineArray = (String)JOptionPane.showInputDialog(null,
                "Enter integers to search. Seperate by commas.", null);
        //splits string into array delimeted by commas
        String[] arrayString = defineArray.split(",");
        //creates int array of size of string array
        array = new int[arrayString.length];
        //adds ints from args[] to int array
        for (int i = 0; i < arrayString.length; i++) {
            array[i] = Integer.parseInt(arrayString[i]);
        }
        //enter integer to search for
        String targetString = (String)JOptionPane.showInputDialog(null,
                "What is your target integer?", null);
        //turns string to int
        target = Integer.parseInt(targetString);


        set = new Vector<Integer>();
        for (int n = 0; n < array.length; n++) {
            set.add(array[n]);
        }
    }

    private static Vector<Vector<Integer>> getSubsets(Vector<Integer> set) {
        Vector<Vector<Integer>> subsetCollection = new Vector<Vector<Integer>>();

        if (set.size() == 0) {
            subsetCollection.add(new Vector<Integer>());
        } else {
            Vector<Integer> reducedSet = new Vector<Integer>();
            reducedSet.addAll(set);

            int first = reducedSet.remove(0);
            Vector<Vector<Integer>> subsets = getSubsets(reducedSet);
            subsetCollection.addAll(subsets);

            subsets = getSubsets(reducedSet);

            for (Vector<Integer> subset : subsets) {
                subset.add(0, first);
            }

            subsetCollection.addAll(subsets);
        }

        return subsetCollection;
    }

    public static Vector<Vector<Integer>> subsetSum(Vector<Integer> subsets, int target) {
        //creates outer vector
        outer = new Vector<Vector<Integer>>();

        for (int k = 0; k < subsets.size(); k++) {
            //if k is the target, display
            if (array[k] == target) {
                //creates new inner vector for values that equal target
                Vector<Integer> inner = new Vector<Integer>();
                outer.add(inner);
                //add k to vector
                inner.add(array[k]);
            }
            for (int l =0; l < subsets.size(); l++) {
                int sum = subsets.elementAt(k);
                if (sum == target) {
                    //creates new inner vector for values that sum up to target
                    Vector<Integer> inner = new Vector<Integer>();
                    outer.add(inner);
                    //add l,k to vector
                    inner.add(array[l]);
                    inner.add(array[k]);
                }
                else {
                    System.out.print("");
                }
            }
        }
        //return combinations that add up to target in vector form
        return outer;
    }

    public static void main(String[] args) {
        //calls sumation constructor
        Sumation s = new Sumation();
        s.getSubsets(set);
        s.subsetSum(subsets, target);
        JOptionPane.showMessageDialog(null, "The combinations that equal to "+target+" are \n"+outer, "Vector", JOptionPane.INFORMATION_MESSAGE);
    }
}
  • 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-26T09:10:19+00:00Added an answer on May 26, 2026 at 9:10 am

    How have you tried to analyse this?

    If you have a debugger (i.e. IDE) on hand, it’s very easy to analyse problems like this. Depending on your preference, you could

    • Step through the program from start to finish
    • Set a breakpoint on the line where the exception’s thrown
    • Set a breakpoint for NullPointerExceptions

    and in all cases you’d easily see what the state of the variables/fields are all all points. If you get into an unexpected state it’s easy to rewind or rerun the program to see the results of previous calculations and watch for where it deviates from your expectations.

    Even if you don’t have a debugger to hand, you could put println statements in every so often (and definitely just before the exception line) so as to see what the values are – a poor man’s debugger.

    But seriously, get a real debugger set up if you’re going to be doing any non-trivial Java dev. If will take 5-20 minutes and will save you more than that the first time you have to investigate a problem like this.


    Anyway, the problem is caused by the fact that you never assign anything to the subsets field, so it’s null when you pass it into subsetSum in main.

    This might be because the subset local variable within the getSubsets method shadows it – if you were expecting that method to modify the field then you’d be mistaken. But then, that would be bad practice anyway (IMHO), as changing state like that in a recursive method would be very easy to get wrong, and even if correct would make it hard to reason about the class at any point (as you’d need to know what state it was in). Assigning results to fields is a common beginner’s mistake (possibly because it feels more object-oriented?), when the better option is to have a method return its result, which will then usually be stored in a local variable.

    So the main method should assign the result of the getSubsets call to a local variable, and then pass that into the subsetSum method.

    (I notice that the types aren’t entirely compatible – are you sure that the argument to subsetSum shouldn’t be a Vector<Vector<Integer>> too? At the moment it looks like you could only pass in a single subset. But that’s a different matter.)

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

Sidebar

Related Questions

I'm trying to write an algorithm that will find the set of all vertices
I am trying to write a linear-time algorithm O(n), which given a table A[0..n-1]
I'm trying to write a combinatorics algorithm to get all the possible combinations of
I'm trying to write C++ code which will compute all possible orderings of groups
I'm trying write a query to find records which don't have a matching record
I was trying to write an algorithm for given problem: we are given a
I'm trying to write an algorithm (which I'm assuming will rely on natural language
I'm trying to write an algorithm to generate the ceiling panel from a horiontally
I am trying to write an efficient algorithm that will effectively let me merge
I am trying to write a small application using bouncycastle algorithm, from the BouncyCastleProvider.java

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.