Click here to Skip to main content
15,887,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I have some problem to ask about String.contains. I try to write code in form load to do Binary Read Text File which inside the text file contain name of some website. but when run the program it has problem appear. I really dont know how to solve it, pls help me if you know the solution

What I have tried:

I try to use String.Contains() like this but when i start to compile the program it show the message error:
System.NullReferenceException: Object reference not set to an instance of an object.
at WindowsApp1.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\USER\source\repos\WindowsApp1\WindowsApp1\Form1.vb:line 20

Imports System.IO
Imports System.Text

Public Class Form1

Dim TextBinaryWrite As BinaryWriter
Dim MyFileName As String = "MyFavorith.txt"
Dim str() As String = {"chat.com", "ABC.com", "stripABC.com"}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Computer.FileSystem.FileExists(MyFileName) Then

Dim Loadline As String
Dim readFile As System.IO.TextReader = New StreamReader(MyFileName)
Dim MName As String
Dim WebType As String = ""
Try
While True
Loadline = readFile.ReadLine()

If Loadline.Contains("stripABC.com") = True Then
WebType = "stripABC.com"
End If

If Loadline Is Nothing Then
Exit While
End If

Dim Row As String() = New String() {MName, Loadline}
'Dim Row As String() = New String() {MName, WebType, "", Loadline}
DataGridView1.Rows.Add(Row)

End While
readFile.Close()
readFile = Nothing
LbStatuse.Text = "State : File is load completed."

Catch ex As Exception
TextBox1.Text = ex.ToString
LbStatuse.Text = "Error Try when load file" & ex.ToString
End Try

Else
'Create new file if no have
Try
TextBinaryWrite = New BinaryWriter(New FileStream(MyFileName, FileMode.Create))
LbStatuse.Text = "Statuse : " & MyFileName & " is created."
TextBinaryWrite.Close()
Catch ex As Exception
LbStatuse.Text = ex.Message + "Cannot create file."
Return
End Try
End If

End Sub


I dont know why can't use this code ??

If Loadline.Contains("stripABC.com") = True Then
WebType = "stripABC.com"
End If
Posted
Updated 14-May-21 23:29pm
v2

Quote:
Why can't use string.contains

The reason is in error message, you need to learn to read carefully.
System.NullReferenceException: Object reference not set to an instance of an object.
at WindowsApp1.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\USER\source\repos\WindowsApp1\WindowsApp1\Form1.vb:line 20

For some reason, Loadline is not a string, and thus, the error.
Lets guess that when ReadLine() fails, it gives a null pointer instead of a string.

Without doing changes in your code, you can use the debugger to see wgat is going on.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Mr.Kim2050 14-May-21 23:11pm    
so for example if you using Binary reader to read text file line by line and you want to search each string contain in text you being read is match with any string like Dim str() As String = {"chat.com", "ABC.com", "stripABC.com"} , how can you do the code ?
Patrice T 14-May-21 23:16pm    
When you read a line, make sure the read was successful.
Mr.Kim2050 14-May-21 23:19pm    
so for example if you using Binary reader to read text file line by line and you want to search each string contain in text you being read is match with any string like Dim str() As String = {"chat.com", "ABC.com", "stripABC.com"} , how can you do the code ?
Mr.Kim2050 14-May-21 23:19pm    
yes it read successful to read line but when i try to take the LoadLine.contains , it has the problem appear
You should test your Loadline string variable first before doing any other operations, like this:
If String.IsNullOrEmpty(Loadline) Then
 
Share this answer
 

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