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

  • SEARCH
  • Home
  • 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 9115555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:27:38+00:00 2026-06-17T04:27:38+00:00

Im getting a ‘NullPointerException’ when I try and load my intent to a class

  • 0

Im getting a ‘NullPointerException’ when I try and load my intent to a class that handles my listview of the data from my database. I’m a total newbie when it comes to listviews so hopefully someone can tell me where Ive gone wrong!

I was thinking that the null pointer was due to the fact no data was being found at the first row posistion that the cursor is at, but I think looking at the logcat, its an issue with my XML layout ‘entries’?

Heres my class for the ListView:

package com.example.sqliteexample;



import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListView;

public class SQLView extends Activity {

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

    HotOrNot H = new HotOrNot(this, null, null);

    setContentView(R.layout.layout);
     ListView listContent = (ListView)findViewById(R.id.contentList);

     HotOrNot Content = new HotOrNot(this, null, null);

    Cursor cursor = Content.getData();

    startManagingCursor(cursor);

    @SuppressWarnings("static-access")
    String [] from = new String [] {H.KEY_NAME, H.KEY_HOTNESS};
    int [] to = new int [] {R.id.txtName, R.id.txtAge};

    @SuppressWarnings("deprecation")
    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.entries, cursor, from, to);

    listContent.setAdapter(cursorAdapter);

The ‘getData’ method from my DB handler class:

public Cursor getData() {

        String [] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);


        return c;

HotOrNot DB handler class:

package com.example.sqliteexample;

import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.ListView;

public class HotOrNot {

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String KEY_HOTNESS = "persons_hotness";

private static String DATABASE_NAME = "HotOrNotdb";
private static String DATABASE_TABLE = "peopleTable";
private static int DATABASE_VERSION = 30;

private static DBHelper ourHelper;
private static Context ourContext;
private static SQLiteDatabase ourDatabase;

ListView listview;


public void DB_NAME(String DBName)
{
    DATABASE_NAME = DBName;

}

public String returnDB_NAME()
{
    return DATABASE_NAME;
}


public void DB_tableNAME(String DBtName)
{
    DATABASE_TABLE = DBtName;

}

public String returnDB_tNAME()
{
    return DATABASE_TABLE;
}


public void DB_NAME(int DBVersion)
{
    DATABASE_VERSION = DBVersion;

}

public int returnDB_VERSION()
{
    return DATABASE_VERSION;
}



// class constructor for context. when the object is constructed in the main programme
// the context of that class i.e 'this' is sent to this constructor to set the object context.
// null values set on SQLiteDatabase and DBhelper as there is nothing to pass from the called
// objects.
public HotOrNot (Context c, SQLiteDatabase newSQL, DBHelper BD)
{
    ourContext = c;
    ourDatabase = newSQL;
    ourHelper = BD;

}







private static class DBHelper extends SQLiteOpenHelper
{
    public DBHelper(Context context) {
        super(context, KEY_NAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {


        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_NAME + " TEXT NOT NULL, " +
                KEY_HOTNESS + " TEXT NOT NULL);"

                );


    }

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

        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }
}



    public HotOrNot open() throws SQLException
    {

        ourHelper = new DBHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;

    }

    public void close()
    {
        ourHelper.close();

    }

    public long createEntry(String name, String hotness) {


        ContentValues cv = new ContentValues();
        cv.put(KEY_NAME, name);
        cv.put(KEY_HOTNESS, hotness);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);

    }

    public Cursor getData() {

        String [] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        //String result = "";

        //int iRow = c.getColumnIndex(KEY_ROWID);
        //int iName = c.getColumnIndex(KEY_NAME);
        //int ihotness = c.getColumnIndex(KEY_HOTNESS);

        //for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
        //{
            //result = result + c.getString(iRow) + "" + c.getString(iName) + "" + c.getString(ihotness);

        //}

        //c.close();

        return c;
    }

    public String getName(long l) {

        String [] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
        if(c != null)
        {
            // move to the selected row
        c.moveToFirst();
        String name = c.getString(1);
        return name;
        }
        return null;

    }


    public String getHotness(long l) {

        String [] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
        if(c != null)
        {
            // move to the selected row
        c.moveToFirst();
        String hotness = c.getString(2);
        return hotness;

    }
        return null;


    }

    public void updateEntry(long newl, String nameEdit, String hotnessEdit) {
    ContentValues cvUpdate = new ContentValues();

    cvUpdate.put(KEY_NAME, nameEdit);
    cvUpdate.put(KEY_HOTNESS, hotnessEdit);

    ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_ROWID + "=" + newl, null);


    }

    public void delID(long l) {
        ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + l, null);

    }

}

My XML layout for the listview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/txtName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name:" />

<TextView
    android:id="@+id/txtAge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Age:" />

</LinearLayout>

The listview XML layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/contentList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

Logcat:

01-11 00:23:26.655: D/AndroidRuntime(272): Shutting down VM
01-11 00:23:26.655: W/dalvikvm(272): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-11 00:23:26.675: E/AndroidRuntime(272): FATAL EXCEPTION: main
01-11 00:23:26.675: E/AndroidRuntime(272): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqliteexample/com.example.sqliteexample.SQLView}: java.lang.NullPointerException
01-11 00:23:26.675: E/AndroidRuntime(272):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-11 00:23:26.675: E/AndroidRuntime(272):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-11 00:23:26.675: E/AndroidRuntime(272):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-11 00:23:26.675: E/AndroidRuntime(272):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-11 00:23:26.675: E/AndroidRuntime(272):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 00:23:26.675: E/AndroidRuntime(272):  at android.os.Looper.loop(Looper.java:123)
01-11 00:23:26.675: E/AndroidRuntime(272):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-11 00:23:26.675: E/AndroidRuntime(272):  at java.lang.reflect.Method.invokeNative(Native Method)
01-11 00:23:26.675: E/AndroidRuntime(272):  at java.lang.reflect.Method.invoke(Method.java:521)
01-11 00:23:26.675: E/AndroidRuntime(272):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-11 00:23:26.675: E/AndroidRuntime(272):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-11 00:23:26.675: E/AndroidRuntime(272):  at dalvik.system.NativeStart.main(Native Method)
01-11 00:23:26.675: E/AndroidRuntime(272): Caused by: java.lang.NullPointerException
01-11 00:23:26.675: E/AndroidRuntime(272):  at com.example.sqliteexample.HotOrNot.getData(HotOrNot.java:143)
01-11 00:23:26.675: E/AndroidRuntime(272):  at com.example.sqliteexample.SQLView.onCreate(SQLView.java:24)
01-11 00:23:26.675: E/AndroidRuntime(272):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-11 00:23:26.675: E/AndroidRuntime(272):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
  • 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-17T04:27:39+00:00Added an answer on June 17, 2026 at 4:27 am

    You simply forgot to call open().

    HotOrNot Content = new HotOrNot(this, null, null);
    Content.open();
    Cursor cursor = Content.getData();
    

    (But please read about Java naming conventions, which state variables should start with a lowercase letter.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Getting data from a database table to an object in code has always seemed
Getting a strange error when trying to insert data into an Access database using
Getting a MySQL error message when I try to insert a text message from
Getting this error when I follow the simple code example from jsoncpp, that basically
Getting a compile error in C# saying that my child class does not implement
Getting data onto inputStream object from web url inputStream = AWSFileUtil.getInputStream( AWSConnectionUtil.getS3Object(null), cdn.generalsentiment.com, filePath);
Getting 401 errors when trying to use ASP.NET back end in load balanced environment
Getting red warning message that expected identifier for this statement CALayer = ImageView.layer; Using
Getting a background process ID is easy to do from the prompt by going:
Getting some very annoying behaviour from my MVC3 app. So I've got a model

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.