Question

Im not to sure how to fix this since my code is String[] and not Object[] How would i go about fixing this error?

Here is s the full logcat:

06-14 00:25:40.356: W/dalvikvm(13023): threadid=1: thread exiting with uncaught exception (group=0x40aa41f8)
06-14 00:25:40.356: E/AndroidRuntime(13023): FATAL EXCEPTION: main
06-14 00:25:40.356: E/AndroidRuntime(13023): java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]
06-14 00:25:40.356: E/AndroidRuntime(13023):    at com.example.drawingarrays.OutPutConversion.Out(OutPutConversion.java:7)
06-14 00:25:40.356: E/AndroidRuntime(13023):    at com.example.drawingarrays.MainActivity.onClick(MainActivity.java:64)
06-14 00:25:40.356: E/AndroidRuntime(13023):    at android.view.View.performClick(View.java:3540)
06-14 00:25:40.356: E/AndroidRuntime(13023):    at android.view.View$PerformClick.run(View.java:14167)
06-14 00:25:40.356: E/AndroidRuntime(13023):    at android.os.Handler.handleCallback(Handler.java:605)
06-14 00:25:40.356: E/AndroidRuntime(13023):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-14 00:25:40.356: E/AndroidRuntime(13023):    at android.os.Looper.loop(Looper.java:137)
06-14 00:25:40.356: E/AndroidRuntime(13023):    at android.app.ActivityThread.main(ActivityThread.java:4586)
06-14 00:25:40.356: E/AndroidRuntime(13023):    at java.lang.reflect.Method.invokeNative(Native Method)
06-14 00:25:40.356: E/AndroidRuntime(13023):    at java.lang.reflect.Method.invoke(Method.java:511)
06-14 00:25:40.356: E/AndroidRuntime(13023):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-14 00:25:40.356: E/AndroidRuntime(13023):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-14 00:25:40.356: E/AndroidRuntime(13023):    at dalvik.system.NativeStart.main(Native Method)

Here is my code:

public class OutPutConversion {
DrawingTools GAL = new DrawingTools();

public void Out() {
    String[] Result = (String[]) GAL.getAL().toArray();
    float[] vertices = new float[Result.length];
    for (int i = 0; i < vertices.length; i++) {
        vertices[i] = Float.valueOf(Result[i]);

    }
  }
}

Here is the DrawingTools Class:

public class DrawingTools {
ArrayList<String> AL = new ArrayList<String>();

public ArrayList<String> getAL() {
    return AL;
}

public void AddLine(String Input1) {
    String[] pStrings = Input1.split(",");
    for (int i = 0; i < pStrings.length; i++) {
        AL.add(pStrings[i]);
    }

}
Was it helpful?

Solution

GAL.getAL() returns an ArrayList<String>, nevertheless ArrayList.toArray() returns Object[] whatever its generic type is (see API) and you cannot cast it to String[], try GAL.getAL().toArray(new String[0]) instead

OTHER TIPS

You can not cast an entire array, so you have to convert it, or cast it inside the loop appropriately. Or in this case no cast at all.

public void Out() {
    Object[] Result = GAL.getAL().toArray();
    float[] vertices = new float[Result.length];
    for (int i = 0; i < vertices.length; i++) {
        vertices[i] = Float.valueOf(Result[i].toString());

    }
}

Arrays do not follow inheritance rules of their elements. The fact that String extends Object does not mean that String[] extends Object[].

To "cast" from Object[] to String[] you have to create new string array, iterate over elements of object array and copy element-by element with casting exactly as you do this for float.

so, the simplest fix to your code is just do the following:

Object[] Result = GAL.getAL().toArray();
float[] vertices = new float[Result.length];
for (int i = 0; i < vertices.length; i++) {
    vertices[i] = Float.valueOf(String.valuOf(Result[i]));
}

However as far as I understand getAL() method returns Collection<String>. So, you can:

 String[] Result = GAL.getAL().toArray(new String[0]);
    float[] vertices = new float[Result.length];
    for (int i = 0; i < vertices.length; i++) {
        vertices[i] = Float.valueOf(Result[i]);
    }

or even just directly iterate the collection:

Collection<String> Result = GAL.getAL();
float[] vertices = new float[Result.size()];
for (int i = 0; i < vertices.length; i++) {
    vertices[i] = Float.valueOf(Result[i]);
}

And the last tip. There is naming convention in java. All variables and methods names start with small letter. All classes names start with capital letter. Start follow this convention now. Otherwise your code will be unreadable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top