8

//edit: I changed my ArrayList conversion, but I am unable to load the second spinner

What am I doing wrong. I would like to fill a second spinner with an array of w depending on the row selected in the first spinner, but am getting this warning when trying to convert from array list.

***I have this marked in code Null Pointer Exception 11-06 19:03:34.050: WARN/System.err(5342): at RetrievingAmazonXMLDataActivity.onCreate(RetrievingAmazonXMLDataActivity.java:88)

// edited code

for (int i = 0; i < tokens.length; i++) {
            String a = dumpTitles("ProductName", i);
            element = a.split("!");
            allProducts.add(element);
        }

       w =  (String[][])allProducts.toArray(new String[allProducts.size()][]);


Spinner spinnerProducts = (Spinner) findViewById(R.id.spinner2);
        spinnerProducts.setOnItemSelectedListener(this);

// ** error line below

 ArrayAdapter<String> productsArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, productArrayToShow);

       productsArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerProducts.setAdapter(productsArrayAdapter);

 public void onItemSelected(AdapterView<?> parent, View v, int position, long id)
    {

        try {
            selected = parent.getItemAtPosition(position).toString();


            productArrayToShow = w[position];

        } catch (Exception e) {}
    }

// original code

ArrayList<String[]>allProducts = new ArrayList<String[]>();
    for (int i = 0; i < tokens.length; i++) {
        String a = dumpTitles("ProductName", i);
        element = a.split("!");
        allProducts.add(element);
    }


    String[][] w; = new String[allProducts.size()][];
        for (int a = 0; a<allProducts.size(); a++) {
            w[a] = (String[])allProducts.toArray(new String[allProducts.size()]);
        }
1
  • Once a question has been answered, don't change it into a question about the next problem you run into. Post a new question. Commented Nov 7, 2011 at 1:25

4 Answers 4

9

Why not just

String[][] w = allProducts.toArray(new String[][] {});
Sign up to request clarification or add additional context in comments.

Comments

6

It looks like you changed your mind about how you were going to do the conversion half way through. Your code is a bit of this:

String[][] w = new String[allProducts.size()][];
for (int a = 0; a<allProducts.size(); a++) {
  w[a] = allProducts.get(a);
}

and a bit of that:

String[][] w = allProducts.toArray(new String[allProducts.size()][]);

Either will work, though the latter is shorter, and possibly more efficient.

2 Comments

The (String[][]) casting is redundant. You don't need that.
@tom You are correct. I have updated the answer. Thanks!
1

I think you can get by with just this:

String[][] w = allProducts.toArray(new String[allProducts.size()]);

Comments

0

Make them of the same datatype an "ArrayList" and a "Array of Strings" they are not of the same datatype, so they wont be compartable with each other. And please comment where the problem is.

9 Comments

like i have sad try to make sure you convert them to the same data types. For instance w[a] = Convert.ToString(allProducts[a])
Right, and that was the question--answering the "how do I convert these" with "you need to convert them" isn't that useful, that's all I'm saying.
it is very very usefull buddy. you have to convert them to the same type. if it is from ArrayList to String[] make sure the values are converted to Strings.
... I'll try one more time. The original question was "how do I convert?" The answer isn't "you need to convert", the poster is already trying to convert, and was asking how. There's no conversion to strings involved, the issue is converting from List<String[]> to String[][]. They're asking how.
b patient with me Dave.try this code and see if it is not converting like i was saying. List<string> allProducts = new List<string>(); allProducts.Add("12"); String[] w = new String[1]; w[0] = allProducts[0]; Console.WriteLine(allProducts[0]); Console.ReadKey();
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.