1

Is there a workaround for casting a String[] to Object[]? I keep getting an error when using this. I am trying to insert into a jTable and it gives me error saying I am trying to convert type String to type Object...

Here I am getting dates and inserting it into object months.

    // Create calendar and format to first sunday of the year.
    Calendar c;
    Object[] months = new String[52];
    c = Calendar.getInstance();
    c.set(Calendar.MONTH,0);
    c.set(Calendar.DATE, 5);
    // Format Date and insert into months object array
    DateFormat df = new SimpleDateFormat("MM/dd/yyy");
    for (int i =0; i < 52; i++){
        months[i] = df.format(c.getTime());
        c.add(Calendar.DATE, 7);
    }

    //Insert Months array into jComboBox
    jComboBox1.setModel(new DefaultComboBoxModel(months));
    ...
    ...

    //Action performed retrieves selection from jComboBox and insert into table

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           

        // object[] o gets selection from selectedItem()
        Object[] o = (Object[]) jComboBox1.getSelectedItem();

        //error checking to println
        System.out.println(jComboBox1.getSelectedItem() + "    ");

        // create DefaultTableModel and insert into row zero with the object selected
        DefaultTableModel model = (DefaultTableModel) weeklyCalendar.getModel();

        //insert into row, throws error
        model.insertRow(0, o);
    }

soo I guess I am getting a string from getSelectedItem() and trying to cast it to an Object[] with throws the error exception...what can I do to get around that?

mdewitt
  • 2,526
  • 19
  • 23
guy_without_a_name
  • 155
  • 1
  • 2
  • 13

3 Answers3

1

Use jComboBox1.getSelectedObjects() instead if you want an array.

Though not necessary for String, you may want to call .toString() on the returned Object when this line executes, just in case you happen to be storing Objects that are not String.

//error checking to println
System.out.println(jComboBox1.getSelectedItem() + "    ");

Note that the .getSelectedObjects() method wouldn't be the preferred one to use, as it's still only returning a single element in an Object[] array. If you need an array, it would be easier just to use .getSelectedItem() and store the result in an array after the fact.

*Edited to reflect comment feedback.

Ryan J
  • 8,275
  • 3
  • 25
  • 28
  • Ahh thank you! You very wise man! I guess if I would have done some research, I would have found out that I can get and Object. – guy_without_a_name Jan 23 '14 at 22:50
  • This is kind of erroneous, sorry. `getSelectedObjects` only returns a single element array and according to the doc is implemented for compatibility reasons with ItemSelectable. Also you don't need to call toString if an object is in a String concatenation expression: it is called automatically. – Radiodef Jan 23 '14 at 22:54
  • I agree, but thought I attempted to answer the question based on what he was trying to do. The toString() call I usually put in just to be consistent, such that if you have an object other than String being stored, you can still do a valid print. Thanks for the feedback. – Ryan J Jan 23 '14 at 22:57
0

You can't cast the complete String array to Object Array. You will have to cast elements individually. Create a method to cast all elements of type String to Object.

Vivek Vermani
  • 1,934
  • 18
  • 45
  • 2
    Even if this were the problem this is not necessarily true because arrays are covariant in Java. So for example `Object[] objs = new String[] { "hello" }; String[] strs = (String[])objs;` is a valid cast. On the other hand, `Object[] objs = { "hello" }; String[] strs = (String[])objs;` *will* throw an exception. – Radiodef Jan 23 '14 at 22:49
0

The two important lines are here:

jComboBox1.setModel(new DefaultComboBoxModel(months));

Object[] o = (Object[]) jComboBox1.getSelectedItem();

You aren't trying to cast a String[] to an Object[] but rather a String to an Object[] since getSelectedItem returns a single item.

I believe what you are wanting is just this:

Object o = jComboBox1.getSelectedItem();

Or this which will be a valid cast:

String s = (String)jComboBox1.getSelectedItem();
Radiodef
  • 37,180
  • 14
  • 90
  • 125