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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:28:46+00:00 2026-06-08T09:28:46+00:00

I have just spent half an hour figuring this thing out, I have managed

  • 0

I have just spent half an hour figuring this thing out, I have managed to fix my code, but I don’t fully understand what is going on and wondered if someone could shed some light on it.

I have a utils type class, that contains a few static fields (a database connection endpoint for example) that are used by various other programs depending on the task at hand. Essentially a library.

This is how it looked previously (while still broken);

//DBUtils.java
public final class DBUtils {

    private static DBConnection myDBConnection = spawnDBConnection();
    private static DBIndex myDBIndex = null;

    private static DBConnection spawnDBConnection() {
        //connect to the database
        //assign a value to myDBIndex (by calling a method on the DBConnection object) <- IMPORTANT
        //myDbIndex NOT NULL HERE
        System.out.println("database connection completed");
        //return the DBConnection object
    }

    public static searchDB(String name) {
        //use the myDBIndex to find a row and return it
    }
}

So briefly, I am using the static spawnDBConnection() method to assign a value to both myDBConnection and myDBIndex. This works perfectly, the first output line from my program is always “database connection completed”, neither myDBConnection or myDBIndex are null at the end of the spawnDBConnection() method, everything is as it should be.

My external program looks like this;

//DoSomethingUsefulWithTheDatabase.java
public final class DoSomethingUsefulWithTheDatabase {

    public static void main(String args[]) {
        DBUtils.searchDB("John Smith"); //fails with NullPointerException on myDBIndex!
    }
}

This call to searchDB happens after the spawnDBConnection has finished, I have used the standard output extensively to show this. However, once inside the searchDB method, the value of myDBIndex is null! It’s a static field, and it was not null by the end of spawnDBConnection, no other assignments have been made, and now it’s null 🙁

The simple fix was to remove the “= null” so the field declaration now looks like;

private static DBIndex myDBIndex;

Why did that make a difference? I’m thoroughly confused by this one.

  • 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-08T09:28:47+00:00Added an answer on June 8, 2026 at 9:28 am

    That’s because the assignment of null to myDBIndex is done after

    private static DBConnection myDBConnection = spawnDBConnection();
    

    e.g. overrides the assignment in spawnDBConnection

    The sequence is:

    1. declare the fields myDBConnection, myDBIndex
    2. Initialize myDBConnection = spawnDBConnection();

      Which includes calling spawnDBConnection and assignment of the return value to myDBConnection

    3. Initialize myDBIndex (with null)

    In your second example, the 3rd step does not exist.

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

Sidebar

Related Questions

The answer to this is likely to be trivial, but I have spent half
I just spent a half hour debugging some new code that's not broken because
I just spent a few hours pulling my hair out over this. I'm trying
I just spent the last 3 hours trying to figure out this error. I
I think my question must be really stupid, but I just spent one hour
I have just spent half a day quietly going mad. I'm making changes to
I have just created this table: create table dbo.OrderTypes ( ID smallint primary key
We have just migrated from solr3.5 to solr3.6, for all this time we have
I have just started to learn how to code iOS apps. I have made
I have some code that runs fairly well, but I would like to make

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.