前提
やっていることはOpenFileDialogを使い画像ファイルのパスを取得して、それを使いGetFileNameWithoutExtensionでファイル名を取得、pictureboxに表示、ディレクトリを取得してます。
string[]型にFileNameを入れたいのですがエラーが出てしまいます。
どうすればいいでしょうか?
発生している問題・エラーメッセージ
System.NullReferenceException HResult=0x80004003 Message=オブジェクト参照がオブジェクト インスタンスに設定されていません。 Source=画像変換 スタック トレース: 場所 画像変換.Form1.開くToolStripMenuItem_Click(Object sender, EventArgs e) (C:\Users\17S2023\Documents\自作ソフト\作業\画像変換\画像変換\Form1.cs):行 125 場所 System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e) 場所 System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e) 場所 System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) 場所 System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e) 場所 System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met) 場所 System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met) 場所 System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) 場所 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 場所 System.Windows.Forms.Control.WndProc(Message& m) 場所 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 場所 System.Windows.Forms.ToolStrip.WndProc(Message& m) 場所 System.Windows.Forms.MenuStrip.WndProc(Message& m) 場所 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 場所 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 場所 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 場所 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 場所 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 場所 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 場所 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 場所 System.Windows.Forms.Application.Run(Form mainForm) 場所 画像変換.Program.Main() (C:\Users\略\画像変換\Program.cs):行 19 この例外は、最初にこの呼び出し履歴でスローされました: 画像変換.Form1.開くToolStripMenuItem_Click(object, System.EventArgs) の Form1.cs System.Windows.Forms.ToolStripItem.RaiseEvent(object, System.EventArgs) System.Windows.Forms.ToolStripMenuItem.OnClick(System.EventArgs) System.Windows.Forms.ToolStripItem.HandleClick(System.EventArgs) System.Windows.Forms.ToolStripItem.HandleMouseUp(System.Windows.Forms.MouseEventArgs) System.Windows.Forms.ToolStripItem.FireEventInteractive(System.EventArgs, System.Windows.Forms.ToolStripItemEventType) System.Windows.Forms.ToolStripItem.FireEvent(System.EventArgs, System.Windows.Forms.ToolStripItemEventType) System.Windows.Forms.ToolStrip.OnMouseUp(System.Windows.Forms.MouseEventArgs) System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message, System.Windows.Forms.MouseButtons, int) System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message) ... [呼び出し履歴の切り捨て]
該当のソースコード
C#
1string name,path; 2string[] file; 3 private void 開くToolStripMenuItem_Click(object sender, EventArgs e) 4 { 5 OpenFileDialog of = new OpenFileDialog(); 6 of.InitialDirectory = @System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); 7 of.Title = "画像を選択"; 8 if (of.ShowDialog() == DialogResult.OK) 9 { 10 file[0]= (of.FileName); 11 name = (System.IO.Path.GetFileNameWithoutExtension(file[0])).ToString(); 12 pictureBox1.ImageLocation = file[0]; 13 path = System.IO.Path.GetDirectoryName(file[0]); 14 textBox3.Text = path; 15 textBox4.Text = name + "_edit"; 16 } 17 of.Dispose(); 18 }
試したこと
C#
1if (of.ShowDialog() == DialogResult.OK) 2 { 3 textbox3.text= (of.FileName); 4 }
だけ書くとエラーは出ずにtextboxにパスが表示されました。
C#
1if (of.ShowDialog() == DialogResult.OK) 2 { 3 file[0] = (of.FileName); 4 }
これはエラーが出ます。
上記のことからstring[]型にFileNameが入れられないと考えました。
回答2件
あなたの回答
tips
プレビュー