0

I want to make a windows forms application with two forms: form1 and form2, form2 displays a 3D chart with a local SQL server connection to make a database as it's DataSource.

I gave two combobox in form1 which will display the columns in the data so that the user can select the columns they want and then click a button on form1 which will open the form2 with the 3D chart made from taking the columns as it's x and y axis.

For this I made a DataTable and added the data, then provided the DataTable columns names to both the combobox then I made an instance of form2 inside form1 and passed the combobox items as strings to the string variables created in form2.

But it's showing the exception 'Object reference not set to an instance of an object.' How do I fix this error?


//Form1
    public void GetColumns()
        {
            DataTable dt=new DataTable();
            data d = new Dataaccess.data();
            dt = (DataTable)d.GetData();


            foreach (DataColumn dc in dt.Columns)
                {
                    Xaxis.Items.Add(dc.ColumnName);
                    Yaxis.Items.Add(dc.ColumnName);
                }                          
        }
        Form2 frm = new Form2();
        public void Xaxis_SelectedIndexChanged(object sender, EventArgs e)
        {           
            frm.xaxis = Xaxis.SelectedItem.ToString();
        }

        public void Yaxis_SelectedIndexChanged(object sender, EventArgs e)
        {            
            frm.yaxis = Yaxis.SelectedItem.ToString();
        }

        public void Loadchart_Click(object sender, EventArgs e)
        {
            this.Hide();
            frm.Show();
        }
//Form2
public partial class Form2 : Form
    {
        public string xaxis;
        public string yaxis;
        public Application()
        {
            InitializeComponent();  
            FillData();            
        }



public void FillData()
        {     
                object dt;
                data d = new Dataaccess.data();
                dt = d.GetData();
                chart1.DataSource = dt;

           
                chart1.Series[0].LegendText = xaxis;
                chart1.Series[0].IsVisibleInLegend = true;
                chart1.Series[0].XValueMember = xaxis;
                chart1.Series[0].YValueMembers = yaxis;
        }

  • Which line does throw the exception? Which object is not set? Use the [debugger](https://msdn.microsoft.com/en-us/library/y740d9d3.aspx) to find out!! - Also: Do the forms have valis references to each other? A common mistake is to create a new instance instead of referencing the original one? You can pass a reference to the 2nd form, maybe like so : `Form2 frm = new Form2(this);` and store it in its constructor to a Form1 variable there.. – TaW Dec 23 '22 at 16:24
  • The exception shows in the main method at line- static void Main() { System.Windows.Forms.Application.Run(new Form1()); } , also I gave the instance of form2 in form1 – user18568746 Dec 25 '22 at 15:16
  • _The exception shows in the main method at line- static void Main() { System.Windows.Forms.Application.Run(new Form1()); }_ Doubtful. Please show the line where the debugger stops! - Also: Your code shows `Form2 frm = new Form2();` inside a method `GetColumns()`. This means that you can't reference it outside of this method. – TaW Dec 25 '22 at 16:16
  • That's exactly the line it stops, doesn't makes sense to me too, and no I instantiated it outside the method. – user18568746 Dec 27 '22 at 04:20
  • Oh, sorry , I didn't notice due to the bad indenting. What is this: `public Application() { InitializeComponent(); FillData(); }` ?? – TaW Dec 27 '22 at 10:00

0 Answers0