Object reference not set to an instance of an object

D

Dakkar

I made my code like this and im getting the error that i write on the
topic
what should be the problem?


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


public class baglanti : Form
{
private Button btn;
private TextBox text1;
private TextBox text2;
private Label label1, label2;
private RichTextBox txt1;


public baglanti()
{
String path = @"C:\Documents and Settings\Sdkr\My
Documents\Visual
Studio\Projects\connector\connector\sylcn.jpg";
this.Size = new Size(400, 300);
this.Location = new Point(140, 200);
this.Text = " Baglanti Programi";
this.BackgroundImage = new Bitmap(path);
this.MaximizeBox = false;


RichTextBox txt1 = new RichTextBox();
txt1.Size = new Size(100, 100);
txt1.Location = new Point(8, 160);
txt1.Text = "";

label1 = new Label();
label1.Text = "Kullanici Adi:";
label1.Size = new Size(70, 20);
label1.Location = new Point(8, 50);

label2 = new Label();
label2.Text = "Sifre:";
label2.Size = new Size(70, 20);
label2.Location = new Point(8, 75);

text1 = new TextBox();
text1.Size = new Size(100, 15);
text1.Location = new Point(80, 50);
text1.MaxLength = 20;


text2 = new TextBox();
text2.Size = new Size(100, 15);
text2.Location = new Point(80, 75);
text2.MaxLength = 20;

btn = new Button();
btn.Size = new Size(50, 25);
btn.Text = "Baglan";
btn.Location = new Point(8, 100);
btn.Click += new EventHandler(btn_clicked);

this.Controls.Add(label1);
this.Controls.Add(label2);
this.Controls.Add(text1);
this.Controls.Add(text2);
this.Controls.Add(txt1);
this.Controls.Add(btn);

}

public void sorgu(String u_name, String pw)
{
this.txt1.Text = this.txt1.Text+" "+u_name+"
"+pw; //error is here

}

public void btn_clicked(object ob, EventArgs e)
{
this.sorgu(this.text1.Text,this.text1.Text);
}


*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
S

Simon Tammaru

This line:

RichTextBox txt1 = new RichTextBox();

should read instead:

txt1 = new RichTextBox();

because you are specifying the RichTextBox type before the variable name the
compiler treats it as a brand new variable with a scope of that single
method, this means that the private RichTextBox txt1 is not initialized.

HTH

Jax
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top