Autor Beitrag
L4a-Thompson
Hält's aus hier
Beiträge: 6



BeitragVerfasst: So 24.07.11 15:02 
hallo alle zusammen,
ich bekome folgenden fehler: "System.NullReferenceException" wurde aufgefangen. Message=Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.


ich habe ein button array und ein textbox array per laufzeit erzeugt.
jetzt bekomme ich bei der Cut_Button Methode die oben beschriebene fehlermeldung.


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
private void MyButtonClickHandler(object sender, EventArgs e)
        {
            Button _cut_button = (Button)sender;
            int i = int.Parse(_cut_button.Name.Remove(0,7));
            Cut_Button(i);
        }

       
        public void Cut_Button(int i)
        {
            try
            {
                Clipboard.SetText(_serial_tb[i].Text);
                _serial_tb[i].Text = String.Empty;
            }
            catch { }
        }



i wird sauber übergeben. was mache ich falsch?


mfg
Thompson

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19312
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 24.07.11 15:12 
Wo kommt denn der Fehler?

So auf den ersten Blick würde ich vermuten _serial_tb[i] ist null. Das kannst du ja im Debugger prüfen.
L4a-Thompson Threadstarter
Hält's aus hier
Beiträge: 6



BeitragVerfasst: So 24.07.11 16:31 
stimmt aber warum ist _serial_tb[i] = null?

es steht etwas in _serial_tb[i].Text drin wenn diese erzeugt wird.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19312
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 24.07.11 17:24 
Wie sieht denn davon der Quelltext aus? Vielleicht wird das in die falschen Stellen des Arrays gelegt oder ähnliches...

Schau dir doch beim Erzeugen im Debugger einmal das Array an. Landen die Objekte an den richtigen Stellen? Sind diese beim auslesen noch da?
L4a-Thompson Threadstarter
Hält's aus hier
Beiträge: 6



BeitragVerfasst: So 24.07.11 18:13 
ja sind noch da hier mal der code der klasse


ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Key
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        ReadFile readfile = new ReadFile();
        List<string> array;
        TextBox[] _serial_tb;        
        Button[] _cut_bt;


       private void Form1_Load(object sender, EventArgs e)
       {
           Combobox_fuellen();           
       }

        
        //Datei Laden
        public List<string> Combobox_fuellen()
       {
           List<string> _datei_lines = readfile.ReadAll();
            int i;
            
            for (i = 0; i <= readfile.ReadAll().Count; i = i + 3)
            {
                _programme_cb.Items.Add(_datei_lines[i]);
            }          

            return _datei_lines;           
        }


        public int Der_Index()
        {
            List<string> array = Combobox_fuellen();
            
            int _der_index = 0;
            int i = 0;

            for (i = 0; i <= array.Count; i++)
            {
                if (array[i] == _programme_cb.Text)
                {
                    _der_index = i;
                    break;
                }
            }
            return _der_index;
        }


        private void Boxen_erstellen()
        {
            array = Combobox_fuellen();
            int i = Der_Index();    
            int _parts_anzahl = 0;            
            int x = 3;
            int y = 10;


           
            string[] parts = array[i+1].Split(new Char[] { ' ' });
            _parts_anzahl = parts.Count();


            # region Erstellen der Textboxen und Cut Button`s

            // Position der Textboxen
            for (i = 0; i <= _parts_anzahl; i++)
            {
                if (i == 0)
                {
                    x = 3;
                }
                else
                {
                    try
                    {
                        float current_font_size = _textbox_container_panel.Font.Size;
                        Font font = new System.Drawing.Font("Microsoft Sans Serif", current_font_size);
                        Size _text_size = TextRenderer.MeasureText(parts[i - 1], font);
                        x = x + _text_size.Width + 20;
                    } 
                    catch { }
                }


                // Textboxen und Cut Button`s selbst erstellen
                try

                {
                    _serial_tb = new TextBox[_parts_anzahl];
                    _serial_tb[i] = new TextBox();
                    _serial_tb[i].Name = "_serial_tb" + i.ToString();
                    _serial_tb[i].Enabled = true;                     
                    _textbox_container_panel.Controls.Add(_serial_tb[i]);
                    _serial_tb[i].ReadOnly = true;



                    float current_font_size = _textbox_container_panel.Font.Size;
                    Font font = new System.Drawing.Font("Microsoft Sans Serif", current_font_size);
                    Size _text_size = TextRenderer.MeasureText(parts[i], font);
                    _serial_tb[i].Width = _text_size.Width;
                    _serial_tb[i].Location = new Point(x, y);

                    _serial_tb[i].Text = parts[i];


                    _cut_bt = new Button[_parts_anzahl];
                    _cut_bt[i] = new Button();
                    _cut_bt[i].Name = "_cut_bt" + i.ToString();
                    int i2 = i + 1;
                    _cut_bt[i].Text = "Cut" + i2;
                    _cut_bt[i].Enabled = true;
                    _cut_bt[i].Width = 37;
                    _cut_bt[i].Height = 25;
                    _cut_buttons_panel.Controls.Add(_cut_bt[i]);
                    _cut_bt[i].Location = new Point(x, 0);

                    _cut_bt[i].Click += new System.EventHandler(MyButtonClickHandler);


                }
                catch { }
            #endregion
            } 
        }
      
                
        private void _programme_cb_SelectedIndexChanged(object sender, EventArgs e)
        {
            // alte Textboxen 6 Button Löschen
            _textbox_container_panel.Controls.Clear();
            _cut_buttons_panel.Controls.Clear();
            Boxen_erstellen();
        }
        
        
        private void MyButtonClickHandler(object sender, EventArgs e)
        {
            Button _cut_button = (Button)sender;
            int i = int.Parse(_cut_button.Name.Remove(0,7));
            Cut_Button(i);
        }

       
        public void Cut_Button(int i)
        {
            try
            {
                Clipboard.SetText(_serial_tb[i].Text);
                _serial_tb[i].Text = String.Empty;
            }
            catch { }
        }


        private void _beenden_bt_Click(object sender, EventArgs e)
        {

            Application.Exit();
        }
    }
}


Moderiert von user profile iconMartok: C#-Tags hinzugefügt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19312
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 24.07.11 18:26 
user profile iconL4a-Thompson hat folgendes geschrieben Zum zitierten Posting springen:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
            // Position der Textboxen
            for (i = 0; i <= _parts_anzahl; i++)
            {
                [...]
                // Textboxen und Cut Button`s selbst erstellen
                try
                {
                    _serial_tb = new TextBox[_parts_anzahl];
                    _serial_tb[i] = new TextBox();
                    _serial_tb[i].Name = "_serial_tb" + i.ToString();
Autsch, sowas hatte ich mir schon fast gedacht. ;-)
Du erstellst das Array bei jedem Durchlauf der Schleife neu, so dass du den bereits hineingepackten Inhalt wieder löschst. Es ist also nur die letzte Textbox drin.
L4a-Thompson Threadstarter
Hält's aus hier
Beiträge: 6



BeitragVerfasst: So 24.07.11 20:04 
perfekt.
hat funktioniert als ich die beiden arrays auserhalb der schleife platziert habe.
vielen dank. war erst mein zweites programm und nun ist es fertig.


mfg
Thompson