mongoClient.getDatabase is returning a null when I call it in another file

I was trying to insert some data into the database using java, for this, I’ve created a separate class file for the database connection. When inserting the data, I run this query:

DBConnection.mdb.getCollection("Product List").insertOne(document);

(DBconnection is my class file)

It gives me an error that states:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "com.mongodb.client.MongoDatabase.getCollection(String)" because "com.sanrios.productmaster.DBConnection.mdb" is null

I checked permissions for the database but its fine, I don’t know why its returning a null, can anyone please help?

Hello there!

This could happen due to various reasons, such as a failed database connection, an issue with initializing the mdb variable, or a method invocation issue. Can you please share the class file?

The following link, java - What is a NullPointerException, and how do I fix it? - Stack Overflow, is a valuable resource that can assist you in comprehending and troubleshooting this specific type of issue.

Thank you for responding :))

When I run the class file individually, there is no problem with the connection (The message connected successfully shows up). However, the problem occurs when I call a variable from it into another file. I tried following the link earlier as well but could not find a solution.

To resolve my error, I tried multiple methods such as:

  1. trying to connect to the database in the same file and then inserting data,
  2. or connecting to the database when I first run the entire program and then trying to insert

and so on… but each time the issue ends up being either this exception or an exception that states “AWT-event queue 0, illegal state exception, state should be open”.

the variable mdb that I have keeps returning a null value when I call it into my file of data insertion.

Here is the class file:

package com.sanrios.productmaster;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class DBConnection {
    public static MongoClient mongoClient;
    public static MongoDatabase mdb;
    public static MongoCollection mdc;
    public static String databaseName;
    public static String collectionName;
    public static void main(String[]args) throws UnknownHostException {
        String uri = "mongodb+srv://";
        try (MongoClient mongoClient = MongoClients.create(uri)) {
            databaseName = "ProductMaster";
            collectionName = "Product List";
            mdb = mongoClient.getDatabase(databaseName);
            mdc = mdb.getCollection(collectionName);
            System.out.println("\n Connected Successfully \n");

Thank you for your time, please review the code and help me if possible.

No worries - happy to help!

Do you call MongoClient.close anywhere in your application? You must ensure that a MongoClient is not used after it’s been closed, that’s what this error illegal state exception, state should be open generally means.

No, I haven’t closed the connection anywhere in the code

Ok… Can you please share your entire code so that I can run it locally on my machine?