我正在創建查看器。第一次進入目錄時,通過處理文件,精確圖像和序列化數據到數據文件來創建數據文件。然後,我將該新創建的文件反序列化爲要查看的表單。第二次進入目錄時,它會看到該文件嘗試將其反序列化以填充表單。系統需要首先創建時才能正常工作,但如果已經存在,我會收到未引用的對象錯誤。我錯過了什麼?序列化 - 反序列化(二進制)
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
listView1.Items.Clear();
fileInfoList.Clear();
//fileNameList.Clear();
ClearFlowPanel();
TreeNode newSelected = e.Node;
DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
dirPath = nodeDirInfo.FullName;
label_selectedPath.Text = dirPath;
foreach (FileInfo file in nodeDirInfo.GetFiles("*.sbs", option))
{
if (file.Extension == ".sbs")
{
fileInfoList.Add(file);
}
}
foreach (FileInfo info in fileInfoList)
{
ListViewItem i = listView1.Items.Add(info.Name, 1);
i.SubItems.Add(SizeInKB(info.Length));
i.SubItems.Add(info.LastWriteTime.ToShortDateString());
}
listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
string binData = dirPath + "\\" + ".browser" + "\\" + "_browser.bin";
if (File.Exists(binData))
{
DeserializeData(binData); //creates error
}
}
private void DeserializeData(string binPath)
{
FileStream fs = new FileStream(binPath, FileMode.Open);
BinaryFormatter bin = new BinaryFormatter();
int length = (int)bin.Deserialize(fs);
MessageBox.Show(length.ToString());
for (int i = 0; i < length; i++)
{
viewerData[i] = (ViewerData)bin.Deserialize(fs); //problem
}
for (int i = 0; i < viewerData.Length; i++)
{
PopulateFlowControl(viewerData[i]);
viewerNameList.Add(viewerData[i].name);
}
}
private void UpdateDirectory()
{
thumbPath = dirPath + "\\" + ".browser";
if (!Directory.Exists(thumbPath))
{
Directory.CreateDirectory(thumbPath);
}
fileInfoArray = fileInfoList.ToArray();
viewerData = new ViewerData[fileInfoArray.Length];
string binData = thumbPath + "\\" + "_browser.bin";
Stream stream = File.Open(binData, FileMode.Create);
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, fileInfoArray.Length);
ProgressBar_Form progressBar = new ProgressBar_Form(fileInfoArray.Length);
progressBar.Show();
for (int i = 0; i < fileInfoArray.Length; i++)
{
viewerData[i] = new ViewerData(fileInfoArray[i]);
bin.Serialize(stream, viewerData[i]);
progressBar.progressBar1.PerformStep();
progressBar.label_progress.Text = "Processing : " + fileInfoArray[i].Name;
viewerData[i].image.Dispose();
if (File.Exists(viewerData[i].imagePath))
{
File.Delete(viewerData[i].imagePath);
}
}
stream.Close();
progressBar.Close();
DeserializeData(binData); //works fine
}
編輯:
錯誤:未設置爲一個對象的實例對象引用 - 在線路在第一在DeserializeData(串binPath)評論「問題」 for循環;
堆棧跟蹤......在X
在Substance_Browser_12.Form1.DeserializeData(字符串binPath):\ Visual Studio 2010的\項目\物質設計師\ Substance_Browser_12 \ Substance_Browser_12 \ Form1.cs中:在Substance_Browser_12線151 。 Form1.treeView1_NodeMouseClick(Object sender,TreeNodeMouseClickEventArgs e)in X:\ Visual Studio 2010 \ Projects \ Substance Designer \ Substance_Browser_12 \ Substance_Browser_12 \ Form1.cs:line 133 at System.Windows.Forms.TreeView.OnNodeMouseClick(TreeNodeMouseClickEventArgs e) at System.Windows.Forms.TreeView.WmNotify(Message & m) at System.Windows.Forms.TreeView.WndProc(Message &米) 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息&米) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&米) 在System.Windows.Forms.NativeWindow.DebuggableCallback (IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd,Int32 msg,IntPtr wParam,IntPtr lParam)在System.Windows.Forms.Control.SendMessage(Int32 MSG,IntPtr的WPARAM,IntPtr的LPARAM) 在System.Windows.Forms.Control.ReflectMessageInternal(IntPtr的的HWND,消息&米) 在System.Windows.Forms.Control.WmNotify(消息&米) 一個噸System.Windows.Forms.Control.WndProc(消息&米) 在System.Windows.Forms.ScrollableControl.WndProc(消息&米) 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息&米) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&米) 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr的的HWND,MSG的Int32,IntPtr的WPARAM,IntPtr的LPARAM) 在System.Windows.Forms.UnsafeNativeMethods .CallWindowProc(IntPtr wndProc,IntPtr hWnd,Int32 msg,IntPtr wParam,IntPtr lParam) at System.Windows.Forms.NativeWindow.DefWndProc(Message & m) at System.Windows.Forms.Control.DefWndProc(Message &米) 在System.Windows.Forms.TreeView.WmMouseDown(消息&米,MouseButtons按鈕,點擊的Int32) 在System.Windows.Forms.TreeView.WndProc(消息&米) 在System.Windows.Forms.Control的.ControlNativeWindow.OnMessage(消息&米) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&米) 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr的的HWND,MSG的Int32,IntPtr的WPARAM,IntPtr的LPARAM ) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG & msg) at System.Windows。Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context) at System .Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at Substance_Browser_12.Program.Main()in X:\ Visual Studio 2010 \ Projects \ Substance Designer \ Substance_Browser_12 \ Substance_Browser_12 \ Program.cs:line 18 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args) at System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在System.Threading.ThreadHelper.ThreadStart_Context(對象狀態) 在System.Threading.ExecutionContext.RunInternal(ExecutionContext中的ExecutionContext,ContextCallback回調,對象狀態,布爾preserveSyncCtx) 的系統。 Threading.ExecutionContext.Run(的ExecutionContext的ExecutionContext,ContextCallback回調,對象的狀態,布爾preserveSyncCtx) 在System.Threading.ExecutionContext.Run(的ExecutionContext的ExecutionContext,ContextCallback回調,對象狀態) 在System.Threading.ThreadHelper.ThreadStart()
在哪一行你會得到未引用的對象錯誤? – paqogomez
你能提供錯誤信息和堆棧跟蹤嗎? – Grundy
感謝您添加實際的例外文本。 「未設置對象實例的對象引用」與「未引用的對象錯誤」並不等同,這是誤導。 – groverboy