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 8358269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:44:35+00:00 2026-06-09T10:44:35+00:00

Working on developing a Class for my android app that will randomly generate NPCs

  • 0

Working on developing a Class for my android app that will randomly generate NPCs and their stats. Since these methods will be called in different activities figured putting them in their own class would be best. Yet having some trouble getting the cursors they contain to work correctly.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

    public class startscreen extends Activity {
        private dbhelper mydbhelper;
        Intent intent;


        /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.startscreen);
                mydbhelper = dbhelper.getInstance(startscreen.this);
                }


            public void onClickNew(View v){
             intent = new Intent(getBaseContext(), newgame.class);
             startActivity(intent);
             }


            public void onClickLoad(View v){
             //intent = new Intent(getBaseContext(), mainmenu.class);
             //startActivity(intent);
                Generator obj = new Generator();
                obj.randomName();
             }

I am calling the method on this bottom button to my main menu for easy testing.

import java.util.Random;

import android.database.Cursor;
import android.util.Log;

public class Generator {
    public dbhelper mydbhelper;


    public String randomName(){
        Cursor name;
        Random nameRandom = new Random();
        int first = nameRandom.nextInt(171)+1;
        name = mydbhelper.getRandomName(first); //This is where I get the error. "NullPointerException"
        String firstName = name.getString(name.getColumnIndex(dbhelper.KEY_FIRST));
        int last = nameRandom.nextInt(245)+1;
        name =  mydbhelper.getRandomName(last);
        String lastName = name.getString(name.getColumnIndex(dbhelper.KEY_LAST));
        String fullName = firstName + " " + lastName;
        Log.e(fullName, "was the generated name");

        return fullName;

    }

}

If I take out the cursors and put in a stat string to return it works fine. So I’m handling this cursors incorrectly. Sadly Singleton and using methods from other classes is on of my short comings so hoping someone here will be able to explain what I’m doing wrong.

  public Cursor getRandomName(int number){
      return myDataBase.query(NAME_TABLE, new String[]{KEY_ID, KEY_FIRST, KEY_LAST},
              KEY_ID + " = " + number , null, null, null, KEY_ID);
    }

This is the cursor I’m working with, located in my dbhelper class.

  • 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-09T10:44:37+00:00Added an answer on June 9, 2026 at 10:44 am

    mydbhelper in your Activity class is a different reference than the mydbhelper in your Generator class. You need to call mydbhelper = dbhelper.getInstance() in your Generator class or pass in the reference before you use it.

    A quick way to do it would be:

    public String randomName(dbhelper mydbhelp) {
      // code 
      name = mydbhelper.getRandomName(first);
      // code
      name.close
    }
    

    Remember to close both your cursor and your database when you’re done with it. Close the Cursor first.

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

Sidebar

Related Questions

I'm currently working on developing a custom keyboard app that will be optimized for
I am developing an Android app using Phonegap and jQueryMobile, working in Eclipse. I
I'm developing an Android application with a database. That database will have more than
I'm developing an Android app that utilizes a third-party jar. I want to monitor
Currently I'm developing an Android app for that I'm using the Facebook SDK. It's
I'm working on developing an application that talks to a family of USB sensors.
I am a newbie working towards developing an IE extension that would appear as
Hi I am developing an Android app using an EditText, and a add en
We are developing a system for Android devices. For that we are using FC16,
Currently I'm working on an app for Android phones. We want to detect features

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.