2

I have the following code which does nothing but reading some values from a DB in a background thread and using the values I draw a Line chart using a jar.For the line chart I use for value for each array and the problem is that the third that I pass to the constructor that draws the LineChart is float....


float[] viteza;

String[] time;

int contor=0;

public void onResume() {

    super.onResume();

    init_task = new InitTask();

    init_task.execute(db);

}

public class InitTask extends AsyncTask<DBAdapter,String, Void> {

    String TABLE_3;

    protected Void doInBackground(DBAdapter... db) {
        try {
            db[0].createDatabase();
            db[0].openDataBase();
            Cursor c = db[0].getCursor3(db[0].TABLE_3, user_id);

            String[] array=new String[2];
            viteza = new float[c.getCount()];
            time = new String[c.getCount()];

            if (c.moveToFirst()) {

                do {


                    publishProgress(c.getString(3),c.getString(4));
                    Thread.sleep(500);


                } while (c.moveToNext());

            }
            c.close();
            db[0].close();

        } catch (Exception e) {
            Log.d("Eroare", "doInBackground", e);
        }

        return null;
    }

    protected void onProgressUpdate(String...values) {

        Aitem items[] = new Aitem[1];

        viteza[contor]=Float.valueOf(values[0]);
        time[contor]=values[1];
        if(contor>3)
        {
        items[0]=new Aitem(Color.RED, "Evolution",viteza);
        lv.setTitle("Evolutia vitezei");

        lv.setAxisValueX(time);

        setContentView(lv);
        }
        contor++;
    }

}



}

I get error at this line:

viteza[contor]=Float.valueOf(values[0]);


java.lang.NumberFormatException: 
   at org.apache.harmony.luni.util.FloatingPointParser.parseFltImpl(Native Method)
 at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:321)

Has anyone any clue what is gooing wrong???

EDIT:

In my humble opinion values[0]=c.getString(3) and values[1]=c.getString(4) so ....are Strings...aren't they?

Ishtar
  • 11,542
  • 1
  • 25
  • 31
adrian
  • 4,574
  • 17
  • 68
  • 119
  • 1
    You left out the most important part: what's the value of `values[0]`? – dmon Jun 01 '11 at 19:59
  • =)))....when I posted this I said I have to put all the variables I use...so THEY can fgure out what I'm doing wrong and yet...I'll edit my question;)) – adrian Jun 01 '11 at 20:01

3 Answers3

1

I'm not sure just by looking at this, but I can tell you, this is where Log.v is extremely handy. Put a Log.v(TAG,"\""+values[]+"\""), and you'll see exactly what's getting to that function. If it's anything other than a pure number, that'd be what's causing the exception to occur.

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
  • (601): "214.721" (601): "0" I assume that there are numbers...only this I got displayed after that FC came;))) – adrian Jun 01 '11 at 20:06
  • @george - Do you mean that `values[0]` contains exactly `214.721`? It it the same as doing `values[0] = "214.721";`? – Ishtar Jun 01 '11 at 20:13
  • values[0] in my DB is 214.721099853516 but in my logcat...using Log.v()....it displays "214.721"....but I assume that " "...comes from "+values[]+" – adrian Jun 01 '11 at 20:16
  • @george - You could try `Log.v(TAG,values[0]);`, so it will be without `"`'s. – Ishtar Jun 01 '11 at 20:28
  • I TRIED....THERE ARE CORRECT NUMBERS DISPLAYED.....SOMEWHERE ELSE IS THE PROBELM..... – adrian Jun 01 '11 at 20:31
  • @george: It seems like it should be working. The parenthesis are to help make sure there isn't a null or space messing things up, which seems to be good. Hmmmm... Did you put this before the floating point conversion statement? – PearsonArtPhoto Jun 02 '11 at 00:19
  • @george: Did the logging help discover that? – PearsonArtPhoto Jun 02 '11 at 13:11
  • Yes....it did....but now I have another issue...in which the logging can't help:| – adrian Jun 02 '11 at 16:05
0

Use try catch block where you are parsing string into int

try{
// todo

}

catch( NumberFormatException ex ) {

}
Himanshu
  • 31,810
  • 31
  • 111
  • 133
Drw
  • 470
  • 8
  • 19
0

Obviously the first element isn't a proper string containing a float.

So obviously the content of c.getString(3) must be wrong. Check that this is a string, and that this string contains strictly a float. Check for invalid characters, spaces, etc.

Emmanuel
  • 16,791
  • 6
  • 48
  • 74