Tiedt Tech
Tiedt Tech

Reputation: 717

Variable is null in other context C# Windows Forms

I'm going crazy now ... I can not understand why if I create the variable "server" in the event button2_Click_2, when trying to access it in the event it button3_Click_1 be null.

What should I do to access it in button3_Click_1?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using TCCWindows.Lib;
using System.Web.Http.SelfHost;
using System.Web.Http;


namespace TCCWindows
{
    public partial class FormPrincipal : Form
    {   
        HttpSelfHostServer server;
        HttpSelfHostConfiguration config;

        public FormPrincipal()
        {
            InitializeComponent();
        }

        private void button2_Click_2(object sender, EventArgs e)
        {
            var config = new HttpSelfHostConfiguration(textBox1.Text);

            config.Routes.MapHttpRoute(
                "API Default", "api/{controller}/{id}",
                new { id = RouteParameter.Optional });
            HttpSelfHostServer server = new HttpSelfHostServer(config);
            server.OpenAsync();              
            MessageBox.Show("Server is ready!");  
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            server.CloseAsync();    
        }
    }

    public class ProductsController : ApiController
    {

        public string GetProductsByCategory(string category)
        {
            return (category ?? "Vazio");
        }
    }
}

Upvotes: 1

Views: 279

Answers (3)

Jesse C. Slicer
Jesse C. Slicer

Reputation: 20157

Note the removal of both the local variable declarators var and HttpSelfHostServer in HttpSelfHostServer:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using TCCWindows.Lib;
using System.Web.Http.SelfHost;
using System.Web.Http;


namespace TCCWindows
{
    public partial class FormPrincipal : Form
    {   
        HttpSelfHostServer server;
        HttpSelfHostConfiguration config;

        public FormPrincipal()
        {
            InitializeComponent();
        }

        private void button2_Click_2(object sender, EventArgs e)
        {
            config = new HttpSelfHostConfiguration(textBox1.Text);

            config.Routes.MapHttpRoute(
                "API Default", "api/{controller}/{id}",
                new { id = RouteParameter.Optional });
            server = new HttpSelfHostServer(config);
            server.OpenAsync();              
            MessageBox.Show("Server is ready!");  
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            server.CloseAsync();    
        }
    }

    public class ProductsController : ApiController
    {

        public string GetProductsByCategory(string category)
        {
            return (category ?? "Vazio");
        }
    }
}

Upvotes: 1

Grant Winney
Grant Winney

Reputation: 66449

You're referring to server in both button clicks, but at no point do you actually instantiate the object.

After instantiating config in button2_Click_2, you'll want to also instantiate server:

server = new HttpSelfHostServer(config);

But if the button3_Click_1 event runs before button2_Click_2, you'll still get an exception thrown because server will still be null.

Unless you have some way of enforcing which button is clicked, you might want to move the objects being instantiated into the constructor so you're sure they aren't null when you reference them.

Upvotes: 1

Christoffer Mansfield
Christoffer Mansfield

Reputation: 803

You are declaring a new variable named server in you Button2_Click_2 method. You need to assign it to the field, so change

HttpSelfHostServer server = new HttpSelfHostServer(config);

into

server = new HttpSelfHostServer(config);

Upvotes: 4

Related Questions