Jump to content

System.NullReferenceException: 'A referência de objecto não foi definida como uma instância de um objecto.'


Recommended Posts

Posted (edited)

Boa tarde Pessoal

tenho um problema, criei uma base de dados em access para abrir num programa que estou criando, mas a tabela que criei não contem registos e o programa não abre

e dá erro....

o programa só abre se tiver registos dentro da base de dados....

como é que eu faço para abrir o programa com a base de dados sem registos, para depois dentro do programa adicionar....

Preciso mesmo de ajuda pessoal

Atenciosamente

Ghost

O erro que aparece no visual studio ao abrir o programa é o seguinte:

System.NullReferenceException: 'A referência de objecto não foi definida como uma instância de um objecto.'

System.Windows.Forms.DataGridView.CurrentRow.get retornou Nothing.

Edited by Ghost

Diogo Fernandes

Posted

Boa tarde Ghost,

se não tens linhas, então o CurrentRow não vai ter conteúdo (é Null), dai dar erro ao usares o get.

Não mostraste o código que tens, mas faz uma verificação se tens linhas antes de fazer alguma coisa. Por exemplo com DataGridView.Rows.Count

 

Espero que ajude,
nelsonr

Posted

Boas Nelson

o código onde dá o erro é o seguinte:

    Private Sub MostraRegistos()
        RegistoStripLabel.Text = "Reclamação nº:" & RegistoAtual + 1

        txtReferencia.Text = dgvBaterias.CurrentRow.Cells("Referencia").Value.ToString
        txtCliente.Text = dgvBaterias.CurrentRow.Cells("Cliente").Value.ToString
        txtNCliente.Text = dgvBaterias.CurrentRow.Cells("NCliente").Value.ToString
        txtFTCliente.Text = dgvBaterias.CurrentRow.Cells("FCliente").Value.ToString
        dtpDFatura.Text = dgvBaterias.CurrentRow.Cells("DFatura").Value.ToString
        txtNDevolucao.Text = dgvBaterias.CurrentRow.Cells("NDevolucao").Value.ToString
        txtNCArmazem.Text = dgvBaterias.CurrentRow.Cells("NCArmazem").Value.ToString
        txtNCCliente.Text = dgvBaterias.CurrentRow.Cells("NCCliente").Value.ToString
        txtMEntrada.Text = dgvBaterias.CurrentRow.Cells("MEntrada").Value.ToString
        cboDesignacao.Text = dgvBaterias.CurrentRow.Cells("Designacao").Value.ToString
        cboObservacoes.Text = dgvBaterias.CurrentRow.Cells("Observacoes").Value.ToString
    End Sub


    Private Sub PreencheDataSet()
        dsBaterias.Clear()
        Dim da = New OleDb.OleDbDataAdapter("SELECT * from Baterias", cnADONETConexao)
        da.Fill(dsBaterias, "Baterias")
        RegistoAtual = 0
        TotalRegistos = dsBaterias.Tables("Baterias").Rows.Count - 1

    End Sub

Diogo Fernandes

Posted

Bom dia Ghost,

não indicas a linha de erro, mas de acordo com o outro tópico que criaste, será na segunda do MostraRegistos.

Tal como tinha indicado na mensagem anterior, se não tens registos, o CurrentRow vai estar a NULL, o que provoca o erro quando tenta aceder às células.

Adiciona uma verificação se a grelha tem linhas antes de fazeres aquela parte do código. Algo assim (não testado):

Private Sub MostraRegistos()
	RegistoStripLabel.Text = "Reclamação nº:" & RegistoAtual + 1

	if DataGridView.Rows.Count=0 then return

	txtReferencia.Text = dgvBaterias.CurrentRow.Cells("Referencia").Value.ToString
	txtCliente.Text = dgvBaterias.CurrentRow.Cells("Cliente").Value.ToString
	txtNCliente.Text = dgvBaterias.CurrentRow.Cells("NCliente").Value.ToString
	txtFTCliente.Text = dgvBaterias.CurrentRow.Cells("FCliente").Value.ToString
	dtpDFatura.Text = dgvBaterias.CurrentRow.Cells("DFatura").Value.ToString
	txtNDevolucao.Text = dgvBaterias.CurrentRow.Cells("NDevolucao").Value.ToString
	txtNCArmazem.Text = dgvBaterias.CurrentRow.Cells("NCArmazem").Value.ToString
	txtNCCliente.Text = dgvBaterias.CurrentRow.Cells("NCCliente").Value.ToString
	txtMEntrada.Text = dgvBaterias.CurrentRow.Cells("MEntrada").Value.ToString
	cboDesignacao.Text = dgvBaterias.CurrentRow.Cells("Designacao").Value.ToString
	cboObservacoes.Text = dgvBaterias.CurrentRow.Cells("Observacoes").Value.ToString
End Sub 

Espero que ajude,
nelsonr

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.