2014-09-25 11 views
0

이 오류를 해결하는 데 어려움이 있습니다. 이전에 작동 했었지만 어쩌면 코드가 약간 길어서 어딘가에서 실수를 한 것일 수도 있습니다.FileSystem Watcher 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.

public class MyFileWatcher 
{ 
    private TextBox _textBox; 
    private ListBox _listBox; 
    private string _folderDestination; 
    FileSystemWatcher _watcher; 
    private int _interval; 
    //Timespan created when interval is set 
    private TimeSpan _recentTimeSpan; 
    Dictionary<string, DateTime> _lastFileEvent = new Dictionary<string, DateTime>(); 
    DateTime _current; 



    public MyFileWatcher(TextBox textBox, ListBox listBox, string destfolderTextBox ,DateTime current, System.ComponentModel.ISynchronizeInvoke syncObj) 
    { 
     this._textBox = textBox; 
     this._listBox = listBox; 
    this._folderDestination = destfolderTextBox; 
    this._current = current; 

     this._watcher = new FileSystemWatcher(); 
    this._watcher.SynchronizingObject = syncObj; 
     this._watcher.Changed += new FileSystemEventHandler(convertXML); 

     this._watcher.IncludeSubdirectories = false; 
     this._watcher.Path = textBox.Text; 
     this._watcher.EnableRaisingEvents = true; 

     // add any other required initialization of the FileSystemWatcher here. 

    } 

    public void WatchFile(TextBox ctrlTB, ListBox ctrlLB) 
    { 
     // FileSystemWatcher _watcher = new FileSystemWatcher(); 
     //var localTB = ctrlTB as TextBox; 
     //var localLB = ctrlLB as ListBox; 
     _watcher.Path = ctrlTB.Text; 
     _watcher.Path = ctrlTB.Text; 



     _watcher.NotifyFilter = NotifyFilters.LastWrite; 
     _watcher.Filter = "*.xml"; 

     _watcher.Changed += new FileSystemEventHandler(convertXML); 
     // _watcher.Error += new ErrorEventHandler(WatcherError); 
     // _watcher.Changed += (s, e) => convertXML(s,e); 
     // _watcher.Error += new ErrorEventHandler(WatcherError); 

     _watcher.IncludeSubdirectories = false; 
     _watcher.EnableRaisingEvents = true; 


     ctrlLB.Items.Add("Started Monitoring @ " + ctrlTB.Text); 
     ctrlLB.SelectedIndex = ctrlLB.Items.Count - 1; 
    } 

다음과 같은 컨트롤에 의해 시작 트리거 것 : 그것은 일반적으로 잘 작동

private void button12_Click(object sender, EventArgs e) 
    { 
     current = new DateTime(); 

     if ((!Directory.Exists(this.textBox2.Text)) || (!Directory.Exists(this.textBox7.Text))) 
     { 
      //Form2.ActiveForm.Text = "Please select Source Folder"; 
      // popup.Show("Please Select Source Folder"); 
      MessageBox.Show("Please Select Proper Source Folder"); 
      return; 
     } 

     else 
     { 
      textBox2.Enabled = false; 

      button12.Enabled = false; 
      button11.Enabled = true; 
      button2.Enabled = false; 
      button7.Enabled = false; 
      textBox7.Enabled = false; 
      // button4.Enabled = false; 
      // WatchFile(); 
      string destfolder = textBox7.Text + "\\"; 
      destfolder += "test.xml"; 
      MyFileWatcher myWatcher = new MyFileWatcher(textBox2, listBox2, destfolder, current, this); 

      myWatcher.WatchFile(textBox2, listBox2); 
     } 
    } 

여기

는 코드입니다. 그것은 myWatcher처럼 보인다

private void button11_Click(object sender, EventArgs e) 
    { 
     // _watcher.EnableRaisingEvents = false; 
     //  _watcher.Changed -= new FileSystemEventHandler(InitList); 
     // _watcher.Dispose(); 
     //((FileSystemWatcher)sender).Dispose(); 
     listBox2.Items.Add("Stopped Monitoring Directory "); 
     listBox2.SelectedIndex = listBox2.Items.Count - 1; 
     textBox2.Enabled = true; 

     button10.Enabled = true; 
     button2.Enabled = true; 
     button7.Enabled = true; 
     textBox7.Enabled = true; 
     // if (myWatcher != null) 
      myWatcher.RemoveWatcher(); **// here is where the error comes up.** 
    } 

가 null의 다음 컨트롤 정지하려고 때 오류가납니다. 하지만이 null이 시작 컨트롤에 할당되었습니다.

답변

1

당신 클래스 필드를 사용하는 대신 로컬 변수 myWatcher을 선언하고 있습니다 (해당 시점에서 초기화 된 로컬 myWatcher 및 널 남아 클래스 수준에서 myWatcher)입니다 :

button11_Click의 라인 myWatcher.RemoveWatcher();이 예외가 발생 이유를 설명
private void button12_Click(object sender, EventArgs e) 
{ 
    ... 
    MyFileWatcher myWatcher = new MyFileWatcher(...); 
    ... 
} 

.

는 대신 새 로컬 변수를 선언하는 클래스 필드를 사용하는 button12_Click의 코드를 변경해야

private void button12_Click(object sender, EventArgs e) 
{ 
    ... 
    myWatcher = new MyFileWatcher(...); 
    ... 
} 
1

이 코드가 컴파일되면 다른 곳에서 선언 된 myWatcher이 있습니다.

myWatcher 당신은 지역button12_Click에있다 button12_Click 설정 :

MyFileWatcher myWatcher = new MyFileWatcher(textBox2, listBox2, destfolder, current, this); 

이것은 당신이 button11_Click에 액세스하는 다른 (글로벌) myWatcher 널 (null), 잎 : button12_Click에서

myWatcher.RemoveWatcher(); **// here is where the error comes up.** 
+0

올바른, 범인을 얻었다. 감사합니다 백만 – user726720