0

Hey I'm trying to insert data that I get from CallLog.Calls. But it returng a NUllPointerException at this line, considering what the LogCat says:

this.dh.insert(1, contactName, numType, contactNumber, duration, callDate, currTime);

Here is the error:

12-08 11:18:16.377: ERROR/AndroidRuntime(16418): Caused by: java.lang.NullPointerException
12-08 11:18:16.377: ERROR/AndroidRuntime(16418):     at com.psyhclo.RatedCalls.onCreate(RatedCalls.java:58)

Here is the method insert from the class CallDataHelper.java

public boolean insert(Integer cId, String cName, String numType,
String cNum, String dur, String date, String currTime) {
this.db.execSQL("insert into "
+ TABLE_NAME
+ "(id, contact_id, contact_name, number_type, contact_number, duration, date, current_time, cont) "
+ "values( ? ," + cId + ", " + cName + ", " + numType + ", "
+ cNum + ", " + dur + ", " + date + ", " + currTime + ", ? )");
return true;  
}

And here is the code of the class RatedCalls.java that I try to insert the data

package com.psyhclo;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.psyhclo.R;

import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class RatedCalls extends ListActivity {

  private static TextView txtView;
  private CallDataHelper dh;
  StringBuilder sb = new StringBuilder();

  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   Cursor cursor = getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
android.provider.CallLog.Calls.DATE + " DESC ");

startManagingCursor(cursor);
int numberColumnId = cursor
.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
int durationId = cursor
.getColumnIndex(android.provider.CallLog.Calls.DURATION);
int contactNameId = cursor
.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
int dateId = cursor.getColumnIndex(android.provider.CallLog.Calls.DATE);
int numTypeId = cursor.getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);
Date dt = new Date();
    int hours = dt.getHours();
    int minutes = dt.getMinutes();
    int seconds = dt.getSeconds();
    String currTime = hours + ":"+ minutes + ":"+ seconds;

ArrayList<String> callList = new ArrayList<String>();
if (cursor.moveToFirst()) {
 do {

String contactNumber = cursor.getString(numberColumnId);
String contactName = cursor.getString(contactNameId);
String duration = cursor.getString(durationId);
String callDate = DateFormat.getDateInstance().format(dateId);  
String numType = cursor.getString(numTypeId);

this.dh.insert(1, contactName, numType, contactNumber, duration, callDate, currTime);
Toast.makeText(getBaseContext(), "Inserted!", Toast.LENGTH_LONG);
callList.add("Contact Number: "
  + contactNumber + "\nContact Name: " + contactName
  + "\nDuration: " + duration + "\nDate: " + callDate);

} while (cursor.moveToNext());
}
   setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem, callList));
   ListView lv = getListView();
   lv.setTextFilterEnabled(true);

   lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position,
   long id) {

   Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
          Toast.LENGTH_SHORT).show();

   }
   });  
   }
   }
9
  • 1
    Please reformat your question so that we can properly read your code. Also, which line of code is identified by line 58? Commented Dec 8, 2010 at 13:30
  • Its reformated. And the line is this.dh.insert(1, contactName, numType, contactNumber, duration, callDate, currTime); Commented Dec 8, 2010 at 13:31
  • 1
    You dont initialized your dh anywhere hence the NullPointerException Commented Dec 8, 2010 at 13:33
  • dh is a object of the class CallDataHelper. dh makes the reference to the method insert on CallDataHelper class. I've initialized dh as null, but it keep showing the same error. Commented Dec 8, 2010 at 13:35
  • If you're initializing it as null then ccheneson is correct. You need to initialize it to an instance of the CallDataHelper class, e.g. dh = new CallDataHelper(); Commented Dec 8, 2010 at 13:39

1 Answer 1

2

Initialize your CallDataHelper inside your onCreate() to get the context of your Activity.

Here is an example.

private CallDataHelper dh = null;

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    // All your other stuff

    dh = new CallDataHelper(this);

    // and so on ...

    dh.insert( ....)

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

1 Comment

@ccheneson: Right. Thank you. Typed in a hurry. :)

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.