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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:44:54+00:00 2026-06-05T17:44:54+00:00

I am trying to initialize a vector which has integers 1 to n in

  • 0

I am trying to initialize a vector which has integers 1 to n in the form of strings for starters.

This is my declaration for the vector.

 Vector<String> candidatesSet,frequentItemSet,mFCandidatesSet,mFSet = new <String>Vector();

The loop i using to initialize is

for(int i=0; i<crows; i++)
        {
           candidatesSet.add(Integer.toString(i+1));
        }

Here we get the value of variable crowsduring runtime.

but it is throwing a NullpointerException in the line where i am adding strings to the objects.

I tried intializing the vector to a null by

 candidatesSet = null;

But it didnt 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-06-05T17:44:56+00:00Added an answer on June 5, 2026 at 5:44 pm

    First of all, something like is wrong:

    Vector<String> set = new <String>Vector();
    

    The correct syntax is this:

    Vector<String> set = new Vector<String>();
    

    Second of all, if you do something like this:

    Vector<String> set1,set2,set3,set4,set5 = new Vector<String>();
    

    …only set5 will be initialized. Each variable must be initialized independently. You could do something like this:

    Vector<String> set1,set2,set3,set4,set5;
    set1 = set2 = set3 = set4 = set5 = new Vector<String>();
    

    …but then all of the variables would point to the same Vector, and modifications to one variable would affect all the others. You’ll have to initialize each variable separately.

    Third, doing this:

    candidatesSet = null;
    

    …does nothing if candidatesSet is not initialized yet, since non-primitive instance variables are initialized to null anyway. That’s your problem, you’re calling .add(String) on a null object, which cases a NullPointerException.

    Fixing those issues will make your code work, but there’s one last problem. Vector is a somewhat outdated class, and it has been replaced by the Java Collections API. Try using ArrayList instead of Vector, like so:

    List<String> candidatesSet = new ArrayList<String>();
    

    This will make your code more efficient and less archaic.

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

Sidebar

Related Questions

I'm trying to initialize string with iterators and something like this works: ifstream fin(tmp.txt);
I'm trying to get CMtoaPlugin::listArnoldNodes() to return an array of strings std::vector<std::string> ArnoldNodes =
I'm trying to design a class which has two vectors of large sequences. std::vector<double>
trying to initialize a string from a vector. I am supposed to get hey
I'm trying to initialize a dictionary with string elements as keys and int[] elements
So i'm trying to initialize a variable in my opencl host code like this:
I have been trying to initialize 1 vector and 1 map in a class.
I am trying to initialise an std::vector<std::unique_ptr<std::string>> in a way that is equivalent to
I have this declaration of a multidimensional vector std::vector< vector < vector < ofxImage
Am trying to inherit a class from a C++ vector and initialize it at

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.