1

i have a issue making a SQLite edition, basicly, the error is java.lang.NumberFormatException, im making Long.parseLong in my data, but i dont know why the program continues sending me that error , the code of my first class is next:

public class SQLiteActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
EditText ecoursename, etopic, ecourse_description, elength, erating, esearch;
Button insert, view, search, edit, delete;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ecoursename = (EditText) findViewById(R.id.cName);
    etopic = (EditText) findViewById(R.id.cTopic);
    ecourse_description= (EditText) findViewById(R.id.cDescription);
    elength= (EditText) findViewById(R.id.cLength);
    erating= (EditText) findViewById(R.id.cRating);
    esearch = (EditText) findViewById(R.id.etSearch);


    insert = (Button) findViewById(R.id.btInsert);
    view = (Button) findViewById(R.id.btView);
    search = (Button) findViewById(R.id.btSearch);
    edit = (Button) findViewById(R.id.btEdit);
    delete = (Button) findViewById(R.id.btDelete);


    search.setOnClickListener(this);
    edit.setOnClickListener(this);
    delete.setOnClickListener(this);
    insert.setOnClickListener(this);
    view.setOnClickListener(this);

}

public void onClick(View v) {
    //TODO Auto-generated method stub
    switch (v.getId()){
    case R.id.btInsert:


        boolean works = true;
        try{
        String course_name = ecoursename.getText().toString();
        String topic = etopic.getText().toString();
        String course_description = ecourse_description.getText().toString();
        String length = elength.getText().toString();
        String rating = erating.getText().toString();

        ecoursename.setText("");
        etopic.setText("");
        ecourse_description.setText("");
        elength.setText("");
        erating.setText("");        

        DBAdapter entry = new DBAdapter (SQLiteActivity.this);
        entry.open();
        entry.createEntry(course_name, topic, course_description, length, rating);
        entry.close();
        }catch(Exception e){
            works = false;
            String error = e.toString();
            Dialog d = new Dialog(this);
            d.setTitle("Dont Works");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();


        }finally{
            if(works){

        // make click says inserted text
        Dialog d = new Dialog(this);
        d.setTitle("Works?");
        TextView tv = new TextView(this);
        tv.setText("is Working");
        d.setContentView(tv);
        d.show();

        }

        }

        break;

        case R.id.btView:           

        Intent i = new Intent("com.sqlite.base.data.SQLVista");
        startActivity(i);

        break;

        case R.id.btSearch:
            String s = esearch.getText().toString();
            Long ls = Long.parseLong(s);

            DBAdapter dbase= new DBAdapter(this);

            try{
            dbase.open();
            }catch(Exception e){

                e.printStackTrace();
            }
            String bN = dbase.getN(ls);
            String bT = dbase.getT(ls);
            String bD = dbase.getD(ls);
            String bL = dbase.getL(ls);
            String bR = dbase.getR(ls);

            dbase.close();

            ecoursename.setText(bN);
            etopic.setText(bT);
            ecourse_description.setText(bD);
            elength.setText(bL);
            erating.setText(bR);

            break;

        case R.id.btEdit:
            try{
            String eCo = ecoursename.getText().toString();
            String eTo = etopic.getText().toString();
            String eDe = ecourse_description.getText().toString();
            String eLe = elength.getText().toString();
            String eRa = erating.getText().toString();
            String eRow = esearch.getText().toString();
            long eRowl = Long.parseLong(eRow);
            DBAdapter edit = new DBAdapter(this);

            edit.open();
            edit.edit(eRowl, eCo, eTo, eDe, eLe, eRa);
            edit.close();

            }catch(Exception e) {
                String error = e.toString();
                Dialog d = new Dialog(this);
                d.setTitle("Dont Works!");
                TextView tv = new TextView(this);
                tv.setText(error);
                d.setContentView(tv);
                d.show();
            }

            break;

        case R.id.btDelete:
            break;  


    }
}
}

my DBAdapter class:

public class DBAdapter {

public static final String ID_ROW = "_id";
public static final String ID_COURSENAME = "course_name";
public static final String ID_TOPIC = "topic";
public static final String ID_DESCRIPTION = "course_description";   
public static final String ID_LENGTH = "length";
public static final String ID_RATING = "rating";

private static final String N_DB = "Clases";
private static final String N_TABLE = "Courses";
private static final int VERSION_DB = 1;

private DBHelper nHelper;
private final Context nContext;
private SQLiteDatabase nDB;



private static class DBHelper extends SQLiteOpenHelper{

    public DBHelper(Context context) {
        super(context, N_DB, null, VERSION_DB);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(" CREATE TABLE " + N_TABLE + "(" + ID_ROW + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ID_COURSENAME + " TEXT NOT NULL, " + ID_TOPIC + " TEXT NOT NULL, " + ID_DESCRIPTION + " TEXT NOT NULL, " + ID_LENGTH + " TEXT NOT NULL, " + ID_RATING + " TEXT NOT NULL);"


        );

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + N_TABLE);
        onCreate(db);           
    }

}

public DBAdapter (Context c){

    nContext = c;
}

public DBAdapter open() throws SQLException{
    nHelper = new DBHelper(nContext);
    nDB = nHelper.getWritableDatabase();
    return this;
}

public void close() {
    // TODO Auto-generated method stub
    nHelper.close();
}

public long createEntry(String course_name, String topic, String course_description, String length, String rating ) {
    // TODO Auto-generated method stub
    //insert data
    ContentValues cv = new ContentValues();

    cv.put(ID_COURSENAME, course_name);
    cv.put(ID_TOPIC, topic);
    cv.put(ID_DESCRIPTION, course_description);
    cv.put(ID_LENGTH, length);
    cv.put(ID_RATING, rating);

    //return content in table
    return nDB.insert(N_TABLE, null, cv);

}

public String receive() {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ID_ROW, ID_COURSENAME, ID_TOPIC, ID_DESCRIPTION, ID_LENGTH, ID_RATING};
    Cursor c = nDB.query(N_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iRow = c.getColumnIndex(ID_ROW);
    int cName = c.getColumnIndex(ID_COURSENAME);
    int cTopic = c.getColumnIndex(ID_TOPIC);
    int cDescription = c.getColumnIndex(ID_DESCRIPTION);
    int cLength = c.getColumnIndex(ID_LENGTH);
    int cRating = c.getColumnIndex(ID_RATING);

    //loop
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = result + c.getString(iRow) + " " + c.getString(cName) + " " + c.getString(cTopic) + " " + c.getString(cDescription) + " " + c.getString(cLength) + " " + c.getString(cRating) + "\n ";

    }

    return result;
}

public String getN(Long ls) {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ID_ROW, ID_COURSENAME, ID_TOPIC, ID_DESCRIPTION, ID_LENGTH, ID_RATING};
    Cursor c =nDB.query(N_TABLE, columns, ID_ROW + "=" + ls, null, null, null, null);
    if(c != null){
        c.moveToFirst();
        String ns = c.getString(1);
        return ns;
    }

    return null;
}

public String getT(Long ls) {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ID_ROW, ID_COURSENAME, ID_TOPIC, ID_DESCRIPTION, ID_LENGTH, ID_RATING};
    Cursor c =nDB.query(N_TABLE, columns, ID_ROW + "=" + ls, null, null, null, null);
    if(c != null){
        c.moveToFirst();
        String ts = c.getString(2);
        return ts;
    }

    return null;
}

public String getD(Long ls) {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ID_ROW, ID_COURSENAME, ID_TOPIC, ID_DESCRIPTION, ID_LENGTH, ID_RATING};
    Cursor c =nDB.query(N_TABLE, columns, ID_ROW + "=" + ls, null, null, null, null);
    if(c != null){
        c.moveToFirst();
        String nd = c.getString(3);
        return nd;
    }

    return null;
}

public String getL(Long ls) {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ID_ROW, ID_COURSENAME, ID_TOPIC, ID_DESCRIPTION, ID_LENGTH, ID_RATING};
    Cursor c =nDB.query(N_TABLE, columns, ID_ROW + "=" + ls, null, null, null, null);
    if(c != null){
        c.moveToFirst();
        String nl = c.getString(4);
        return nl;
    }

    return null;
}

public String getR(Long ls) {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ID_ROW, ID_COURSENAME, ID_TOPIC, ID_DESCRIPTION, ID_LENGTH, ID_RATING};
    Cursor c =nDB.query(N_TABLE, columns, ID_ROW + "=" + ls, null, null, null, null);
    if(c != null){
        c.moveToFirst();
        String nr = c.getString(5);
        return nr;
    }

    return null;
}

public void edit(long eRowl, String eCo, String eTo, String eDe,
        String eLe, String eRa) throws SQLException {
    // TODO Auto-generated method stub

    ContentValues cvEdit = new ContentValues();
    cvEdit.put(ID_COURSENAME, eCo);
    cvEdit.put(ID_TOPIC, eTo);
    cvEdit.put(ID_DESCRIPTION, eDe);
    cvEdit.put(ID_LENGTH, eLe);
    cvEdit.put(ID_RATING, eRa);
    nDB.update(N_TABLE, cvEdit, ID_ROW + "=" + eRowl, null);

}}

really would appreciate your help

1 Answer 1

2

Thanks for your screenshot.Could you please improve your code to this:

}catch(Exception e) {
            String error = e.getMessage(); //instead of String error = e.toString();
...

So we can see the actual cause. Can you please make the "don't works" dialog large? I think some text is being trimmed.

I believe you are trying to parse an empty string "".

Sign up to request clarification or add additional context in comments.

Comments

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.