» »

[ c# ] NullRefferenceException

[ c# ] NullRefferenceException

atasmrk ::

Torej, sledeco kodo sem skopiral z neta (no, ne cisto vse, tisto if in naprej dol).

public bool startMonitoring()
{
   IntPtr pNotifyInfo = IntPtr.Zero;
   IntPtr handle = IntPtr.Zero;
   int change = 0;
   int waitResult = 0;

   handle = FindFirstPrinterChangeNotification(hPrinter, PRINTER_CHANGE_JOB, 0, IntPtr.Zero);
   waitResult = WaitForSingleObject(handle, INFINITE);

   if ( FindNextPrinterChangeNotification(handle, ref change, IntPtr.Zero, out pNotifyInfo)) 
   {
      PRINTER_NOTIFY_INFO info = (PRINTER_NOTIFY_INFO) Marshal.PtrToStructure (pNotifyInfo, typeof (PRINTER_NOTIFY_INFO));     
      int pData = (int)pNotifyInfo + Marshal.SizeOf( ypeof(PRINTER_NOTIFY_INFO));
      PRINTER_NOTIFY_INFO_DATA[] data = new PRINTER_NOTIFY_INFO_DATA[info.Count];
      for ( uint i = 0; i < info.Count; i++ )
      {
         data[i] = (PRINTER_NOTIFY_INFO_DATA)Marshal.PtrToStructure((IntPtr)pData, typeof(PRINTER_NOTIFY_INFO_DATA));
         pData += Marshal.SizeOf( typeof(PRINTER_NOTIFY_INFO_DATA));
      }
      FreePrinterNotifyInfo( pNotifyInfo );
      Console.WriteLine("konec branja strukture");
   }
   return false;
}


pred tem pa imam deklarirani strukturi v istem razredu

struct PRINTER_NOTIFY_INFO
 { 
	public uint Version; 
	public uint Flags; 
	public uint Count; 
	// Array of PRINTER_NOTIFY_INFO_DATA follows
}

struct PRINTER_NOTIFY_INFO_DATA
{
        public ushort Type; 
	public ushort Field; 
	public uint Reserved; 
	public uint Id; 
	public uint cbBuf;  // or adwData[0]
	public IntPtr pBuf; // or uint adwData[1]
}


Ob prevajanju te kode mi javi warning

PRINTER_NOTIFY_INFO.Version' is never assigned to, and will always have its default value 0

ob izvajanju prve vrstice znotraj if-a pa tisti slavni

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

Tu imam pa toliko stvari, da ne vem, kateri ogjekt zadeva misli. Se komu kaj vec sanja?
  • spremenilo: atasmrk ()

Kekec ::

Za začetek bi lahko preveril če ti je sploh kaj dalo v pNotifyInfo, če je pa prazen pa je exception čist razumljiv!

atasmrk ::

Ja, ekekec, to je bilo to. Nic se ni tja noter zapisalo. In ko sem se se malo poglobil v MSDN, sem opazil, da s tistim IntPtr.Zero sploh ne zahtevam podatkov. Tako sem deklariral se eno strukturo PRITNER_NOTIFY_OPTIONS

struct PRINTER_NOTIFY_OPTIONS
{
	public uint Version; 
	public uint Flags; 
	public uint Count;
	public IntPtr buf;
}


in jo porinil na tisto mesto v klicu procedure, kjer je bila potrebna. tistega buf se sploh nisem dotika, ker sem flags nastavil na PRINTER_NOTIFY_OPTIONS_REFRESH, in bi torej ta funkcija morala vse druge clane te strukture ignorirati.
Sedaj pa dobim NullRefferenceException v klicu funkcije FindNexPrinterChangeNotification(). In mi se zmeraj ni jasno, kaj je narobe. Kaj bi pa sedaj, laho bilo 0?

atasmrk ::

ce pa naredim

PRINTER_NOTIFY_OPTIONS notifyOptions;
IntPtr no = IntPtr.Zero;

notifyOptions.Count = 0;
notifyOptions.Flags = PRINTER_NOTIFY_OPTIONS_REFRESH;
notifyOptions.Version = 2;
notifyOptions.buf = IntPtr.Zero;
no = Marshal.AllocCoTaskMem(Marshal.SizeOf(notifyOptions));


in potem klicem

if (FindNextPrinterChangeNotification(this.hChangeObject, ref change, no, pNotifyInfo)) 


pa stvar vrne false, GetLastError() pa vrne ERROR_INVALID_PARAMETER. Kaki prebliski?


Vredno ogleda ...

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

[C#] pixel scan z Thread

Oddelek: Programiranje
121169 (951) perooo25
»

kako narediti program ki bere drugi program

Oddelek: Programiranje
465742 (4055) imagodei
»

video v c#

Oddelek: Programiranje
241535 (1075) boss-tech
»

[C#] Sesutje aplikacije

Oddelek: Programiranje
111614 (1449) Jean-Paul
»

[C#] unmanaged->managed code

Oddelek: Programiranje
112141 (2016) Zzzzzzz

Več podobnih tem