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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:17:55+00:00 2026-05-30T10:17:55+00:00

I writing an application where i use an array within a class. This array

  • 0

I writing an application where i use an array within a class. This array is declared globally outside of any method scope so that all of the objects methods will be able to access the data. However, when i assign an object to a index that object is only available as a part of the array in that particular method. Any other method trying to read that index of the array gets a nullpointerexception.

Some code illustrating my usage:

Account[] accounts = new Account[8000];


public void addAccount() {


    cli.printMessage("Welcome to the account add wizard.");
    String holdername = cli.getInput(entername);
    int accountnr = Integer.parseInt(cli.getInput(enternr));
    BigDecimal accbalance = new BigDecimal(cli.getInput(enterinitialbalance));

    accounts[accountnr] = new Account(holdername, accountnr, accbalance);
    String accountinfo=accounts[accountnr].number + " " + " " + accounts[accountnr].holder + " " + accounts[accountnr].balance;
    cli.printMessage(accountinfo);

}

Adding the account works all the way, this do not when ran from the same instance of this class:

public void getAccountinfo() {

    int accountnr = Integer.parseInt(cli.getInput(getinfo));
    String accountinfo=accounts[accountnr].number + "" + "" + accounts[accountnr].holder + accounts[accountnr].balance;
    cli.printMessage(accountinfo);
}

Is this due to scope or is it due to me confusing instance and class variables? Might it be something completely different?

The error i get is a nullpointer exception at

        String accountinfo=accounts[accountnr].number + "" + "" + accounts[accountnr].holder + accounts[accountnr].balance;

Grateful for any help!

  • 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-05-30T10:17:56+00:00Added an answer on May 30, 2026 at 10:17 am

    In getAccountInfo you let the user input a number X.
    If the array ‘cell’ at X was not previously set via addAccount, accounts[X] is null and thus accounts[X].number correctly bombs out.

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

Sidebar

Related Questions

I'm writing an application for personal use that will track how often I reach
So I am writing a web application for use within my organization. The application
I am writing a Facebook application that would use a Postgres DB along with
I am writing a Cocoa application that makes use of the ParseKit framework (
I'm writing an application that I will use to keep up with my monthly
I'm writing an application with some client-side JS that I use to update the
I am writing an Android application that interfaces with a RESTful service. This web
I'm writing an application that makes use of Java's speech API and FreeTTS. The
I am writing an application that makes use of Uniform Type Identifiers . Specifically,
I am writing an application needs to use large audio multi-samples, usually around 50

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.