[SOLVED] NullPointerException error in displaying database

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

[SOLVED] NullPointerException error in displaying database

Postby totw » Sun Apr 05, 2009 10:07 am

seems that there is a problem with setlistadapter... i debugged the application .the null pointer error comes at :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. class DataBaseWork extends ListActivity
  2.  
  3. {
  4.  
  5. .
  6.  
  7. .
  8.  
  9. .
  10.  
  11. .
  12.  
  13. .
  14.  
  15. .
  16.  
  17.  
  18.  
  19.  public void FillData()
  20.  
  21.     {
  22.  
  23.         Cursor c= GetAllRows();
  24.  
  25.    
  26.  
  27.        
  28.  
  29.         startManagingCursor(c);
  30.  
  31.     ListAdapter adapter = new SimpleCursorAdapter(
  32.  
  33.                         this,
  34.  
  35.                         R.layout.displaylog,
  36.  
  37.                         c,
  38.  
  39.                         new String[] {"login_time", "serverIP"},
  40.  
  41.                         new int[] {R.id.col1,R.id.col2}
  42.  
  43.                         );
  44.  
  45.         this.setListAdapter(adapter);
  46.  
  47.        
  48.  
  49.     }
  50.  
  51.     .
  52.  
  53.     .
  54.  
  55.     .
  56.  
  57.     .
  58.  
  59.  
  60.  
  61. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


the 'databasework' is not an activity.. the 'test' class shown below is the subactivity not the main one..

i am calling the filldata() method from 'test' class :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class test extends Activity {
  2.  
  3.         DataBaseWork dbw;
  4.  
  5.         @Override
  6.  
  7.         public void onCreate(Bundle icicle) {
  8.  
  9.                 dbw = new DataBaseWork(this);
  10.  
  11.                 super.onCreate(icicle);
  12.  
  13.                 dbw.FillData();
  14.  
  15.                  
  16.  
  17.                  TextView col1 = (TextView)findViewById(R.id.col1);
  18.  
  19.                  TextView col2 = (TextView)findViewById(R.id.col2);
  20.  
  21.                  
  22.  
  23.                   setContentView(R.layout.displaylog);
  24.  
  25.                
  26.  
  27.                  
  28.  
  29.                 }
  30.  
  31.  
  32.  
  33.  
  34.  
  35. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


cannot figure out what the problem is.. but when i comment out filldata(), the exception error vanishes.. so the problem lies there i guess.. hav wasted a lot of time on this.. plz help..

here is one of the relevant xml files i am using..

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:orientation="vertical"
  6.  
  7.    android:layout_width="fill_parent"
  8.  
  9.    android:layout_height="fill_parent"
  10.  
  11.    >
  12.  
  13.     <TextView id="@+id/col1"
  14.  
  15.          android:layout_width="130sp"
  16.  
  17.                 android:layout_height="wrap_content"
  18.  
  19.                 android:textSize="10sp"
  20.  
  21.                 android:text="Login Time"
  22.  
  23.    />
  24.  
  25.         <TextView id="@+id/col2"
  26.  
  27.          android:layout_width="fill_parent"
  28.  
  29.                 android:layout_height="wrap_content"
  30.  
  31.                 android:textSize="10sp"
  32.  
  33.                 android:text="Server Address"
  34.  
  35.    />
  36.  
  37. </LinearLayout>
  38.  
  39.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
Last edited by totw on Tue Apr 07, 2009 7:06 pm, edited 1 time in total.
User avatar
totw
Developer
Developer
 
Posts: 27
Joined: Tue Mar 31, 2009 10:35 am

Top

Postby WarrenFaith » Tue Apr 07, 2009 12:12 pm

First of all: use lower case method names. No C# style with upper case :) this is java :)

Ok, i struggle often while using wrong column names, so are you sure these columns really exists?

Try your SimpleCursorAdapter() like that:
SimpleCursorAdapter(this, R.layout.main, cursor, null, null);

Try to figure out, if the Cursor have something "inside".

Which object is null in your method? There is a lot of things which can be null :) Please post the line/object which creates the NPE.
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Problem Solved

Postby totw » Tue Apr 07, 2009 7:04 pm

My problem was solved as I tried out a different method to display the contents of my database, instead of using the SimpleCursorAdapter....Thanks for your reply though, WarrenFaith :)

I am posting the modified relevant code in order to display the contents of a database on screen.. i hope this helps someone who might me stuck out there..

I have executed the following code in a different class which extends Activity.
Below, I am using an object 'dbw' of the class I have defined to handle my database.
Then, I have used a simple arraylist called 'results' to insert rows of my database in the form of an array of arrays.
I have then assigned a cursor to all the rows in the database.
Then, rows are inserted using a for loop, followed by declaration and setting of the arrayadapter..





Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onCreate(Bundle icicle) {
  2.  
  3.                
  4.  
  5.                 dbw = new DataBaseWork(this);
  6.  
  7.                 super.onCreate(icicle);
  8.  
  9.                
  10.  
  11.                 setContentView(R.layout.displaylog);
  12.  
  13.                
  14.  
  15.                  ListView list = (ListView)findViewById(R.id.list);
  16.  
  17.  
  18.  
  19.                
  20.  
  21.                  ArrayList<String> results = new ArrayList<String>();
  22.  
  23.                
  24.  
  25.        
  26.  
  27.                
  28.  
  29. Cursor c= dbw.GetAllRows();
  30.  
  31.        
  32.  
  33.        
  34.  
  35.         startManagingCursor(c);
  36.  
  37.         c.first();
  38.  
  39.          
  40.  
  41.          String i1,i2;
  42.  
  43.          i1 = new String();
  44.  
  45.          i2 = new String();
  46.  
  47.          
  48.  
  49.          for(int j=0; j<c.count(); j++)
  50.  
  51.          {
  52.  
  53.                  i1=c.getString(1);
  54.  
  55.                  
  56.  
  57.                  i2=c.getString(2);
  58.  
  59.          
  60.  
  61.                  results.add(""+ i1
  62.  
  63.                                  + "   :  " + i2);
  64.  
  65.        
  66.  
  67.                  c.next();
  68.  
  69.          }
  70.  
  71.                  
  72.  
  73.                  
  74.  
  75.                   final ArrayAdapter<String> aa;
  76.  
  77.                   aa = new ArrayAdapter<String>(this,
  78.  
  79.                   android.R.layout.simple_list_item_1,
  80.  
  81.                                                results);
  82.  
  83.                   // Bind the array adapter to the listview.
  84.  
  85.                   list.setAdapter(aa);
  86.  
  87.                
  88.  
  89.                  
  90.  
  91.                 }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4



So, if you are having problems with the simplecursoradapter, try out the method above..
User avatar
totw
Developer
Developer
 
Posts: 27
Joined: Tue Mar 31, 2009 10:35 am

Re: Problem Solved

Postby totw » Tue Apr 07, 2009 7:08 pm

:D
User avatar
totw
Developer
Developer
 
Posts: 27
Joined: Tue Mar 31, 2009 10:35 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 6 guests