2

Hi I retrieve data and one Float value from sqlite and return in arraylist:

if (!cursor.isAfterLast())
        {
            do
            {
                ....
                rowArray.add(cursor.getFloat(2));
                ....
            }
            while (cursor.moveToNext());
        }

Problem occurred in float cast, when I try to set float variable from the list:

ArrayList<Object> row;
        row = db.getNameAsArray(name);

        mCurrentPrice=((Float)row.get(2)).floatValue();

There is no error but app FC in this line. Simple float cast doesnt work neither. Thx for any answer.

Logcat:

02-07 22:34:19.537: ERROR/Retrieve Error(632): java.lang.ClassCastException: java.lang.String

02-07 22:34:21.217: WARN/System.err(632): java.lang.ClassCastException: java.lang.String 02-07 22:34:21.267: WARN/System.err(632): at com.testing.retrieveDrinkName(DatabaseActivity.java:222) 02-07 22:34:21.527: WARN/System.err(632): at android.os.Handler.handleCallback(Handler.java:587) 02-07 22:34:21.547: WARN/System.err(632): at android.os.Handler.dispatchMessage(Handler.java:92) 02-07 22:34:21.596: WARN/System.err(632): at android.os.Looper.loop(Looper.java:123) 02-07 22:34:21.657: WARN/System.err(632): at android.app.ActivityThread.main(ActivityThread.java:4363) 02-07 22:34:21.667: WARN/System.err(632): at java.lang.reflect.Method.invokeNative(Native Method) 02-07 22:34:21.737: WARN/System.err(632): at java.lang.reflect.Method.invoke(Method.java:521) 02-07 22:34:21.769: WARN/System.err(632): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 02-07 22:34:21.797: WARN/System.err(632): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 02-07 22:34:21.817: WARN/System.err(632): at dalvik.system.NativeStart.main(Native Method)

8
  • 1
    Use logcat to see the error causing the FC. To see logcat, switch to the DDMS perspective in eclipse or run adb logcat on the command line. Commented Feb 7, 2011 at 22:20
  • So, a method named "getNameAsArray" returns float values? Why? Commented Feb 7, 2011 at 22:21
  • method return Array of Doubles, Strings and Floats values. Commented Feb 7, 2011 at 22:23
  • logcat show java.lang.ClassCastException java.lang.String Commented Feb 7, 2011 at 22:32
  • 1
    Since you have an array of Objects perhaps you would be better off doing an instanceof check before the cast? My bet is that you are not getting a Float object, and are trying to cast something else. Please provide your logcat output. Commented Feb 7, 2011 at 22:33

1 Answer 1

4

As your last comment shows, you are trying to cast a String to a Float. Your code doesn't make it clear exactly the problem you are trying to solve, but if you have a String value and would like a float, you can do:

mCurrentPrice = Float.parseFloat(stringValue);
Sign up to request clarification or add additional context in comments.

3 Comments

@Hruskozrout Yes, the stacktrace shows that it is a ClassCastException. You can't turn a String into a float by casting it, you need to use the parse method as dave.c describes.
Thanks dave.c and Mayra, with dave.c method its works, I was thinking when do rowArray.add(cursor.getFloat(2)); its Float it looks like all row is represented by String values.. I must review my code again. Thanks for your time.
would the downvoter care to explain how they feel this answer could be improved?

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.