Buenas tarde.
Estoy haciendo un código para que me lea un archivo, lo divida en lienas y a cada linea modificar el texto.
Hasta que usaba MsgBox para ver los resultados, todo esta perfecto. Pero al momento de querer añadirlos a un list view me manda el siguiente error:
Object reference not set to an instance of an object.
Código Error:
Ver originalSystem.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=UNICA - MAYA
StackTrace:
at UNICA___MAYA.panel.Proceso_Tick(Object sender, EventArgs e) in C:\Users\AHD\Documents\Visual Studio 2010\Projects\UNICA - MAYA\UNICA - MAYA\panel.vb:line 190
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at UNICA___MAYA.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Este es mi codigo:
Código vb:
Ver original'Funcion split con la forma en PHP, para no confundirme
Public Function explode(ByRef str As String, ByVal content As String)
Return Split(content, str)
End Function
'Funcion para poner el contenido de un archivo en un string
Public Function get_file_contents(ByVal ruta As String)
Try
Dim SPath As String = ruta
Dim sContent As String = vbNullString
With My.Computer.FileSystem
' verifica si existe el path
If .FileExists(SPath) Then
' lee todo el contenido
sContent = .ReadAllText(SPath)
Return sContent.ToString
Else
Return "False"
End If
End With
' errores
Catch ex As Exception
Return ex.Message.ToString
End Try
End Function
'Funcion para comparar dos archivos y regresar la diferencia
Public Function notInMirror(ByVal original As String, ByVal mirror As String)
'Comparar el arhcivo original y el mirror y regresar solo el contenido de original que no está en mirror
Dim resultado As String = Replace(original, mirror, "")
Return resultado
End Function
Private Sub panel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fileOriginal As String = get_file_contents(filePath)
'Comparar el archivo original con el mirror
Dim diferencia As String = notInMirror(fileOriginal, get_file_contents(mirrorPath))
If diferencia = "" Then
Else
Dim lines As Array = explode(vbCrLf, diferencia)
For Each line In lines
Dim campos As String = Replace(line, "|", "','")
'En esta siguiente linea marca el error
Dim consultaCampos As String = campos.Substring(0, campos.Length - 3)
Dim statement As String = "VALORES ENCONTRADOS ('" & consultaCampos & "')"
'Cuando uso esto, marca el error
'Las columnas son Fecha y Valores
Dim item As ListViewItem
item = New ListViewItem(Date.Now.ToString)
item.SubItems.Add(statement)
lvActions.Items.Add(item)
'Cuando uso un mensaje todo esta bien:
'MsgBox(statement)
Next
End If
End Sub