torg Posted May 29, 2003 Posted May 29, 2003 I`ve been sitting here for a while now, but can`t figure out what I`m doing wrong. I`m hoping that someone out here can help me to solve my problem. I receive the message. that there are noe object reference set to this instance of object. Can anybody tell meg Why? Ive got: Structure individual Dim name1 As String Dim dato As String Dim address As String Dim Phonenumber As String End Structure Dim Person(100) As individual Dim numPosts as integer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim sr As StreamReader = File.OpenText("Personalia.txt") Do While (sr.Peek <> -1) And (numPosts< Person.GetUpperBound(0)) numPosts += 1 Person(numPosts ).name1 = sr.ReadLine Person(numPosts ).dato = sr.ReadLine Person(numPosts ).address = sr.ReadLine Person(numPosts ).Phonenumber = sr.ReadLine Loop .... end sub Function FindName(ByVal name1) As String Dim sr As StreamReader = File.OpenText("Personalia.txt") Dim First, Mid, Last As Integer Dim foundFlag As Boolean First= 1 Last= numPosts Do While (first<= last) And (Not foundFlag) Mid= CInt((first+ last) / 2) Select Case Person(Mid).name1.ToUpper Case name foundFlag = True Case Is > name1 last= first- 1 Case Is < name1 last= first+ 1 End Select Loop If foundFlag Then ListBox1.Items.Add(Person(mid).name1) Else MessageBox.Show("nothing") End If End Function Quote
Administrators PlausiblyDamp Posted May 29, 2003 Administrators Posted May 29, 2003 I notice you are incrementing a variable called antPoster in the While loop but it doesn't appear to be declared anywhere, on a related note you are not incrementing the numPosts variable within the loop and as such you are just going to keep overwriting the first element in the array. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
torg Posted May 29, 2003 Author Posted May 29, 2003 Sorry, that was just a write fault. antPosts is numPosts..As it is in my original code. (had to do some amendments before copying the code to this board) So problem is stilll here... Quote
Administrators PlausiblyDamp Posted May 29, 2003 Administrators Posted May 29, 2003 What is the problem exactly? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
nibsy Posted May 29, 2003 Posted May 29, 2003 Try Dim sr AS StreamReader sr = New StreamReader(New FileStream("Personalia.txt"), FileMode.Open)) I hope this helps :) Quote
torg Posted May 29, 2003 Author Posted May 29, 2003 I receive the following message when trying your proposal. overload resolution failed because no accessible 'New' accepts the number of arguments. I`ve used Dim sr As StreamReader = File.OpenText("Personalia.txt") other places in my code, and have not had any problems with that. so I doubt that`s my problem. If it is there must be another answer to it. Quote
nibsy Posted May 29, 2003 Posted May 29, 2003 Sorry, I lead you astray: Can you try? Dim sr As StreamReader Dim fs As FileStream fs = New New FileStream(("Personalia.txt"), FileMode.Open) sr = New StreamReader(fs) Quote
torg Posted May 31, 2003 Author Posted May 31, 2003 Doesn`t help either. i`m using Option Explicit On Imports System.IO Public Class Form4 in the top of my programming code, så I don`t see that what you suggest would help very much. My problem is.: I`ve got at file "txtPersonalia.txt" where you can get certain information. this informatiin are to be placed into an array on form load where it will be sorted in an ascending order .When I type a text in a textbox, the file will be searched to find a match, and write all information that is relevant for that reslut Structure individual Dim name As String Dim dato As String Dim address As String Dim Phonnumber As String End Structure Dim Person(100) As individual when trying to run I receive the following message Additional information: Object reference not set to an instance of an object. Which drives me crazy Quote
*Experts* mutant Posted May 31, 2003 *Experts* Posted May 31, 2003 What line do you get the error on? Quote
torg Posted May 31, 2003 Author Posted May 31, 2003 at this line: Select Case Person(Mid).name1.ToUpper Quote
Administrators PlausiblyDamp Posted June 1, 2003 Administrators Posted June 1, 2003 What is the value of mid at the time of the error? Also when you get the error what is the value of Person(mid)? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Administrators PlausiblyDamp Posted June 2, 2003 Administrators Posted June 2, 2003 is Person(50).name1 a valid string? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
torg Posted June 2, 2003 Author Posted June 2, 2003 I`m not sure I understand what you`re asking about. The array reads data from a file , as long as ithere are "Data" to read. If there are only 49 posts in the file, then the arrayindex 50 would be empty.As is the case in my example. I`ve only registered 6 Posts/persons in the file. Quote
Administrators PlausiblyDamp Posted June 2, 2003 Administrators Posted June 2, 2003 the problem is probably the fact that the line Mid= CInt((first+ last) / 2) will always equal 50 on the first run through regardless of how many items you've read from the file. You would probably need to have a seperate variable to keep track of the number of valid records read rather than just basing it on the size of the array. If you still have a problem reply and I'll try to knock a sample together. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Administrators PlausiblyDamp Posted June 4, 2003 Administrators Posted June 4, 2003 Just a few other things. I notice you are opening a StreamReader in the first line of the FindName function - did you mean to? Also within the select case statement you have a lne that reads Case Name should that be Case Name1 instead? also you appear to be doing a binary search of the data - am I correct? If so is the data sorted? If so are the blank entries being moved to the start of the array rather than the end? finally are you modifying the numPosts variable anywhere else in the Form_Load? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
torg Posted June 4, 2003 Author Posted June 4, 2003 Yes, that should be Case Name1. Yes, it`s supposted to be a binarysearch of the data And no, the streamreader was not supposted to be in the function. I`ve never taken into concern to move blank fields to the end or start. How do I do that? The posts in the file are already sorted alphabeticalla by name But that`s just while trying to get the code to work. I`m going to use the sort algorithm downbelow As you`ll see downbelow, It`s only in the sort algorithm where I use the variable except from in the FindName function. But it`s not modified there. Sub sort() Dim gap As Integer, doneFlag As Boolean Dim index As Integer, temp As String gap = CInt(numPosts/ 2) Do While gap >= 1 Do doneFlag = True For index = 1 To numPosts- gap If person(index).name1> person(index + gap).name1 Then temp = person(index).name1 person(index).name1= person(index + gap).name1 person(index + gap).name1 = temp doneFlag = False End If Next Loop Until doneFlag = True gap = CInt(gap / 2) Loop End Sub There may be some write faults in the code that may have occured during translation of code. Ignore them. Or assume the syntax is correct. Quote
ccc Posted June 4, 2003 Posted June 4, 2003 I may be stating the obvious, but make sure your string, is actually filled with something before you try calling ToUpper. Dim testStr As String MsgBox(testStr.ToUpper)'Would error Dim testStr As String = "" MsgBox(testStr.ToUpper)'Would not error Dim testStr As String MsgBox(UCase(testStr))'Would not error I stay away from using ToUpper, ToLower, Trim, Length of the string object because of this, and I use functions themselves. :eek: Quote
torg Posted June 5, 2003 Author Posted June 5, 2003 I`ve used If IsNothing(individ(i).navn) Then individ(i).navn = "" in the form_load procedure. Still there is something wrong. It seems as if there are not performed any search at all, atleast no result are shown the search algorithm now looks like this: Function findName(ByVal name2 As String) As Integer Dim first, middle, last As Integer first = 0 last = numOfRecords Do While (first <= last) middle = CInt((first + last) / 2) Select Case UCase(Person(middle).name1) Case name2 Return middle Case Is > name2 last = middle - 1 Case Is < name2 first = middle + 1 End Select Loop Return 0 End Function Quote
Administrators PlausiblyDamp Posted June 5, 2003 Administrators Posted June 5, 2003 I just tried a simple test app using your code and I changed the line Case name2 to Case name2.ToUpper and things worked. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.