Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9053249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:22:46+00:00 2026-06-16T13:22:46+00:00

Im having troubles with displaying one Event information from my database. After clicking on

  • 0

Im having troubles with displaying one Event information from my database. After clicking on my Event from event list in DayPlan.java it should show me some informations about this event but it simply crashes whole application with error : Nullpointer exception 🙁
I used thiscodeasa base: Database example

error is somewhere in OnPostExecute from ViewEvent, could anyone please help me?

my database connector:

    package com.examples.android.calendar;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;


public class DatabaseConnector {

    private static final String DB_NAME = "watchaday";
    private SQLiteDatabase database;
    private DatabaseOpenHelper dbOpenHelper;

    public DatabaseConnector(Context context) {
        dbOpenHelper = new DatabaseOpenHelper(context, DB_NAME, null, 1);
    }

       public void open() throws SQLException 
       {
          //open database in reading/writing mode
          database = dbOpenHelper.getWritableDatabase();
       } 

       public void close() 
       {
          if (database != null)
             database.close();
       }       

       public void insertContact(String date, String hour_from, String hour_to, String event, String colour) 
               {
                  ContentValues newCon = new ContentValues();
                  newCon.put("date", date);
                  newCon.put("hour_from", hour_from);
                  newCon.put("hour_to", hour_to);
                  newCon.put("event", event);
                  newCon.put("colour", colour);


                  open();
                  database.insert("calendar_events", null, newCon);
                  close();
               }


               public void updateContact(long id, String date, String hour_from, String hour_to, String event, String colour) 
               {
                  ContentValues editCon = new ContentValues();
;
                  editCon.put("date", date);
                  editCon.put("hour_from", hour_from);
                  editCon.put("hour_to", hour_to);
                  editCon.put("event", event);
                  editCon.put("colour", colour);

                  open();
                  database.update("calendar_events", editCon, "_id=" + id, null);
                  close();
               }


               public Cursor getAllContacts() 
               {
                  return database.query("calendar_events", new String[] {"_id", "event"}, 
                     null, null, null, null, "date");
               }

               public Cursor getOneContact(long id) 
               {
                  return database.query("calendar_events", null, "_id=" + id, null, null, null, null);
               }

               public void deleteContact(long id) 
               {
                  open(); 
                  database.delete("calendar_events", "_id=" + id, null);
                  close();
               }
}

my database Helper:

 package com.examples.android.calendar;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseOpenHelper extends SQLiteOpenHelper {

    public DatabaseOpenHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createQuery = "CREATE TABLE calendar_events (_id integer primary key autoincrement,date, hour_from, hour_to, event, colour);";                 
        db.execSQL(createQuery);        
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}

my Event List code :

 package com.examples.android.calendar;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class DayPlan extends ListActivity {

     public static final String ROW_ID = "row_id";
     private ListView conListView;
     private CursorAdapter conAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        conListView=getListView();
        conListView.setOnItemClickListener(viewConListener);

        // map each name to a TextView
        String[] from = new String[] { "event" };
        int[] to = new int[] { R.id.countryTextView };
        conAdapter = new SimpleCursorAdapter(DayPlan.this, R.layout.day_plan, null, from, to);
        setListAdapter(conAdapter); // set adapter
    }


    @Override
    protected void onResume() 
    {
       super.onResume();  
       new GetContacts().execute((Object[]) null);
     } 


    @Override
    protected void onStop() 
    {
       Cursor cursor = conAdapter.getCursor();

       if (cursor != null) 
          cursor.deactivate();

       conAdapter.changeCursor(null);
       super.onStop();
    }    


    private class GetContacts extends AsyncTask<Object, Object, Cursor> 
    {
       DatabaseConnector dbConnector = new DatabaseConnector(DayPlan.this);

       @Override
       protected Cursor doInBackground(Object... params)
       {
          dbConnector.open();
          return dbConnector.getAllContacts(); 
       } 

       @Override
       protected void onPostExecute(Cursor result)
       {
          conAdapter.changeCursor(result); // set the adapter's Cursor
          dbConnector.close();
       } 
    } 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
       super.onCreateOptionsMenu(menu);
       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.menu.dayplan_menu, menu);
       return true;
    }   

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
       Intent addContact = new Intent(DayPlan.this, AddEvent.class);
       startActivity(addContact);
       return super.onOptionsItemSelected(item);
    }

    OnItemClickListener viewConListener = new OnItemClickListener() 
    {
       public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) 
       {         
          Intent viewCon = new Intent(DayPlan.this, ViewEvent.class);
          viewCon.putExtra(ROW_ID, arg3);
          startActivity(viewCon);
       }
    };    

}

my View event code which crashes application in OnPostExecute part:

ViewEvent.java :

public class ViewEvent extends Activity {

   private long rowID;
   private TextView dateTv;
   private TextView hourfromTv;
   private TextView hourtoTv; 
   private TextView eventTv;
   private TextView colourTv; 

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

      setUpViews();
      Bundle extras = getIntent().getExtras();
      rowID = extras.getLong(DayPlan.ROW_ID); 
   }

   private void setUpViews() {
       dateTv = (TextView) findViewById(R.id.date);
       hourfromTv = (TextView) findViewById(R.id.hour_fromText);
       hourtoTv = (TextView) findViewById(R.id.hour_toText);
       eventTv = (TextView) findViewById(R.id.eventText);
       colourTv = (TextView) findViewById(R.id.colourText);

   }

   @Override
   protected void onResume()
   {
      super.onResume();
      new LoadContacts().execute(rowID);
   } 

   private class LoadContacts extends AsyncTask<Long, Object, Cursor> 
   {
      DatabaseConnector dbConnector = new DatabaseConnector(ViewEvent.this);

      @Override
      protected Cursor doInBackground(Long... params)
      {
         dbConnector.open();
         return dbConnector.getOneContact(params[0]);
      } 

      @Override
      protected void onPostExecute(Cursor result)
      {

         super.onPostExecute(result);

         result.moveToFirst();
         // get the column index for each data item
       int dateindex = result.getColumnIndex("date");
         int hourfromIndex = result.getColumnIndex("hour_from");
         int hourtoIndex = result.getColumnIndex("hour_to");
         int eventIndex = result.getColumnIndex("event");
         int colourIndex = result.getColumnIndex("colour");

         dateTv.setText(result.getString(dateindex));
         hourfromTv.setText(result.getString(hourfromIndex));
         hourtoTv.setText(result.getString(hourtoIndex));
         eventTv.setText(result.getString(eventIndex));
         colourTv.setText(result.getString(colourIndex));

         result.close();
         dbConnector.close();
      }
   } 


   @Override
   public boolean onCreateOptionsMenu(Menu menu) 
   {
      super.onCreateOptionsMenu(menu);
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.view_event_menu, menu);
      return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) 
   {
      switch (item.getItemId())
      {
         case R.id.editItem:
            Intent addEditContact =
               new Intent(this, AddEvent.class);

            addEditContact.putExtra(DayPlan.ROW_ID, rowID);

            startActivity(addEditContact); 
            return true;

         case R.id.deleteItem:
            deleteContact();
            return true;

         default:
            return super.onOptionsItemSelected(item);
      } 
   }

   private void deleteContact()
   {

      AlertDialog.Builder alert = new AlertDialog.Builder(ViewEvent.this);

      alert.setTitle(R.string.confirmTitle); 
      alert.setMessage(R.string.confirmMessage); 

      alert.setPositiveButton(R.string.delete_btn,
         new DialogInterface.OnClickListener()
         {
            public void onClick(DialogInterface dialog, int button)
            {
               final DatabaseConnector dbConnector = 
                  new DatabaseConnector(ViewEvent.this);

               AsyncTask<Long, Object, Object> deleteTask =
                  new AsyncTask<Long, Object, Object>()
                  {
                     @Override
                     protected Object doInBackground(Long... params)
                     {
                        dbConnector.deleteContact(params[0]); 
                        return null;
                     } 

                     @Override
                     protected void onPostExecute(Object result)
                     {
                        finish(); 
                     }
                  };

               deleteTask.execute(new Long[] { rowID });               
            }
         }
      );

      alert.setNegativeButton(R.string.cancel_btn, null).show();
   }
}

Error :

12-30 17:52:29.334: E/AndroidRuntime(1173): FATAL EXCEPTION: main

12-30 17:52:29.334: E/AndroidRuntime(1173): java.lang.NullPointerException

12-30 17:52:29.334: E/AndroidRuntime(1173): at com.examples.android.calendar.ViewEvent$LoadContacts.onPostExecute(ViewEvent.java:76)

12-30 17:52:29.334: E/AndroidRuntime(1173): at com.examples.android.calendar.ViewEvent$LoadContacts.onPostExecute(ViewEvent.java:1)

12-30 17:52:29.334: E/AndroidRuntime(1173): at android.os.AsyncTask.finish(AsyncTask.java:631)

12-30 17:52:29.334: E/AndroidRuntime(1173): at android.os.AsyncTask.access$600(AsyncTask.java:177)

12-30 17:52:29.334: E/AndroidRuntime(1173): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)

12-30 17:52:29.334: E/AndroidRuntime(1173): at android.os.Handler.dispatchMessage(Handler.java:99)

12-30 17:52:29.334: E/AndroidRuntime(1173): at android.os.Looper.loop(Looper.java:137)

12-30 17:52:29.334: E/AndroidRuntime(1173): at android.app.ActivityThread.main(ActivityThread.java:4745)

12-30 17:52:29.334: E/AndroidRuntime(1173): at java.lang.reflect.Method.invokeNative(Native Method)

12-30 17:52:29.334: E/AndroidRuntime(1173): at java.lang.reflect.Method.invoke(Method.java:511)

12-30 17:52:29.334: E/AndroidRuntime(1173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)

12-30 17:52:29.334: E/AndroidRuntime(1173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

12-30 17:52:29.334: E/AndroidRuntime(1173): at dalvik.system.NativeStart.main(Native Method)
12-30 17:52:31.463: I/Process(1173): Sending signal. PID: 1173 SIG: 9
12-30 17:52:32.305: E/Trace(1191): error opening trace file: No such file or directory (2)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:22:47+00:00Added an answer on June 16, 2026 at 1:22 pm

    Here the error was due to wrong resource Id.Still read below

    result.moveToFirst() returns true if there is at least one record returned in your query.
    Proper way of using it:

    if(result.moveToFirst()){
    
           int dateindex = result.getColumnIndex("date");
             int hourfromIndex = result.getColumnIndex("hour_from");
             int hourtoIndex = result.getColumnIndex("hour_to");
             int eventIndex = result.getColumnIndex("event");
             int colourIndex = result.getColumnIndex("colour");
    
             dateTv.setText(result.getString(dateindex));
             hourfromTv.setText(result.getString(hourfromIndex));
             hourtoTv.setText(result.getString(hourtoIndex));
             eventTv.setText(result.getString(eventIndex));
             colourTv.setText(result.getString(colourIndex));
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having troubles displaying my pop-up from inside a different thread. I have
guys am having some troubles with running out of memory when displaying an animation
I am having trouble displaying results recieved from asp.net WebMethod. I have a HTML
I'm having trouble with displaying image from db. I think that method for saving
I am having trouble passing the return value from TheMethod() to Main and displaying
My Java application is displaying some odd behaviour, and I'm having trouble finding a
Having troubles here finding the right containers to represent a list of tasks on
I'm having some troubles with displaying a graph. It's a very strange issue, because
I'm having trouble displaying the results from Core Data in my UISearchDisplayController when I
I'm having trouble displaying an image in a UIWebView. I retrieve html content from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.