클라이언트의 연락처를 관리하는 응용 프로그램을 만들려고합니다. 양식은 매우 간단합니다. 여기에는 패널 (이 패널 안에는 이름, 전자 메일 등이있는 레이블)과 저장된 모든 연락처를 sdf 파일에 표시하는 목록 상자가 있습니다. 나는 하나의 큰 문제가있다. 다른 클래스의 메서드를 통해 목록 상자 내용을 새로 고칩니다. 목록 상자를 공용으로 설정하면 디버거는 프로젝트를 빌드하는 동안 오류를 표시하지 않습니다. 폼이로드되면 try-catch 예외를 통해이 메시지가 표시됩니다. "객체 참조가 객체의 인스턴스로 설정되지 않았습니다." 저는이 프로젝트를 다르게하려고했습니다. 모든 것이 하나의 클래스로 작성되었습니다.C# 오류 : 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.
database.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Contacts
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Database.RefreshListBox();
}
}
}
여기 사람이 무엇이 잘못 알고 있나요
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
namespace Contacts
{
class Database
{
public static void RefreshListBox()
{
SqlCeConnection connection = null;
try
{
var form1 = Form.ActiveForm as Form1;
connection = new SqlCeConnection("Datasource = C:\\Kontakty.sdf");
connection.Open();
SqlCeCommand command = new SqlCeCommand("SELECT * FROM Contacts", connection);
SqlCeDataReader reader = command.ExecuteReader();
while (reader.Read())
{
form1.listBox1.Items.Add(reader.GetString(0) + " " + reader.GetString(1));
}
}
catch(Exception exc)
{
MessageBox.Show(exc.Message);
}
}
}
Form1.cs를 : 여기 코드는?
PS : From1 여전히로드 아마도 아직 보이지 때문에 활성화되지 않기 때문에 kontakty = 접점 (체코는; -)
오류가 나타날 때까지 중단 점을 설정하고 단계별 실행을 시도 했습니까? – ccozad
try 블록의 어느 라인이 예외를 던지고 있습니까? –