» »

null reference exception na čudnem mestu C#

null reference exception na čudnem mestu C#

multi_vit ::

Pri izvrševanju naslednje kode stranka na svojem računalniku dobi null reference exception, če program poganjam na svoji mašini do te napake ne pride ravno tako do napake ne pride na sodelavčevem račnalniku. Na vseh treh računalnikih je nameščen .NET framework 3,5 SP1.

Konstruktor glavne windows forme
public Main_Form()
{
InitializeComponent();
DAO = new DataAccessObject(); //objekt ki kasneje povzroča težave
Login_Form login = new Login_Form(DAO); //kliče se podforma
login.ShowDialog();

}

kontoruktor podforme

public Login_Form(DataAccessObject DAO)
{
this.DAO = DAO; //refenca objekta ki smo ga prej kreirali se to prepise
canChange = false;
textNameChanged = false;
textPassChanged = false;
loginUspel = false;
stageComplete = 0;
InitializeComponent();
}

program deluje normalno dokler stranka ne pritisne gumba za logiranje v program. Ta gumb reagira na naslednji event handler

private void Login_Button_Click(object sender, EventArgs e)
{
RegistryKey key = null;

if ((Login_Name.Text.Length == 0) || (Pass.Text.Length == 0))
{
//tukaj je nek brezvezni messagebox
return;
}

if ((textNameChanged == true) || (textPassChanged == true))
{
userForData = Login_Name.Text;
passForData = Pass.Text;
}

loginUspel = DAO.Connect(userForData, passForData);


//EDINO MESTO KJER LAHKO PRIDE DO NEOBRAVNAVANE NAPAKE, KER JE OSTALO V TRY CATCH BLOKIH DAO.connect METODA PREVERI ALI STA UPORABNIŠKO IME IN GESLO VELJAVNA ZA DOSTOP DO PODATKOVNE BAZE V PRIMERU ODOBRENEGA DOSTOPA VRNE TRUE DRUGAČE PA FALSE.


if (loginUspel)
{
if ((textNameChanged == true) || (textPassChanged == true))
{
try
{
//NEKAJ PISALNIH DOSTOPOV DO REGISTRA ZNOTRAJ TRY CATCH BLOKA
}
catch (Exception)
{

}
finally
{
key.Close();
}
}
}
else
{
try
{
if (MessageBox.Show(@"Napačno uporabniško ime ali geslo", "Napala!!!", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.Cancel)
{
System.Windows.Forms.Application.Exit();
}

//NEKAJ PISALNIH DOSTOPOV DO REGISTRA ZNOTRAJ TRY CATCH BLOKA
return;
}
catch (Exception)
{
}
finally
{
key.Close();
}
}

if (checkBox1.Checked == false)
{
try
{
//NEKAJ PISALNIH DOSTOPOV DO REGISTRA ZNOTRAJ TRY CATCH BLOKA
}
catch (Exception)
{
}
finally
{
key.Close();
}
}

this.Close();
}
Torej stranka dobi sporočilo o naslednji napaki

Details:


See the end of this message for details on invoking

just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************

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

at SOGLASJA_PROIZVAJALCI.Login_Form.Login_Button_Click(Object sender, EventArgs e) in C:\Pogodbe\......\Login_Form.cs:line 242

at System.Windows.Forms.Control.OnClick(EventArgs e)

at System.Windows.Forms.Button.OnClick(EventArgs e)

at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)

at System.Windows.Forms.Control.WndProc(Message& m)

at System.Windows.Forms.ButtonBase.WndProc(Message& m)

at System.Windows.Forms.Button.WndProc(Message& m)

at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)

at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************

mscorlib

Assembly Version: 2.0.0.0

Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)

CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll

----------------------------------------

SOGLASJA_PROIZVAJALCI

Assembly Version: 1.0.0.0

Win32 Version: 1.0.0.0

CodeBase: file:///D:/Uporabniki/xxxx.exe

----------------------------------------

System.Windows.Forms

Assembly Version: 2.0.0.0

Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)

CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll

----------------------------------------

System

Assembly Version: 2.0.0.0

Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)

CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll

----------------------------------------

System.Drawing

Assembly Version: 2.0.0.0

Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)

CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll

----------------------------------------

System.Data

Assembly Version: 2.0.0.0

Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)

CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll

----------------------------------------

System.Transactions

Assembly Version: 2.0.0.0

Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)

CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Transactions/2.0.0.0__b77a5c561934e089/System.Transactions.dll

----------------------------------------

System.EnterpriseServices

Assembly Version: 2.0.0.0

Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)

CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.EnterpriseServices/2.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll

----------------------------------------

System.Configuration

Assembly Version: 2.0.0.0

Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)

CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll

----------------------------------------

System.Xml

Assembly Version: 2.0.0.0

Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)

CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll

----------------------------------------

************** JIT Debugging **************

To enable just-in-time (JIT) debugging, the .config file for this

application or computer (machine.config) must have the

jitDebugging value set in the system.windows.forms section.

The application must also be compiled with debugging

enabled.

For example:



system.windows.forms jitDebugging="true"



When JIT debugging is enabled, any unhandled exception

will be sent to the JIT debugger registered on the computer

rather than be handled by this dialog box.


Kakšna ideja zakaj se na strankinem računalniku pojavlja ta napaka????

multi_vit ::

public Login_Form(DataAccessObject DAO)
{
this.DAO = DAO; //refenca objekta ki smo ga prej kreirali se to prepise
canChange = false;
textNameChanged = false;
textPassChanged = false;
loginUspel = false;
stageComplete = 0;
InitializeComponent();
}

Ok zdalje vidim to zadevo ali morda InitializeComponent postavi vse vrennosti na default torej tudi this.DAO na null?? Samo zakaj potem zadeva na moji mašini deluje??

Zgodovina sprememb…

Ericssony ::

Kakšna je pa koda v vrstici 242 v Login_Form ker tam pride do napake.

multi_vit ::

Kakšna je pa koda v vrstici 242 v Login_Form ker tam pride do napake.


Tja tole

key.Close();

napaka je kot kaze v tem ker stranka nima dostopa do pisanja v register na lastnem računalniku in potem je (key == null). Nekako nisem računal s tem da v primeru nedostopnosti registra pri naslednjem stavku

key = Registry.LocalMachine.CreateSubKey(@"Software\.......XXX");

ostane key == null

silly me;(

Ericssony ::

Potem pa z if stavkom to preveri in prestreži exception...

krho ::

Pa NE moreš pisat v HKLM kot ne admin uporabnik no.
si.Mail odprto-kodni odjemalec elektronske pošte. - http://www.simail.si
Uredite si svojo zbirko filmov, serij in iger - http://xcollect.sf.net


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

Seštevanje, odštevanje za drugošolce

Oddelek: Šola
101711 (1367) norcuron
»

[c#] Vprašanje glede release verzije aplikacije

Oddelek: Programiranje
51501 (1325) Ericssony
»

Tezava pri instanciranju class-a

Oddelek: Programiranje
61305 (1176) KernelPanic
»

Mono, je že kdo delal z njim?

Oddelek: Programiranje
171646 (1176) Beezgetz
»

Visual Basic.NET - za začetnika ?

Oddelek: Programiranje
304797 (3916) mHook

Več podobnih tem