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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:47:00+00:00 2026-06-18T18:47:00+00:00

I have a problem with storing values in a multidimensional array. The main concept

  • 0

I have a problem with storing values in a multidimensional array. The main concept of the idea is that I have an arraylist which is called user_decide and I transform it in a array. So, the decide array looks like [1, 45, 656, 8, 97, 897], but all the rows don’t have the same number of elements. Then, I split this replace [,] and spaces and I would like to store each value individually in a 2D array. So, I split it with the “,” and try to store each value in a different position. Everything seems to be printed great, even the cut[j] is what I want to store, but I get a java.lang.NullPointerException, which I don’t get. The count variable is actually the count = user_decide.size()

String [] decide = user_decide.toArray(new String[user_decide.size()]);
for (int i = 0; i < count; i ++){
   decide[i] =
      decide[i].replaceAll("\\s", "").replaceAll("\\[","").replaceAll("\\]", "");
}
String [][] data1 = new String[count][];
for (int i = 0; i < count; i++){
   String [] cut = decide[i].split("\\,");
   for (int j = 0; j < cut.length; j++){
      System.out.println(cut[j]);
      data1[i][j] = cut[j];
   }
}

Another question is why I cannot store it in a Int [][] array? Is there a way to do that?

Thank you a lot.

** EDIT **

I just made an edit about my answer after I accepted the question. I am trying to store it in a 2D int array.

String [][] data1 = new String[user_decide.size()][];
int [][] data = new int [user_decide.size()][];

for (int i = 0; i < user_decide.size(); i++){
    data1[i] = decide[i].split("\\,");
    for (int j = 0; j < data1[i].length; j++) {
        data[i] = new int [data1[i].length]; 
        data[i][j] = Integer.parseInt(data1[i][j]);
        System.out.println(data1[i][j]);
    }
}
  • 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-18T18:47:01+00:00Added an answer on June 18, 2026 at 6:47 pm

    Ivaylo Strandjev’s answer shows the reason for your problem. But there’s a much simpler solution:

    String [][] data1 = new String[count][];
    for (int i = 0; i < count; i++){
      data1[i] = decide[i].split("\\,");
      System.out.println(Arrays.toString(data1[i]));
    }
    

    Also, you don’t need to escape the comma.

    EDIT

    Saw your edit. There is a big mistake, see my comment in your code:

    String [][] data1 = new String[user_decide.size()][];
    int [][] data = new int [user_decide.size()][];
    
    for (int i = 0; i < user_decide.size(); i++){
        data1[i] = decide[i].split("\\,");
        for (int j = 0; j < data1[i].length; j++) {
            data[i] = new int [data1[i].length]; // This line has to be prior to the
            // inner loop, or else you'll overwrite everything but the last number.
            data[i][j] = Integer.parseInt(data1[i][j]);
            System.out.println(data1[i][j]);
        }
    }
    

    If all you want is the int[], this is what I would do:

    int [][] data = new int [user_decide.size()][];
    
    for (int i = 0; i < user_decide.size(); i++){
        String[] temp = decide[i].split(",");
        data[i] = new int [temp.length];
        for (int j = 0; j < temp.length; j++){
            data[i][j] = Integer.parseInt(temp[j]);
            System.out.println(data1[i][j]);
        }
    }
    

    There are probably nicer ways, but I don’t know why you are using user_decide.size() ( a Collection) for the condition and decide[i] (an array) within the loop. There’s no good reason I can think of mixing this, as it could lead to errors.

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

Sidebar

Related Questions

I have a problem related to storing data dynamically in two Dimensional String Array
I am facing a problem storing original values of a form. I have a
I have a precision problem storing a float value in mysql via jpa (EclipseLink).
I have a problem, storing instances of a viewController object. I want every user
I'm having a problem relating to storing a variable within Javascript. I have a
I have a problem that output string must be utf8-formatted, I am writing currently
I have a problem with my database class. I have a method that takes
i have a problem with double values i need to store in an android
I have a problem, I am designing a database which will store different products
How to initialize a multidimensional array by loop operation in VB .Net? I have

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.