Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Program to Read RFID Tag through RFID Reader Module but i getting error """ Failed beacause Object reference not set to an instance of an object.:::::::::::::::

My Source code in below............

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Threading
Imports System.IO.Ports

'Declare a delegate before starting of the class.
'Initialize the delegate in Form1() or the constructor of your form.
' Set the delegate to a function that writes a global value to your listBox.

'Public Delegate Sub del(ByVal data As String)
Partial Public Class Form4
Inherits Form
' Public Sub New()
' InitializeComponent()
'End Sub
Private Sub Form4_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = False

'Load all the available ports at the start of Form Load.
Dim ports() As String = System.IO.Ports.SerialPort.GetPortNames()
For i As Integer = 0 To ports.Length - 1
ComboBox1.Items.Add(ports(i))
Next i


End Sub
Private Sub btnGetACNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetACNo.Click
'Choose button to connect to the port.
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'choose to connect to the port.
If Timer1.Enabled = True Then
' Call cmdOpen()
MessageBox.Show("Success to Connect")

Else
Exit Sub
End If
End Sub
'Private serialPort1 As SerialPort
Private Sub cmdOpen()
' Try Read to connect to the port.
Try
'SerialPort1 = New SerialPort()
SerialPort1.PortName = ComboBox1.SelectedItem.ToString()
' SerialPort1.PortName = "COM1"
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
'serialPort.Handshake = Handshake.None;
SerialPort1.Open()
' Thread.Sleep(500);
'string data1 = serialPort.ReadLine();
SerialPort1.ReadTimeout = 2000
' AddHandler serialPort.DataReceived, AddressOf sp_DataReceived
'SerialPort1.BaudRate = 9600
' SerialPort1.Open()
AddHandler SerialPort1.DataReceived, AddressOf serialPort1_DataReceived
MessageBox.Show("Success to Connect")
'sp_DataReceived(this , null);
Timer1.Enabled = False
SerialPort1.Close()

Catch ex As Exception
MessageBox.Show("Failed because" & ex.Message)
'Exit Sub
End Try

End Sub
'Declare a delegate before starting of the class.
'Initialize the delegate in Form1() or the constructor of your form.
' Set the delegate to a function that writes a global value to your listBox.
Private Delegate Sub del(ByVal data As String)
Private MyDlg As del
Public Sub New()
InitializeComponent()
MyDlg = New del(AddressOf Display)
End Sub

' So this delegate will be called from SerialPorts data listener and will assign the data to ListBox via delegate.
'We will display the received Tag via Message Box and Also will put it in ListBox.
Private Sub Display(ByVal s As String)
ListBox1.Items.Add(s)

End Sub
Private data As String = String.Empty
Private Delegate Sub SetTextDeleg(ByVal text As String)

Private Sub serialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
Me.BeginInvoke(New SetTextDeleg(AddressOf serialPort1_DataReceived), New Object() {data})
Thread.Sleep(500)
data = SerialPort1.ReadLine()
'data = serialPort.ReadExisting();

data = SerialPort1.ReadExisting()
Me.BeginInvoke(MyDlg, data)

' My Problem is showing that s is not a member of Data

MessageBox.Show(data)

End Sub
Private Sub serialPort1_DataReceived(ByVal data As String)
txtAccessCardNo.Text = data.Trim()

End Sub


End Class
Posted
Updated 17-Oct-12 20:48pm
v3
Comments
Sergey Alexandrovich Kryukov 17-Oct-12 23:51pm    
Please don't re-post. Use only one of your pages with one of the two last questions.
--SA
Bernhard Hiller 18-Oct-12 3:17am    
When you post code snippets, show only the reqwuired amount of code. Lines which were commented out, are not required. Use proper formatting.
Next, learn how to use the Debugger of Visual Studio! That will be the most important lesson you've ever come across.
[no name] 18-Oct-12 9:15am    
Find out which line has the null object and then make it not null.
Hetal Jariwala 24-Oct-12 7:00am    
use reference guide with the readers....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900