2011-09-14 66 views
-1

我對這個失去了我的想法。任何人都可以發現錯誤在我的代碼註釋,因爲是給我一個NullReferenceException錯誤在強調一點....在此先感謝您的任何建議以下...c#Stream.Seek給我一個'NullReferenceException'

//retreive the picture from isolated storage 
    Stream file = PicturesLoader.LoadFileFromStorage(PhoneApplicationService.Current.State[PictureCrop.PictureStateKey_Url].ToString()); 
    BitmapImage bi = new BitmapImage(); 
    bi.SetSource(file); 

    //load _pic 
    Picture _pic = new Picture(); 

    _pic.Name = "TempFile"; 
    _pic.Url = PhoneApplicationService.Current.State[PictureCrop.PictureStateKey_Url].ToString(); 
    _pic.Height = bi.PixelHeight; 
    _pic.Width = bi.PixelWidth; 
    _pic.Bitmap = bi; 

    //save the new cropped image for later use 
    file.Seek(0, SeekOrigin.Begin); 
    App._capturedImage.SetSource(file); <------ THROWS ERROR HERE, suggest file = NullReferenceException ??????????????????? 

    //Get rid of the stream.... 
    file.Dispose(); 
+0

難道你不能把一個斷點,而不是猜測? App,_capturedImage也可以爲null。 – Polyfun

+1

在這段代碼中,App和_apturedImage都從天而降。優秀的NRE候選人。 *文件*變量不是。使用調試器。 –

+0

我會推薦這個[在C#中調試指南](http://msdn.microsoft.com/en-us/vcsharp/aa336801)。也許在C#的基礎課程之上。 –

回答

2

一槍在黑暗中:大概_capturedImagenull之一。

+0

謝謝阿德里安,你當然是對的,非常感謝.. –

+0

很高興爲您提供幫助。它發生在我們所有人身上:有時我們只是失明,不能看到發生了什麼,即使它在那裏,尖叫...... –

1

要麼_capturedImage是空引用,或者(假設App是一個字段或屬性而不是類名)App正在返回空引用。