为什么我会收到 NullException?

发布于 2025-01-08 17:59:17 字数 1217 浏览 2 评论 0原文

我搜索了一段时间,没有找到解决方案,还是我只是没有看到这个小错误?

我用 Visual C# 编写了一个程序 Form1.cs 程序.cs Server.cs

Server.cs


namespace WindowsApplication1 {
class testServer {
public Form1 form1;
form1.send("data");

Program.cs


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication1 
{
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
}

Form1.cs


namespace WindowsApplication1
{
public partial class Form1 : System.Windows.Forms.Form
{


    public Form1()
    {
        InitializeComponent();

    }
    private testServer Server;

private void startServer_Click(object sender, System.EventArgs e)
    {

        Server = new Server(data);
        Server.form1 = this;

    }

一切正常,但在 Server.cs 中我得到一个 nullException form1.send("data"); 看来 form1 确实为空,但为什么呢?

我哪里忘记了什么?

I searched a while and found no solution or do I just not see the little error?

I wrote a program with Visual C# and have
Form1.cs
Program.cs
Server.cs

Server.cs


namespace WindowsApplication1 {
class testServer {
public Form1 form1;
form1.send("data");

Program.cs


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication1 
{
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
}

Form1.cs


namespace WindowsApplication1
{
public partial class Form1 : System.Windows.Forms.Form
{


    public Form1()
    {
        InitializeComponent();

    }
    private testServer Server;

private void startServer_Click(object sender, System.EventArgs e)
    {

        Server = new Server(data);
        Server.form1 = this;

    }

Everything works but in Server.cs I get a nullexception with form1.send("data");
It seems form1 is really null but why?

Where did I forget something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

会傲 2025-01-15 17:59:17

您必须创建 Form1 的实例,尝试

public Form1 form1 = new Form1();

You have to create an instance of Form1, try

public Form1 form1 = new Form1();
太阳男子 2025-01-15 17:59:17
public Form1 form1;
form1.send("data");

form1 永远不会被实例化。这是您的 NullReferenceException (NRE)。

更好的:

public Form1 form1 = new Form1();
form1.send("data");
public Form1 form1;
form1.send("data");

form1 is never instantiated. There is your NullReferenceException (NRE).

Better:

public Form1 form1 = new Form1();
form1.send("data");
说不完的你爱 2025-01-15 17:59:17

也许您忘记将 testServer 中的 form1 变量分配给构造参数。

顺便说一句:您的代码看起来不像好的设计:您不应该传递 Form 对象。

Maybe you forgot to assign the form1 variable in testServer to the construct parameter.

BTW: You code doesn't look like good design: You shouldn't pass Form objects around.

裸钻 2025-01-15 17:59:17

它是空的,因为你没有初始化它。

public Form1 form1;
..
// Initialize the object BEFORE using it!
form1 = new Form1();
form1.send("data");

It is null because you're not initializing it.

public Form1 form1;
..
// Initialize the object BEFORE using it!
form1 = new Form1();
form1.send("data");
梦过后 2025-01-15 17:59:17

form1 变量在代码中使用之前未初始化

The form1 variable is not initialised before being used in the code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文