qq_43653171
以墨代水
2018-11-27 14:44
采纳率: 33.3%
浏览 550

关于C#“未将对象引用设置到对象的实例”的几个问题。

我是一个超级小白,也是第一次提问,还望各位大佬指教。

首先,我编写了一个WPF窗体和一个TextBox.ID为"aa1"的“TextChanged", 想达到如下目的: 当“aa1”的text值发生改变后,立即实时的将"aa1"的text值赋给TextBox.ID为“aa2”的text。
但是调试时在“aa2.Text = Convert.ToString(x1);”这句话提示“System.NullReferenceException:“未将对象引用设置到对象的实例。”

个人想法是这样的:因为这是“aa1”的TextChanged,所以C#可以知道“aa1”是什么,但是对“aa2”没有说明,导致C#不知道这个“aa2”是什么,问题可能是出在“实例化”与“引用”之间。

想请问各位大佬:
1.对于这种情况我应该怎样去修改?
2.在“未将对象引用设置到对象的实例”这句话中,“对象”指的是什么?

代码片段如下:

         private void Aa1_TextChanged(object sender, TextChangedEventArgs e)
        {
            double x1 = Convert.ToDouble(aa1.Text);
            aa2.Text = Convert.ToString(x1);
        }

谢谢。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • qq_43653171
    以墨代水 2018-12-01 02:52
    已采纳

    自己来结一下帖子,直接上代码。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.ApplicationServices;
    using Autodesk.Revit.UI.Events;
    using Autodesk.Revit.DB.Events;
    using Autodesk.Revit;
    using System.Drawing;
    
    using Rectangle = System.Windows.Shapes.Rectangle;
    
    namespace Bridge_design
    {
    /// <summary>
    /// Cross_section_size.xaml 的交互逻辑
    /// </summary>
    public partial class Cross_section_size : System.Windows.Window
    {
        ExternalCommandData m_revit;
        public Cross_section_size(ExternalCommandData revit)
        {
            InitializeComponent();
            m_revit = revit;
            InitTextbox();
            InitCanvas();
    
        }
    
        private void InitCanvas()
        {
            double a1 = Convert.ToDouble(aa1.Text);
            double a2 = Convert.ToDouble(aa2.Text);
            double a3 = Convert.ToDouble(aa3.Text);
            double a4 = Convert.ToDouble(aa4.Text);
            double a5 = Convert.ToDouble(aa5.Text);
            double a6 = Convert.ToDouble(aa6.Text);
            double a7 = Convert.ToDouble(aa7.Text);
            double a8 = Convert.ToDouble(aa8.Text);
            double a9 = Convert.ToDouble(aa9.Text);
            double b1 = Convert.ToDouble(bb1.Text);
            double b2 = Convert.ToDouble(bb2.Text);
            double b3 = Convert.ToDouble(bb3.Text);
            double b4 = Convert.ToDouble(bb4.Text);
            double b5 = Convert.ToDouble(bb5.Text);
            double b6 = Convert.ToDouble(bb6.Text);
            DrawingLine(new System.Windows.Point(0, 0), new System.Windows.Point(2 * a1 + 2 * a2 + 2 * a3 + a5, 0));
            DrawingLine(new System.Windows.Point(0, b1), new System.Windows.Point(a1, b1));
            DrawingLine(new System.Windows.Point(a1, b1), new System.Windows.Point(a1 + a2, b1 + b2));
            DrawingLine(new System.Windows.Point(a1 + a2, b1 + b2), new System.Windows.Point(a6, b1 + b2 + b3 + b4 + b5));
            DrawingLine(new System.Windows.Point(a6, b1 + b2 + b3 + b4 + b5), new System.Windows.Point(a6 + 2 * a7 + a9, b1 + b2 + b3 + b4 + b5));
            DrawingLine(new System.Windows.Point(a6 + 2 * a7 + a9, b1 + b2 + b3 + b4 + b5), new System.Windows.Point(a1 + a2 + 2 * a3 + a5, b1 + b2));
            DrawingLine(new System.Windows.Point(a1 + a2 + 2 * a3 + a5, b1 + b2), new System.Windows.Point(a1 + 2 * a2 + 2 * a3 + a5, b1));
            DrawingLine(new System.Windows.Point(a1 + 2 * a2 + 2 * a3 + a5, b1), new System.Windows.Point(2 * a1 + 2 * a2 + 2 * a3 + a5, b1));
            DrawingLine(new System.Windows.Point(a1 + a2 + a3, b1), new System.Windows.Point(a1 + a2 + a3 + a5, b1));
            DrawingLine(new System.Windows.Point(a1 + a2 + a3 + a5, b1), new System.Windows.Point(a1 + a2 + a3 + a4 + a5, b1 + b2));
            DrawingLine(new System.Windows.Point(a1 + a2 + a3 + a4 + a5, b1 + b2), new System.Windows.Point(a6 + a7 + a8 + a9, b1 + b2 + b3));
            DrawingLine(new System.Windows.Point(a6 + a7 + a9, b1 + b2 + b3 + b4), new System.Windows.Point(a6 + a7, b1 + b2 + b3 + b4));
            DrawingLine(new System.Windows.Point(a6 + a7 - a8, b1 + b2 + b3), new System.Windows.Point(a1 + a2 + a3 - a4, b1 + b6));
            DrawingLine(new System.Windows.Point(a1 + a2 + a3 - a4, b1 + b6), new System.Windows.Point(a1 + a2 + a3, b1));
            if (b4 == 0)
            {
            }
            else
            {
                DrawingLine(new System.Windows.Point(a6 + a7 + a8 + a9, b1 + b2 + b3), new System.Windows.Point(a6 + a7 + a9, b1 + b2 + b3 + b4));
                DrawingLine(new System.Windows.Point(a6 + a7, b1 + b2 + b3 + b4), new System.Windows.Point(a6 + a7 - a8, b1 + b2 + b3));
            }
        }
        private void InitTextbox()
        {
            this.aa1.Text = 17.5.ToString();
            this.aa2.Text = 19.2.ToString();
            this.aa3.Text = 33.ToString();
            this.aa4.Text = 15.ToString();
            this.aa5.Text = 113.ToString();
            this.aa6.Text = 67.ToString();
            this.aa7.Text = 28.ToString();
            this.aa8.Text = 16.ToString();
            this.aa9.Text = 51.ToString();
            this.bb1.Text = 15.ToString();
            this.bb2.Text = 6.5.ToString();
            this.bb3.Text = 110.ToString();
            this.bb4.Text = 9.5.ToString();
            this.bb5.Text = 18.ToString();
            this.bb6.Text = 7.ToString();
            this.aa1.TextChanged += Aa1_TextChanged;
    
        }
    
        private void Aa1_TextChanged(object sender, TextChangedEventArgs args)
        {
            mainPane1.Children.Clear();
            System.Windows.Controls.TextBox textBox = sender as System.Windows.Controls.TextBox;
            if (textBox == null)
                return;
            double x1 = Convert.ToDouble(textBox.Text);
            double x2 = Convert.ToDouble(aa2.Text);
            double x3 = Convert.ToDouble(aa3.Text);
            double x4 = Convert.ToDouble(aa4.Text);
            double x5 = Convert.ToDouble(aa5.Text);
            double x6 = Convert.ToDouble(aa6.Text);
            double x7 = Convert.ToDouble(aa7.Text);
            double x8 = Convert.ToDouble(aa8.Text);
            double x9 = Convert.ToDouble(aa9.Text);
            double y1 = Convert.ToDouble(bb1.Text);
            double y2 = Convert.ToDouble(bb2.Text);
            double y3 = Convert.ToDouble(bb3.Text);
            double y4 = Convert.ToDouble(bb4.Text);
            double y5 = Convert.ToDouble(bb5.Text);
            double y6 = Convert.ToDouble(bb6.Text);
            DrawingLine(new System.Windows.Point(0, 0), new System.Windows.Point(2 * x1 + 2 * x2 + 2 * x3 + x5, 0));
            DrawingLine(new System.Windows.Point(0, y1), new System.Windows.Point(x1, y1));
            DrawingLine(new System.Windows.Point(x1, y1), new System.Windows.Point(x1 + x2, y1 + y2));
            DrawingLine(new System.Windows.Point(x1 + x2, y1 + y2), new System.Windows.Point(x6, y1 + y2 + y3 + y4 + y5));
            DrawingLine(new System.Windows.Point(x6, y1 + y2 + y3 + y4 + y5), new System.Windows.Point(x6 + 2 * x7 + x9, y1 + y2 + y3 + y4 + y5));
            DrawingLine(new System.Windows.Point(x6 + 2 * x7 + x9, y1 + y2 + y3 + y4 + y5), new System.Windows.Point(x1 + x2 + 2 * x3 + x5, y1 + y2));
            DrawingLine(new System.Windows.Point(x1 + x2 + 2 * x3 + x5, y1 + y2), new System.Windows.Point(x1 + 2 * x2 + 2 * x3 + x5, y1));
            DrawingLine(new System.Windows.Point(x1 + 2 * x2 + 2 * x3 + x5, y1), new System.Windows.Point(2 * x1 + 2 * x2 + 2 * x3 + x5, y1));
            DrawingLine(new System.Windows.Point(x1 + x2 + x3, y1), new System.Windows.Point(x1 + x2 + x3 + x5, y1));
            DrawingLine(new System.Windows.Point(x1 + x2 + x3 + x5, y1), new System.Windows.Point(x1 + x2 + x3 + x4 + x5, y1 + y2));
            DrawingLine(new System.Windows.Point(x1 + x2 + x3 + x4 + x5, y1 + y2), new System.Windows.Point(x6 + x7 + x8 + x9, y1 + y2 + y3));
            DrawingLine(new System.Windows.Point(x6 + x7 + x9, y1 + y2 + y3 + y4), new System.Windows.Point(x6 + x7, y1 + y2 + y3 + y4));
            DrawingLine(new System.Windows.Point(x6 + x7 - x8, y1 + y2 + y3), new System.Windows.Point(x1 + x2 + x3 - x4, y1 + y6));
            DrawingLine(new System.Windows.Point(x1 + x2 + x3 - x4, y1 + y6), new System.Windows.Point(x1 + x2 + x3, y1));
            if (y4 == 0)
            {
            }
            else
            {
                DrawingLine(new System.Windows.Point(x6 + x7 + x8 + x9, y1 + y2 + y3), new System.Windows.Point(x6 + x7 + x9, y1 + y2 + y3 + y4));
                DrawingLine(new System.Windows.Point(x6 + x7, y1 + y2 + y3 + y4), new System.Windows.Point(x6 + x7 - x8, y1 + y2 + y3));
            }
    
        }
    
        protected void DrawingLine(System.Windows.Point startPt, System.Windows.Point endPt)
        {
            LineGeometry myLineGeometry = new LineGeometry();
            myLineGeometry.StartPoint = startPt;
            myLineGeometry.EndPoint = endPt;
    
            Path myPath = new Path();
            myPath.Stroke = System.Windows.Media.Brushes.Black;
            myPath.StrokeThickness = 1;
            myPath.Data = myLineGeometry;
    
            this.mainPane1.Children.Add(myPath);
        }
    }
    }
    

    我的程序是再Revit中运行的,结果就是实时修改画线。

    点赞 评论
  • chenxiaohui318
    大苟姬 2018-11-27 15:06

    TextBox是类
    aa1和aa2都是对象。在这个方法前,有没有实例化TextBox aa2?

    点赞 评论
  • dabocaiqq
    dabocaiqq 2018-11-28 02:41

    你看下,你的id和你的控件名要匹配,大小写也要一致,否则找不到就会报错。

    点赞 评论
  • CSDNSion
    csdnSion 2018-11-28 04:18

    手动编写的嘛?不是拖拽的话,要自己手动实例化
    比如: private TextBox textBox1;private TextBox textBox2;
    实例化:
    textBox1 = new TextBox();textBox2 = new TextBox();
    textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
    对象简单的说就是 textBox1,textBox2 类似这种带new **()

    点赞 评论
  • x657032732
    x657032732 2018-11-28 08:10

    看看是否是 aa1.Text值为空导致的

    点赞 评论

相关推荐