質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Windows Forms

Windows Forms(WinForms)はMicrosoft .NET フレームワークに含まれる視覚的なアプリケーションのプログラミングインターフェイス(API)です。WinFormsは管理されているコードの既存のWindowsのAPIをラップすることで元のMicrosoft Windowsのインターフェイスのエレメントにアクセスすることができます。

Q&A

解決済

2回答

1005閲覧

string[]型にOpenFileDialogのFileNameを入れるとハンドルされていない例外が発生する

naoiwa24

総合スコア18

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Windows Forms

Windows Forms(WinForms)はMicrosoft .NET フレームワークに含まれる視覚的なアプリケーションのプログラミングインターフェイス(API)です。WinFormsは管理されているコードの既存のWindowsのAPIをラップすることで元のMicrosoft Windowsのインターフェイスのエレメントにアクセスすることができます。

0グッド

0クリップ

投稿2022/06/14 03:53

編集2022/06/14 03:55

前提

やっていることは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が入れられないと考えました。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Zuishin

2022/06/14 04:37

FileName は string 型なので string[] には入りません。 FileNames なら入ります。 file = of.FileNames;
naoiwa24

2022/06/14 05:24

ありがとうございます、できました。ベストアンサーにしたいので回答に投稿していただけませんでしょうか?
Zuishin

2022/06/14 05:29

自分で回答してそれをベストアンサーに選ぶ自己解決でお願いします。
guest

回答2

0

自己解決

FileName は string 型なので string[] には入らないので、

C#

1file = of.FileNames;

とすることで実装できました。

Zuishin様の要望で自己解決欄に記載します。本当にありがとうございました。

y_waiwai様の

C#

1string[] file= new string[1]();

はエラーが出ます。この場合はdodox86様の指摘通り()を外して

C#

1string[] file= new string[1]

で実行できます。

投稿2022/06/14 06:17

naoiwa24

総合スコア18

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

C#

1string[] file= new string[1]();

file[0] のみ使うならこれで。

投稿2022/06/14 04:08

編集2022/06/14 04:08
y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

dodox86

2022/06/14 04:44

誤:new string[1]() 正:new string[1] では?
Zuishin

2022/06/14 04:49

要素を一つしか使わないのに配列を使う意味ありますか?
dodox86

2022/06/14 05:02

配列を使う有効性は質問者さんのもとのコードを尊重して譲ったとしても、文法エラーなご回答はいただけません。「new string[1]()は私が知らない書き方なだけか?」と思って、コメント前に思わずVisualStudio 2022で確認してしまいましたよ。 「error CS0149: メソッド名が必要です」のエラーです。
naoiwa24

2022/06/14 05:26

確認しました。dodox86さんの言う通り()はいりませんでした。
Zuishin

2022/06/14 05:35

親の遺言に逆らってコードを書いたりするから。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問