Hi!

I'm trying to create String array from ArrayList. This task seems to be trivial, however I have a problem because of using "interface". The code and error message are shown below. Please, give me some advices to solve this problem. Thanks!

public class Class1
{
	public List<myInterface> getAllTasks()
	{
           List<myInterface> subjects = new ArrayList<myInterface>();
           ...
           return subjects;
        }
}

public interface myInterface
{
...
}

// THIS CODE PRODUCES THE ERROR MESSAGE SHOWN BELOW
Class1 myClass  = new Class1();
List<myInterface> listOfTasks = myClass.getAllTasks();
Exception in thread "main" java.lang.ArrayStoreException
	at java.lang.System.arraycopy(Native Method)
	at java.util.ArrayList.toArray(Unknown Source)
	at org.cellfex.test.gui.MySoft$1.<init>(MySoft.java:71)
	at org.cellfex.test.gui.MySoft.initComponents(MySoft.java:70)
	at org.cellfex.test.gui.MySoft.<init>(MySoft.java:26)
	at org.cellfex.test.gui.MySoft.main(MySoft.java:183)

Edited 6 Years Ago by LianaN: n/a

The documentation for ArrayStoreException says

Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects. For example, the following code generates an ArrayStoreException:

         Object x[] = new String[3];
         x[0] = new Integer(0);

So are you trying to store something in subjects that doesn't implement myInterface?

Yes, sorry, I forget to include the most important lines of code:

JList myList = new JList();

myList.setModel(new AbstractListModel() {
				String[] values = listOfTasks.toArray(new String[listOfTasks.size()]);
				public int getSize() { return values.length; }
				public Object getElementAt(int i) { return values[i]; }
			});

So, how could I create a String array from my ArrayList? I just have to fill JList with the ArrayList values.

Edited 6 Years Ago by LianaN: n/a

Which line produces the error? What's in myInterface? I think the problem might be that listOfTasks contains objects that implement myInterface, so how can they be represented as Strings?

The error is produced by:

String[] values = listOfTasks.toArray(new String[listOfTasks.size()]);

I've tried Object[] instead of String[], but it didn't help.

This is the code of myInterface:

public interface myInterface
{
	public String getResult1();

	public String getResult2();

	public boolean execute1();

	public boolean execute2();
}

So, probably it is because of boolean values that may be returned...

Edited 6 Years Ago by LianaN: n/a

How would you expect an object that implements myInterface to be represented as a String? Do you have an idea what the String should look like? If so, you can add a toString method to your interface. Then any class that implements myInterface will have to implement the toString method. This won't fix the problem immediately, but it will lead you in the right direction. The method in the interface should look like this:

public String toString();

Well, the following code works as I expected:

list1.setModel(new AbstractListModel() {
				Object[] values = listOfTasks.toArray();
				public int getSize() { return values.length; }
				public Object getElementAt(int i) { return listOfTasks.get(i).getName(); }
			});

Thanks!

Edited 6 Years Ago by LianaN: n/a

OK. I thought you said you tried Object[] and it didn't work, but glad you got it working.

This question has already been answered. Start a new discussion instead.