概要
c#でフォームアプリを作っています。その中でcsvファイルのデータを配列に読み込む処理があるのですが、その処理をしている部分でNullReferenceExceptionが出てしまいます。おそらくインスタンスの生成がうまくいってないと思うのですが、なぜなのかわからず困っています。c#は勉強している最中で、そこら辺がまだよく理解できてないのでお力を貸していただきたいです。
フォームアプリの仕様
8個のcsvファイル(データは数値、行と列のデータの数は揃っていて8個のファイルすべて同じデータ数、空データなし)をopenFileDialogで開きます。calcボタンが押されるとそれらのデータを一旦配列に読み込み、計算処理をした結果を新しい2つのcsvファイルに書き込んで保存します。
ソースコード
以下該当部分の抜粋です。
c#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.IO; 7using System.Linq; 8using System.Text; 9using System.Threading.Tasks; 10using System.Windows.Forms; 11 12 13//namespaceなど中略 14 15 private void buttonCalc_Click(object sender, EventArgs e) //calcボタンが押されたときに発生するイベント 16 { 17 if (openFileDialog1.FileNames.Length == 8) //ファイルが8つ選択されていたら 18 { 19 StreamReader[] readCSVfile = new StreamReader[8]; //ファイルから読み込んでデータを格納するためのstreamオブジェクト 20 for (int n = 0; n < 8; n++) //ファイル8個開く 21 { 22 readCSVfile[n] = new StreamReader(openFileDialog1.FileNames[n], Encoding.GetEncoding("Shift_JIS")); //データが入っているcsvファイルを開く 23 } 24 string calculated1FilePath = textBoxSaveFolder.Text + "\\calculated1.csv"; //計算結果を保存するcsvファイルのパス 25 string calculated2FilePath = textBoxSaveFolder.Text + "\\calculated2.csv"; //計算結果を保存するcsvファイルのパス 26 StreamWriter calculated1CSV = new StreamWriter(calculated1FilePath); //calculated1のデータを書き込むcsvファイルを生成 27 StreamWriter calculated2CSV = new StreamWriter(calculated2FilePath); //calculated2のデータを書き込むcsvファイルを生成 28 29 30 try 31 { 32 for (int i = 0; readCSVfile[0].EndOfStream == false; i++) //行を走査 33 { 34 string[,] dataPixelLine = new string[8, readCSVfile[0].ReadLine().Split(',').Length]; //データ一行分をカンマで区切ったものを8つ(ファイル8つあるので)格納するためのstring型二次元配列//添字の0次元目はファイル番号、1次元目は列番号 35 36 for (int n = 0; n < 8; n++) //ファイルを8個同時に開いて計算する 37 { 38 for (int s = 0; s < readCSVfile[n].ReadLine().Split(',').Length; s++) //列を走査 39 { 40 dataPixelLine[n, s] = String.Copy(readCSVfile[n].ReadLine().Split(',')[s]); //一行分の文字列をカンマで区切って一列ずつstring型2次元配列に格納 -------ここでNullReferenceExceptionが出る------- 41 } 42 } 43 44 45 46 string[] calculated1String = new string[dataPixelLine.GetLength(0)]; //計算したcalculated1の値を一行分記憶するためのstring型配列 47 string[] calculated2String = new string[dataPixelLine.GetLength(0)]; //計算したcalculated2の値を一行分記憶するためのstring型配列 48 49 //ここに計算処理が入る 50 //計算結果がcalculated1Stringとcalculated2Stringに格納される 51 52 string calculated1Line = string.Join(",", calculated1String); //calculated1String[]の各要素に格納されている文字を、カンマ区切って一つの文字列にする 53 string calculated2Line = string.Join(",", calculated2String); //calculated2String[]の各要素に格納されている文字を、カンマ区切って一つの文字列にする 54 calculated1CSV.WriteLine(calculated1Line); //新しいcsvファイルに一行書き込む 55 calculated2CSV.WriteLine(calculated2Line); //新しいcsvファイルに一行書き込む 56 } 57 } 58 finally 59 { 60 for (int n = 0; n < 8; n++) //ファイルクローズ処理 61 { 62 readCSVfile[n].Close(); 63 } 64 } 65 MessageBox.Show("処理終了"); 66 } 67 else 68 { 69 MessageBox.Show("ファイルを8つ選択してください"); 70 } 71 }
スタックトレース
************** 例外テキスト ************** System.NullReferenceException: オブジェクト参照がオブジェクト インスタンスに設定されていません。 場所 calc_csv.Form1.buttonCalc_Click(Object sender, EventArgs e) 場所 C:¥Users¥myPC¥Visual Studio 2017¥Projects¥C#¥Windows Form Apprications¥calc csv¥calc csv¥Form1.cs:行 77 場所 System.Windows.Forms.Control.OnClick(EventArgs e) 場所 System.Windows.Forms.Button.OnClick(EventArgs e) 場所 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 場所 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 場所 System.Windows.Forms.Control.WndProc(Message& m) 場所 System.Windows.Forms.ButtonBase.WndProc(Message& m) 場所 System.Windows.Forms.Button.WndProc(Message& m) 場所 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 場所 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 場所 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
よろしくお願いします。
バージョンなど
Windows10 Pro 64bit
Visual Studio 2017 community
.NET Framework 4.7
C# 7.0
回答3件
あなたの回答
tips
プレビュー