2012-04-12 2 views
0

가 발생합니다은 (listApplications는 ListView 컨트롤입니다)이 코드 NullReferenceException이

private void ShowApplicationPropertiesForm() { 
     String FullPath = String.Empty; 
     String Title = String.Empty; 
     String Description = String.Empty; 
     Boolean Legacy = false; 
     Boolean Production = false; 
     Boolean Beta = false; 
     MyCustomListViewItemDescendant lvi = (MyCustomListViewItemDescendant)listApplications.SelectedItems[0]; 
     FullPath = lvi.ExePath; 
     Title = lvi.Text; 
     Description = lvi.ToolTipText; 

     ApplicationProperties ap = new ApplicationProperties(
      FullPath, 
      Title, 
      Description, 
      Legacy, 
      Production, 
      Beta); 
     ap.Show(); 
    } 


//overloaded form constructor 
public ApplicationProperties(String AFullPath, String ATitle, String ADescription, Boolean ALegacy, Boolean AProduction, Boolean ABeta) { 
      this.Text = String.Format("{0} Properties", ATitle); 
      textBoxFullPath.Text = AFullPath; 
      textBoxTitle.Text = ATitle; 
      textBoxDescription.Text = ADescription; 
      checkBoxLegacy.Checked = ALegacy; 
      checkBoxProduction.Checked = AProduction; 
      checkBoxBeta.Checked = ABeta; 
     } 

... 내가지고있어, "System.NullReferenceException는 처리되지 않은했다 메시지 = 개체 참조가 개체의 인스턴스로 설정되지 않았습니다. "

그것을 통해 스테핑 폭탄은 라인 :

textBoxFullPath.Text = AFullPath; 

textBoxFullPath 폼에 텍스트 상자이다; "Q : \ 무엇 \ 이세요 곧 \ 마 \ BabyBlue.exe \"업데이트

: 부분적으로 해결

AFullPath는 종류의 유효한 값이 있습니다.

이전의 "조기 할당"문제였습니다. 할당자를 Load() 이벤트로 이동하면 더 이상 폭탄을 투하하지 않습니다 (아래 코드).

그러나 이제는 런타임시 폼에 아무 것도 표시되지 않습니다 ... ???!?

public partial class ApplicationProperties : Form { 
     String _fullPath = String.Empty; 
     String _title = String.Empty; 
     String _description = String.Empty; 
     Boolean legacy = false; 
     Boolean production = false; 
     Boolean beta = false; 

     public ApplicationProperties() { 
      InitializeComponent(); 
     } 

     public ApplicationProperties(String AFullPath, String ATitle, String ADescription, Boolean ALegacy, Boolean AProduction, Boolean ABeta) { 
      _fullPath = AFullPath; 
      _title = ATitle; 
      _description = ADescription; 
      legacy = ALegacy; 
      production = AProduction; 
      beta = ABeta; 
      this.CenterToScreen(); 
     } 

     private void ApplicationProperties_Load(object sender, EventArgs e) { 
      //this.Text = String.Format("{0} Properties", _title);   
      Text = String.Format("{0} Properties", _title); 
      textBoxFullPath.Text = _fullPath; 
      textBoxTitle.Text = _title; 
      textBoxDescription.Text = _description; 
      checkBoxLegacy.Checked = legacy; 
      checkBoxProduction.Checked = production; 
      checkBoxBeta.Checked = beta; 
     } 

다시 업데이트 :

가 추가 "를 InitializeComponent를();" 오버로드 된 생성자에게 트릭을했습니다 - 감사합니다, SW!

+0

어디에서'textBoxFullPath'를 초기화합니까? –

+3

양식이 Visual Studio로 만들어 졌습니까? 그렇다면 InitializeComponent()에 대한 호출을 추가하여 TextBox 및 기타 UI 요소를 만들어야합니다. –

+0

@JJ : String.Empty를 의미합니까? 습관. 아마도 도움이되지는 않지만 어떻게 상처받을 수 있는지 보지 못합니다. –

답변

1

디자이너는 항상 매개 변수가없는 생성자를 호출하므로 WinForm Forms에 대한 자체 생성자를 만들지 않으려 고합니다.

아래에서 조정할 제안 사항을 참조하십시오. 원하는 위치로 이동하지 못하면 알려 주시면 업데이트하겠습니다.

public ApplicationProperties(String AFullPath, String ATitle, String ADescription, Boolean ALegacy, Boolean AProduction, Boolean ABeta) 
     : this() // --> Call the parameterless constructor before executing this code 
    { 
     _fullPath = AFullPath; 
     _title = ATitle; 
     _description = ADescription; 
     legacy = ALegacy; 
     production = AProduction; 
     beta = ABeta; 
     this.CenterToScreen(); // --> Maybe move this to the Shown event (not sure if you need to) 
    } 
+0

사실 매개 변수없는 생성자를 호출하는 ": this()"를 추가하면 내 할당이 나타나지 않습니다 (컨트롤이 표시되지만 텍스트가 없습니다). 그것은 그것없이 작동합니다. –

+0

실제로 루트 문제를 해결하는 것은 InitializeComponent()를 호출하는 것 뿐이라고 생각합니다. 두 번 반복하지 않아도되도록 노력하고 있지만 괜찮습니다. –

2

저는 C#을 처음 사용 했으므로 잘못된 것이라면 용서해주십시오.

On MSDN, 오류는 잘못된 참조로 인한 것입니다. 따라서 귀하의 경우에는 textBoxFullPath이 없을 수도 있습니다 (철자를 확인해야합니다).

하지만 여기서는 파일 스트림에 관한 것이므로 경로를 사용하고 있으므로 this이 도움이 될 수 있습니다. (첫 번째 대답을 확인하십시오).

희망이 있습니다.

관련 문제