Note that there are some explanatory texts on larger screens.

plurals
  1. POLogcat error while getting data from Sqlitedatabase to listview
    text
    copied!<p>and my XML file is </p> <pre><code> &lt;ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" &gt; &lt;/ListView&gt; &lt;/RelativeLayout&gt; </code></pre> <p>in these database i am getting values from database and displaying them in a listview. my main activity is package com.example.kern;</p> <pre><code> import android.os.Bundle; import android.provider.Contacts.People; import android.app.Activity; import android.app.ListActivity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.view.Menu; import android.widget.CursorAdapter; import android.widget.ListView; import android.widget.SimpleCursorAdapter; public class Second extends ListActivity { DatabaseAdapter helper; SQLiteDatabase db; private CursorAdapter dataSource; private static final String fields[] = { "Name", "Numb"}; ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); helper=new DatabaseAdapter(this); helper.open(); lv=(ListView)findViewById(android.R.id.list); Cursor data = db.query("Mycon", fields,null, null, null, null, null); dataSource = new SimpleCursorAdapter(this, R.layout.activity_second, data, fields, new int[] { R.id.name, R.id.number }); lv.setAdapter(dataSource); } } </code></pre> <p>and mydatabase adapter class is package com.example.kern;</p> <pre><code> import android.content.ContentValues; import android.content.Context; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import com.example.kern.DatabaseAdapter; import com.example.kern.DatabaseAdapter.DatabaseHelper; public class DatabaseAdapter { DatabaseHelper helper; SQLiteDatabase db; static final int VERSION=2; static final String DATA_NAME="Mycontsc"; private static final String query = "create table Mycon(id Integer primary key,Name text,Numb text)"; //the query has been changed private final Context mCtx; public DatabaseAdapter(Context ctx) { this.mCtx = ctx; } //DatabaseHelper has become a subclass public class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context) { super(context, DATA_NAME, null, VERSION); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase arg0) { // TODO Auto-generated method stub arg0.execSQL(query); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } } public DatabaseAdapter open() throws SQLException { helper = new DatabaseHelper(mCtx); db=helper.getWritableDatabase(); db=helper.getReadableDatabase(); return this; } public void close() { helper.close(); } public long AddDetail(String name,String num) { ContentValues vals=new ContentValues(); vals.put("Name",name); vals.put("Numb", num); long a=db.insert("Mycon", null, vals); return a; } } </code></pre> <p>it is showing null pointer exception in logcat</p> <pre><code> 09-18 10:32:02.631: E/AndroidRuntime(2286): FATAL EXCEPTION: main 09-18 10:32:02.631: E/AndroidRuntime(2286): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kern/com.example.kern.Second}: java.lang.NullPointerException 09-18 10:32:02.631: E/AndroidRuntime(2286): Caused by: java.lang.NullPointerException 09-18 10:32:02.631: E/AndroidRuntime(2286): at com.example.kern.Second.onCreate(Second.java:29) </code></pre> <p>please check the program and give me the solution,it is showing exception,alerady there is values in the database and i am retriving them.. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload