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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:34:47+00:00 2026-05-16T03:34:47+00:00

Wondering if I am going about this the right way. I am creating a

  • 0

Wondering if I am going about this the right way. I am creating a C# application that loads a few variables from my App.Config.xml file. I am loading these into a “config” class (Config.cs) along with other variables that I am loading from a MySQL database. This is what my class looks like so far:

 class Config
    {
        public static string ServerHostname = ConfigurationManager.AppSettings["ServerHostname"];
        public static string SoftwareVersion = "v0.1a";
        public static int StationID = DBConnector.GetStationID();
        public static string StationDescription = DBConnector.GetStationDescription();
        public static string StationName = ConfigurationManager.AppSettings["StationName"];
    }

I am using Config.StationName to pull the Config.StationID and Config.StationDescription from a MySQL database like this in DBConnector.cs:

public static int GetStationID()
    {
        try
        {
            MySqlConnection conn = new MySqlConnection(connStr);
            conn.Open();
            string sql = "select station_id from station_master where station_name = @station_name limit 1";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@station_name", Config.StationName);
            object result = cmd.ExecuteScalar();
            conn.Close();
            return Convert.ToInt32(result);
        }
        catch (Exception ex)
        {
            ErrorConnection += ex.ToString();
            return 0;
        }
    }
    public static string GetStationDescription()
    {
        try
        {
            MySqlConnection conn = new MySqlConnection(connStr);
            conn.Open();
            string sql = "select station_description from station_master where station_id = '" + Config.StationID +"' limit 1";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            // cmd.Parameters.AddWithValue("@station_name", Config.StationName.ToString());
            object result = cmd.ExecuteScalar();
            conn.Close();
            MessageBox.Show(sql);
            return (result.ToString());
        }
        catch (Exception ex)
        {
            ErrorGenericDBException += ex.ToString();
            MessageBox.Show(ErrorGenericDBException);
            return "Error";
        }
    }

The DBConnector.GetStationID class works fine. It returns the int value of my station. But when I try to display the Config.StationDescription, it throws an exception of System.NullReferenceException: Object reference not set to an instance of an object at Namespace.DBConnector.GetStationDescription().

I thought that since I was using a static class for Config.StationName, i don’t have to create an instance to refer to it. I need help to understand why its throwing an exception.

  • 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-16T03:34:47+00:00Added an answer on May 16, 2026 at 3:34 am

    To me it looks like you might be mixing up what part of the code has the problem. Since you’re saying that it’s GetSTationDescription that throws, I’m not sure why you then assume StationName has a problem?

    I’d take a look at the line object result = cmd.ExecuteScalar();, if you put a breakpoint on the line after that, does result have a value?

    Another issue is that you’re currently only closing the connection if an exception is not thrown, it would be safer to have the conn.Close(); inside a finally statement (or use a using statement so you don’t have to worry about closing it).

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

Sidebar

Related Questions

Wondering if I'm going about this the right way or not. I have 3
Right, this is a very odd issue that I'm wondering how i am going
I'm wondering about computational efficiency. I'm going to use Java in this example, but
I'm going to implement an web app in gae. I was wondering which is
Wondering what the best / good way of doing this would be in jQuery.
I'm currently creating a fullscreen Java application that will be like a kiosk application.
I want to be accurate semantically and legible for screen-readers. I'm wondering about this
This is going to sound like a dumb question, but I was wondering where
this is probably going over the top, but I was after a function that
This is a shoutout as I've been spending the last few days learning about

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.