0

In this program (VB, ASP.NET 2010) I create three fields: accno, name and balance, and the following buttons: create, destroy, set and get. But while clicking on set or get method it gives the following exception: object reference not set to an instance of an object

Default.aspx.vb

Partial Class _Default
    Inherits System.Web.UI.Page

    Dim obj As account 'declaring the obj of class account

    Protected Sub btn_create_Click(sender As Object, e As System.EventArgs) Handles btn_create.Click
        obj = New account 'initializing the object obj on class accounts
    End Sub    

    Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click
        'sending the values from textboxes to accounts class through method setdata
        Try
            obj.setdata(CInt(txt_accno.Text), (txt_name.Text), CInt(txt_bal.Text))
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click
        'calling the method getdata to view the output
        Try
            obj.getdata()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Protected Sub btn_destroy_Click(sender As Object, e As System.EventArgs) Handles btn_destroy.Click
        'calling the constructor
        obj = Nothing
    End Sub
End Class

Account.vb

Imports Microsoft.VisualBasic

Public Class account

    Private accno As Integer
    Private acc_name As String
    Private bal As Integer

    'constructor
    Public Sub New()
        MsgBox("object created")
    End Sub

    'public method to populate above three private variable

    Public Sub setdata(ByVal a As Integer, ByVal b As String, ByVal c As Integer)
        Me.accno = a
        Me.acc_name = b
        Me.bal = c
    End Sub

    Public Sub getdata()
        MsgBox(Me.accno.ToString + vbNewLine + Me.acc_name + vbNewLine + Me.bal.ToString)
    End Sub

    'destructor
    Protected Overrides Sub finalize()
        MsgBox("object destroyed")
    End Sub

End Class
Alex
  • 23,004
  • 4
  • 39
  • 73
firefly
  • 419
  • 1
  • 8
  • 18
  • 2
    Why do you shout? Your keyboard seems to be ok. – Tim Schmelter Dec 09 '13 at 09:03
  • I hope all of those `MsgBox`s are only for temporary debugging - you are aware that they only work when running using the dev servers, and even when they do work, then run on the server, not (necessarily) the same machine the web page is being displayed on. – Damien_The_Unbeliever Dec 09 '13 at 09:05
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Dec 09 '13 at 09:05
  • 1
    I fixed the title, the words, the casing, and the formatting to make the question more readable – Alex Dec 09 '13 at 09:10

2 Answers2

0

You have use New Keyword for Initializing Objects...

Dim obj As NEW account

Default.aspx.vb Partial Class _Default Inherits System.Web.UI.Page

'declaring the obj of class account

Dim obj As NEW account    


Protected Sub btn_create_Click(sender As Object, e As System.EventArgs) Handles btn_create.Click

    'initializing the object obj on class accounts
    obj = New account



End Sub


Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click

    'sending the values from textboxes to accounts class through method setdata

    Try
        obj.setdata(CInt(txt_accno.Text), (txt_name.Text), CInt(txt_bal.Text))

    Catch ex As Exception

        MsgBox(ex.Message)
    End Try


End Sub

Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click

    'calling the method getdata to view the output

    Try

        obj.getdata()

    Catch ex As Exception

        MsgBox(ex.Message)
    End Try

End Sub

Protected Sub btn_destroy_Click(sender As Object, e As System.EventArgs) Handles btn_destroy.Click

    'calling the constructor

    obj = Nothing

End Sub
End Class

Please mark it as answer if it's working.

Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
0

use below code. You need to initialize in page load method and need to check is postback property.

 Default.aspx.vb Partial Class _Default Inherits System.Web.UI.Page

        'declaring the obj of class account

        Dim obj As account 

        Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
            if not ispostback then
                 obj = New account
            end
       End Sub

        Protected Sub btn_create_Click(sender As Object, e As System.EventArgs) Handles btn_create.Click

            'initializing the object obj on class accounts
            obj = New account



        End Sub


        Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click

            'sending the values from textboxes to accounts class through method setdata

            Try
                obj.setdata(CInt(txt_accno.Text), (txt_name.Text), CInt(txt_bal.Text))

            Catch ex As Exception

                MsgBox(ex.Message)
            End Try


        End Sub

        Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click

            'calling the method getdata to view the output

            Try

                obj.getdata()

            Catch ex As Exception

                MsgBox(ex.Message)
            End Try

        End Sub

        Protected Sub btn_destroy_Click(sender As Object, e As System.EventArgs) Handles btn_destroy.Click

            'calling the constructor

            obj = Nothing

        End Sub
        End Class
Indranil.Bharambe
  • 1,462
  • 3
  • 14
  • 25