0

i try to show data from my sqlitedatabase in listview using adapter but error occurs table not found and nullpointerexception , please reply fast .. thank you

`package utils;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;



import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;


public class DBhelper extends SQLiteOpenHelper {



private final static String DATABASE_NAME = "dataofg.sqlite";
private String DATABASE_PATH = "data/data/com.example.gindicator/databases/";

private final static int DATABASE_VERSION = 4;

private Context mContext;
private SQLiteDatabase sqliteDatabase = null;

public DBhelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

    mContext = context;

    // TODO Auto-generated constructor stub
}

public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if (dbExist) {
    } else {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

private boolean checkDataBase() {

    try {
        String myPath = DATABASE_PATH + DATABASE_NAME;
        sqliteDatabase = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    } catch (SQLiteException e) {
    }
    if (sqliteDatabase != null) {
        sqliteDatabase.close();
    }
    return sqliteDatabase != null ? true : false;
}

private void copyDataBase() throws IOException {

    InputStream myInput = mContext.getAssets().open(DATABASE_NAME);
    String outFileName = DATABASE_PATH + DATABASE_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[1024];
    int length;

    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    myOutput.flush();
    myOutput.close();
    myInput.close();
}

public void openDataBase() throws SQLException {
    String myPath = DATABASE_PATH + DATABASE_NAME;
    sqliteDatabase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);
}

public DBhelper open() throws SQLException {
    sqliteDatabase = getWritableDatabase();
    return this;
}

@Override
public synchronized void close() {

    if (sqliteDatabase != null)
        sqliteDatabase.close();
    super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}



public void GetStationData()  {
    String StrQuery = "Select * from tblStations";

    Constant.ArrStationName.clear();
    open();
    Cursor cursor = sqliteDatabase.rawQuery(StrQuery, null);
    //Log.e("cursor", "" + cursor.getCount());
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
        do {

    // Log.i("City","" +               
   cursor.getString(cursor.getColumnIndex("strStationName")));
            Constant.ArrStationName.add(cursor.getString(cursor
                    .getColumnIndex("strStationName")));
        } while (cursor.moveToNext());

    }
    cursor.close();
close();

}`

it's DBhelper calss and below i defined array

`package utils;

import java.util.ArrayList;

public class Constant {


public static ArrayList<String> ArrStationName = new ArrayList<String>();
}

` adapter class

`package com.gindicator.adapter;



import com.example.gindicator.R;

import utils.Constant;
import android.content.Context;
import android.view.LayoutInflater;    
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CitybusAdapter extends BaseAdapter {
Context context;
LayoutInflater LI;

public CitybusAdapter(Context contextpara) {
    context = contextpara;
    LI = LayoutInflater.from(contextpara);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return Constant.ArrStationName.size();
}

@Override
public int getViewTypeCount() {
    // TODO Auto-generated method stub
    return Constant.ArrStationName.size();
}

@Override
public int getItemViewType(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    if (convertView == null) {
        convertView = LI.inflate(R.layout.city_raw, null);
        TextView txtCityRaw = (TextView) convertView
                .findViewById(R.id.txtCityRaw);
        txtCityRaw.setText(Constant.ArrStationName.get(position));

    }
    return convertView;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

}`

main class where i display data from my database

`package com.example.gindicator;

import java.io.IOException;

import com.gindicator.adapter.CitybusAdapter;



import utils.Constant;
import utils.DBhelper;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;


public class Citybusbtn1 extends Activity{
public static DBhelper myDbHelper;
ListView lst ;
CitybusAdapter cityobject;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.citybusbtn1);


        cityobject = new CitybusAdapter(Citybusbtn1.this);
        DBhelper myDbHelper = new DBhelper(this);

        try {
            myDbHelper.createDataBase();

            // myDbHelper.openDataBase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            myDbHelper.GetStationData();


        Initialization();


    }



    private void Initialization() {
    // TODO Auto-generated method stub

        if (Constant.ArrStationName.size() > 0) {
            lst=(ListView)findViewById(R.id.hotellist);
            lst.setAdapter(cityobject);
        }
}




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
`

when 1st time i run program it's show error like this in log and stop program while running in 1st page

    04-16 11:27:45.702: D/dalvikvm(14822): GC_CONCURRENT freed 0K, 7% free    
     16033K/17223K,      
  paused 1ms+1ms, total 34ms
   04-16 11:27:45.702: D/dalvikvm(14822): WAIT_FOR_CONCURRENT_GC blocked 15ms
   04-16 11:27:45.706: E/SQLiteLog(14822): (14) cannot open file at line 30174 of        
  [00bb9c9ce4]
  04-16 11:27:45.706: E/SQLiteLog(14822): (14) os_unix.c:30174: (2) 
  open(//data/data/com.example.gindicator/databases/dataofg.sqlite) - 
 04-16 11:27:45.709: E/SQLiteDatabase(14822): Failed to open database 
  'data/data/com.example.gindicator/databases/dataofg.sqlite'.
 04-16 11:27:45.709: E/SQLiteDatabase(14822):   
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could 
not    
open database

and after it's show error like this logcat whwn i clicked button which show listview of data

04-16 11:15:02.925: V/Provider/Setting(14024): from settings cache , name =                

 sound_effects_enabled value = 0
04-16 11:15:02.986: D/AbsListView(14024): checkAbsListViewlLogProperty get invalid     

command
 04-16 11:15:03.002: E/SQLiteLog(14024): (1) no such table: tblStations
  04-16 11:15:03.002: D/AndroidRuntime(14024): Shutting down VM
  04-16 11:15:03.003: W/dalvikvm(14024): threadid=1: thread exiting with uncaught             
  exception (group=0x40f61908)
  04-16 11:15:03.011: E/AndroidRuntime(14024): FATAL EXCEPTION: main  
  04-16 11:15:03.011: E/AndroidRuntime(14024): java.lang.RuntimeException: Unable to    
   start activity 
   ComponentInfo{com.example.gindicator/com.example.gindicator.Citybusbtn1}: 

     android.database.sqlite.SQLiteException: no such table: tblStations (code 1): , 

      while compiling: Select * from tblStations

      04-16 11:15:03.011: E/AndroidRuntime(14024):  at 

      android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2189)


        04-16 11:15:03.011: E/AndroidRuntime(14024):    at 

          android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2216)


          04-16 11:15:03.011: E/AndroidRuntime(14024):  at 

         android.app.ActivityThread.access$600(ActivityThread.java:149)
 04-16 11:15:03.011: E/AndroidRuntime(14024):   at         
  android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at 
 android.os.Handler.dispatchMessage(Handler.java:99)
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at 
  android.os.Looper.loop(Looper.java:153)
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at         
  android.app.ActivityThread.main(ActivityThread.java:5000)
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at 
 java.lang.reflect.Method.invokeNative(Native Method)
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at
   java.lang.reflect.Method.invoke(Method.java:511)

   04-16 11:15:03.011: E/AndroidRuntime(14024):     at 
  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at    
   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)

   04-16 11:15:03.011: E/AndroidRuntime(14024):     at 
  dalvik.system.NativeStart.main(Native Method)
   04-16 11:15:03.011: E/AndroidRuntime(14024): Caused by:       
  android.database.sqlite.SQLiteException: no such table: tblStations (code 1): , while          
   compiling: Select * from tblStations
   04-16 11:15:03.011: E/AndroidRuntime(14024):     at 

   android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
     04-16 11:15:03.011: E/AndroidRuntime(14024):   at 

    android.database.sqlite.SQLiteConnection.acquirePreparedStatement
  (SQLiteConnection.java:882  
      )
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at        
   android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
    04-16 11:15:03.011: E/AndroidRuntime(14024):    at 
     android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    04-16 11:15:03.011: E/AndroidRuntime(14024):    at      
   android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
    04-16 11:15:03.011: E/AndroidRuntime(14024):    at 
     android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
     04-16 11:15:03.011: E/AndroidRuntime(14024):   at 
     android.database.sqlite.SQLiteDirectCursorDriver.query
  (SQLiteDirectCursorDriver.java:44)
   04-16 11:15:03.011: E/AndroidRuntime(14024):     at       
   android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
    04-16 11:15:03.011: E/AndroidRuntime(14024):    at   
      android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)

    04-16 11:15:03.011: E/AndroidRuntime(14024):    at 
    utils.DBhelper.GetStationData(DBhelper.java:123)
   04-16 11:15:03.011: E/AndroidRuntime(14024):     at    
   com.example.gindicator.Citybusbtn1.onCreate(Citybusbtn1.java:40)

   04-16 11:15:03.011: E/AndroidRuntime(14024):     at 
  android.app.Activity.performCreate(Activity.java:5020)
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at 
  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at   
  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
 04-16 11:15:03.011: E/AndroidRuntime(14024):   ... 11 more
 04-16 11:15:04.696: I/Process(14024): Sending signal. PID: 14024 SIG: 9

thank you frnds

Ujesh
  • 1,698
  • 2
  • 23
  • 35
  • db table tblStations is exactly there? – xoxol_89 Apr 16 '14 at 06:28
  • yes .. in assets folder i copyed my database – Ujesh Apr 16 '14 at 06:49
  • Your code will not work because of security issues - I am pretty sure Android will forbid you to copy your database to /data/data/*. Your usage of SQLiteOpenHelper is incorrect. Please refer to the [similar question] (http://stackoverflow.com/questions/23092495/android-sqlite-exception-no-suche-table-in-existing-database-with-three-tables/23106397#23106397) – Drew Apr 26 '14 at 12:47
  • i try same code on mac-book.. and give same error... and other all database are working.. so i think it's not security isues – Ujesh Apr 26 '14 at 19:25

1 Answers1

0

Try to get data from some other table. If it works proper delete table and recreate. If it doesn't, check security of your database and authentications of your android project.

Pawan
  • 167
  • 12