How can I fix the error: System.NullReferenceException: 'Object reference not set to an instance of an object.'

Farhoud Alimohammadi 0 Reputation points
2024-08-31T16:10:16.2+00:00

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Drawing;

using System.Windows.Forms;

namespace int_Kiosk

{

class slipingLabel : Label

{

    private Timer timer = new Timer();

    private bool slide=false;

    private int a=0;

    private bool art = false;

    public slipingLabel()

    {


        

        base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint |

            ControlStyles.SupportsTransparentBackColor | ControlStyles.ResizeRedraw |

            ControlStyles.UserPaint, true);

        this.AutoSize = false;

        base.Width = 30;

        base.Height = 15;

        this.a = 0;


        

        this.timer.Tick += new EventHandler(this.timer_Tick);

        this.timer.Interval = 0;

        this.slide = false;

        this.timer.Enabled = false;

    }

    protected override void Dispose(bool disposing)

    {

        this.timer.Stop();

        base.Dispose(disposing);

    }

    protected override void OnBackColorChanged(EventArgs e)

    {

        base.Invalidate();

        base.OnBackColorChanged(e);

    }

    protected override void OnCreateControl()

    {

        base.OnCreateControl();

    }

    protected override void OnForeColorChanged(EventArgs e)

    {

        base.Invalidate();

        base.OnForeColorChanged(e);

    }

    protected override void OnPaint(PaintEventArgs e)

    {

        Brush brush;

        using (brush = (Brush)new SolidBrush(this.BackColor))

        {

            e.Graphics.FillRectangle(brush, base.ClientRectangle);

        }

        Size size = TextRenderer.MeasureText(this.Text, this.Font);

        int num = (base.Height / 2) - (size.Height / 2);

        using (brush = (Brush)new SolidBrush(this.ForeColor))

        {

            e.Graphics.DrawString(this.Text, this.Font, brush, (float)this.a, (float)num);

        }

        base.OnPaint(e);

    }

    protected override void OnResize(EventArgs e)

    {

        this.timer.Enabled = true;

        base.OnResize(e);

    }

    private void timer_Tick(object sender, EventArgs e)

    {

        Size size = TextRenderer.MeasureText(this.Text, this.Font);

        if (size.Width <= base.Width)

        {

            this.timer.Stop();

            this.a = 1;

            base.Invalidate();

        }

        else

        {

            int num = (size.Width >= base.Width) ? (size.Width - base.Width) : 0;

            if (this.a >= 1)

            {

                this.art = false;

            }

            if (-this.a >= (num = this.Font.Height))

            {

                this.art = true;

            }

            this.a = this.art ? (this.a + 1) : this.a - 1;

            base.Invalidate();

        }

    }


    

    public int SlideTime

    {

        get =>

            this.timer.Interval;

        set

        {

            this.timer.Interval = value;

            base.Invalidate();

        }

    }

    public bool Slide

    {

        get =>

            this.slide;

        set

        {

            this.slide = true;

            this.timer.Enabled = this.slide;

            if (!this.slide)

            {

                this.a = 0;

                base.Invalidate();

            }

        }

    }

    public override string Text

    {

        get => base.Text;

        set

        {

            base.Text = value;

            this.timer.Start();

        }

    }

}
```}

Error: System.NullReferenceException: 'Object reference not set to an instance of an object.'

I would really appreciate if my error is solved in any way.

Thanks in advance
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,841 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 44,501 Reputation points Microsoft Vendor
    2024-09-02T09:23:08.2433333+00:00

    Hi @Farhoud Alimohammadi , Welcome to Microsoft Q&A,

    From your code, it can be seen that this is a custom label control of Winform. There is an obvious error that this.timer.Interval cannot be 0.

    This is the optimized code:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace xxx
    {
        public partial class SlidingLabel : Label
        {
            private readonly Timer _timer = new Timer();
            private int _offset = 0;
            private bool _isSliding = false;
            private bool _directionRight = false;
    
            public SlidingLabel()
            {
                SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint |
                         ControlStyles.SupportsTransparentBackColor | ControlStyles.ResizeRedraw |
                         ControlStyles.UserPaint, true);
    
                AutoSize = false;
                Width = 30;
                Height = 15;
    
                _timer.Interval = 10;
                _timer.Tick += Timer_Tick;
            }
    
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    _timer.Dispose();
                }
                base.Dispose(disposing);
            }
    
            protected override void OnBackColorChanged(EventArgs e)
            {
                Invalidate();
                base.OnBackColorChanged(e);
            }
    
            protected override void OnForeColorChanged(EventArgs e)
            {
                Invalidate();
                base.OnForeColorChanged(e);
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                using (var backgroundBrush = new SolidBrush(BackColor))
                {
                    e.Graphics.FillRectangle(backgroundBrush, ClientRectangle);
                }
    
                var textSize = TextRenderer.MeasureText(Text, Font);
                int yPosition = (Height / 2) - (textSize.Height / 2);
    
                using (var textBrush = new SolidBrush(ForeColor))
                {
                    e.Graphics.DrawString(Text, Font, textBrush, _offset, yPosition);
                }
    
                base.OnPaint(e);
            }
    
            protected override void OnResize(EventArgs e)
            {
                _timer.Enabled = true;
                base.OnResize(e);
            }
    
            private void Timer_Tick(object sender, EventArgs e)
            {
                var textSize = TextRenderer.MeasureText(Text, Font);
                int maxOffset = textSize.Width - Width;
    
                if (textSize.Width <= Width)
                {
                    _timer.Stop();
                    _offset = 0;
                    Invalidate();
                }
                else
                {
                    if (_offset <= -maxOffset)
                    {
                        _directionRight = true;
                    }
                    else if (_offset >= 0)
                    {
                        _directionRight = false;
                    }
    
                    _offset += _directionRight ? 1 : -1;
                    Invalidate();
                }
            }
    
            public int SlideTime
            {
                get => _timer.Interval;
                set
                {
                    _timer.Interval = value;
                    Invalidate();
                }
            }
    
            public bool Slide
            {
                get => _isSliding;
                set
                {
                    _isSliding = value;
                    _timer.Enabled = _isSliding;
    
                    if (!_isSliding)
                    {
                        _offset = 0;
                        Invalidate();
                    }
                }
            }
    
            public override string Text
            {
                get => base.Text;
                set
                {
                    base.Text = value;
                    _timer.Start();
                }
            }
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.