.csv file splitting
.csv file splitting
.csv file splitting
.csv file splitting
.csv file splitting
.csv file splitting .csv file splitting .csv file splitting .csv file splitting .csv file splitting .csv file splitting .csv file splitting .csv file splitting
.csv file splitting .csv file splitting
.csv file splitting
Go Back  Xtreme Visual Basic Talk > > > .csv file splitting


Reply
 
Thread Tools Display Modes
  #1  
Old 02-02-2009, 08:57 PM
Killerklark Killerklark is offline
Newcomer
 
Join Date: Feb 2009
Posts: 9
Unhappy .csv file splitting


Hi All!
I am new here and new to VB 2008. I have a program I have been working on for a about a week now... yes its homework... the program is 90% complete but I cant get past an error.

I get this error "Object reference not set to an instance of an object." when I run the program and click the button. It happens towards the bottom on the line.split portion of the code.



Any pointers would be very helpful.

V/r
KK
Attached Files
File Type: txt myproblem.txt (1.8 KB, 14 views)
Reply With Quote
  #2  
Old 02-02-2009, 09:09 PM
Tom Moran Tom Moran is offline
Freshman
 
Join Date: Jul 2008
Posts: 37
Default

This forum is for VB6, not 2008
Reply With Quote
  #3  
Old 02-02-2009, 09:19 PM
Burningslash12's Avatar
Burningslash12 Burningslash12 is offline
Junior Contributor
 
Join Date: Sep 2008
Location: Singapore
Posts: 303
Default

Hi! Perhaps the "New" keyword is missing.

Code:
Dim sr As StreamReader = New File.OpenText("csvSTOCKS.TXT")
I usually do it this way.

Code:
Dim sr As StreamReader = New StreamReader("csvSTOCKS.TXT")
Hope this helps.
__________________
Burningslash12
"We may rise and fall, but in the end, we'll meet our fate together..."

Don't Click Me!
Reply With Quote
  #4  
Old 02-02-2009, 09:23 PM
Killerklark Killerklark is offline
Newcomer
 
Join Date: Feb 2009
Posts: 9
Default

Tom sorry, I couldn't find a msg board for 2008.

Burningslash12, I tried it and no change, but one of the options in the error list does tell me to add the "New" statement. I have tried it everywhere. I tried where you said as well. It didn't work.
Reply With Quote
  #5  
Old 02-02-2009, 09:53 PM
Burningslash12's Avatar
Burningslash12 Burningslash12 is offline
Junior Contributor
 
Join Date: Sep 2008
Location: Singapore
Posts: 303
Default

Could you be more specific with the error you are seeing.

This code works perfectly on my PC, please try.
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sr As StreamReader = New StreamReader("C:\Test.csv") 'csv file contains "Test1,Test2"
        Dim line(2) As String

        line = sr.ReadLine.Split(",")

        MessageBox.Show(line(0)) 'shows Test1
        MessageBox.Show(line(1)) 'shows Test2
    End Sub
__________________
Burningslash12
"We may rise and fall, but in the end, we'll meet our fate together..."

Don't Click Me!
Reply With Quote
  #6  
Old 02-02-2009, 10:22 PM
Killerklark Killerklark is offline
Newcomer
 
Join Date: Feb 2009
Posts: 9
Default .csv file splitting

Burningslash12,
Here is a pic of the error.

I tried your code and it worked also...(when I take all of my code out).

but not when I put the New in as mentioned on your first post.

Thanks for your replies.
Attached Images
File Type: jpg error.JPG (152.1 KB, 9 views)
Reply With Quote
  #7  
Old 02-02-2009, 10:25 PM
Killerklark Killerklark is offline
Newcomer
 
Join Date: Feb 2009
Posts: 9
Default

Just so you know where I am putting it ... here it is.
Code:
Dim sr As StreamReader = New StreamReader("csvSTOCKS.TXT") lstoutput.Items.Clear() Dim line(4) As String Dim lines(20) As Stock Dim Cost, PL, CP As New Integer Cost = line(1) * line(3) CP = (line(4) * line(1)) - Cost PL = line(4) - Cost Dim fmtstr As String = "{0,-14} {1,16} {2,14} {3,14}" lstoutput.Items.Add(String.Format(fmtstr, "", "", "Current", "Profit")) lstoutput.Items.Add(String.Format(fmtstr, "Stock", "Cost", "Value", "(or Loss)")) lstoutput.Items.Add(String.Format(fmtstr, "==============", "================", "==============", "==============")) For i As Integer = 0 To 20 line = sr.ReadLine.Split(",") lines(i).name = line(0) lines(i).numshares = line(1) lines(i).entered = line(2) lines(i).PP = CDbl(line(3)) lines(i).CP = CDbl(line(4)) lstoutput.Items.Add(String.Format(fmtstr, lines(i).name, Cost, CP, PL)) Next End Sub

Last edited by passel; 02-03-2009 at 08:36 AM.
Reply With Quote
  #8  
Old 02-02-2009, 10:40 PM
Killerklark Killerklark is offline
Newcomer
 
Join Date: Feb 2009
Posts: 9
Default .csv file splitting

Ok,

I moved the sr.Readline down now to here:
Code:
For i As Integer = 0 To 20 Dim sr As StreamReader = New StreamReader("csvSTOCKS.TXT") line = sr.ReadLine.Split(",") lines(i).name = line(0) lines(i).numshares = line(1) lines(i).entered = line(2) lines(i).PP = CDbl(line(3)) lines(i).CP = CDbl(line(4)) lstoutput.Items.Add(String.Format(fmtstr, lines(i).name, Cost, CP, PL)) Next End Sub
Now I get output but its only the first line's stockname and zero's across the board. I am going to check my math. I am still not sure how to make it read all lines, maybe a loop?
Attached Images
File Type: jpg error.JPG (131.0 KB, 4 views)

Last edited by passel; 02-03-2009 at 08:37 AM.
Reply With Quote
  #9  
Old 02-02-2009, 11:14 PM
Burningslash12's Avatar
Burningslash12 Burningslash12 is offline
Junior Contributor
 
Join Date: Sep 2008
Location: Singapore
Posts: 303
Default

Could you try to put the whole Path & filename of the file you want to load. Instead of just the filename:
Code:
Dim sr As StreamReader = New StreamReader("C:\ ..\..\csvSTOCKS.TXT")
See if it helps, because i cannot replicate your problem.
__________________
Burningslash12
"We may rise and fall, but in the end, we'll meet our fate together..."

Don't Click Me!
Reply With Quote
  #10  
Old 02-02-2009, 11:24 PM
Killerklark Killerklark is offline
Newcomer
 
Join Date: Feb 2009
Posts: 9
Default

Burningslash12,
We've got it working with output and all. Now Its down to the output its got duplicates. I think it has to do with the :
For i As Integer = 0 To 4
What I want to do is take the "= 0 To 4" out but it gives me another syntax error. I think its looping too many times because I have the same thing in the list eg:
1
2
3
4
1
2
3
4

Except it does it 4 times instead of 2.

V/r
KK
Reply With Quote
  #11  
Old 02-02-2009, 11:29 PM
Killerklark Killerklark is offline
Newcomer
 
Join Date: Feb 2009
Posts: 9
Default

Here is a copy of the new code:
Code:
For i As Integer = 0 To 4 Dim sr As StreamReader = New StreamReader("csvSTOCKS.TXT") Dim doneflag As Boolean doneflag = False Do While (Not doneflag) If (sr.Peek <> -1) Then line = sr.ReadLine.Split(",") lines(i).name = line(0) lines(i).numshares = CDbl(line(1)) lines(i).entered = line(2) lines(i).PP = CDbl(line(3)) lines(i).CP = CDbl(line(4)) Cost = line(1) * line(3) CP = (line(4) * line(1)) - Cost PL = line(4) - Cost lstoutput.Items.Add(String.Format(fmtstr, lines(i).name, Cost, CP, PL)) Else doneflag = True End If Loop

Last edited by passel; 02-03-2009 at 08:35 AM. Reason: Adding [vb][/vb] tags
Reply With Quote
  #12  
Old 02-02-2009, 11:45 PM
Killerklark Killerklark is offline
Newcomer
 
Join Date: Feb 2009
Posts: 9
Default

Burningslash12,
I am going to crash for the night... thx for the help. I will check back tomorrow. I got to hit the hay... work In 5hrs.
V/r
KK
Reply With Quote
  #13  
Old 02-03-2009, 12:02 AM
loquin's Avatar
loquin.csv file splitting loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,400
Default

VB2008 is the latest version of dotNet...
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown
Reply With Quote
  #14  
Old 02-03-2009, 08:32 AM
passel's Avatar
passel.csv file splitting passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

Yes, we've moved the .Net forums to the top of the list, and the 11 year old language forums further down. I assume it has slowed down the misposting, but not as much as you would think.

Note the section header....
"Visual Basic .NET (2002/2003/2005/2008, including Express editions)"

I know it's a lot of numbers to read through, but 2008 is there if you persevere.

Also, put your code between [vb][/vb] tags in your post to make your code more readable (assuming you've used indenting).
Attached Images
File Type: jpg vbnetforum.JPG (89.0 KB, 8 views)
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 02-03-2009 at 08:44 AM.
Reply With Quote
  #15  
Old 02-03-2009, 05:15 PM
Wolfy87 Wolfy87 is offline
Freshman
 
Join Date: Dec 2008
Location: England
Posts: 30
Smile

surely mate you just do:

Dim sr As New StreamReader(mydoc.txt)
sr.Close()

And remember to use Imports.System.IO

Hope this helps, Wolfy87.
Reply With Quote
  #16  
Old 02-03-2009, 07:15 PM
Killerklark Killerklark is offline
Newcomer
 
Join Date: Feb 2009
Posts: 9
Default

Hi Everyone,
Thanks for all the help. I got home from work tonight and got cracking on it. I had a few problems. First off I misplaced some of my earlier script (threw off my math). Then, I definately needed the "New" in the code as well. Then I was checking out an earlier--more simple--program I created and noticed some easy script for the loop. NOW IT ALL WORKS!!! YAY!

THANK YOU TO Wolfy87 and most of all Burningslash12!!

For anyone else interested in the code:

Code:
Private Sub btnshow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnshow.Click lstoutput.Items.Clear() Dim line(4) As String Dim lines(4) As Stock Dim Cost, PL, CV As Double Dim fmtstr As String = "{0,-14} {1,16} {2,14} {3,14}" lstoutput.Items.Add(String.Format(fmtstr, "", "", "Current", "Profit")) lstoutput.Items.Add(String.Format(fmtstr, "Stock", "Cost", "Value", "(or Loss)")) lstoutput.Items.Add(String.Format(fmtstr, "==============", "================", "==============", "==============")) Dim i As Integer = 0 Dim sr As StreamReader = New StreamReader("csvSTOCKS.TXT") Do While sr.Peek <> -1 line = sr.ReadLine.Split(",") lines(i).name = line(0) lines(i).numshares = CDbl(line(1)) lines(i).entered = line(2) lines(i).PP = CDbl(line(3)) lines(i).CP = CDbl(line(4)) Cost = line(1) * line(3) CV = (line(1)) * (line(4)) PL = CV - Cost lstoutput.Items.Add(String.Format(fmtstr, lines(i).name, Cost, CV, PL)) Loop sr.Close() End Sub

Thanks again!!!
V/r
KK
Reply With Quote
  #17  
Old 02-03-2009, 07:39 PM
Burningslash12's Avatar
Burningslash12 Burningslash12 is offline
Junior Contributor
 
Join Date: Sep 2008
Location: Singapore
Posts: 303
Default

Quote:
THANK YOU TO Wolfy87 and most of all Burningslash12!!
You're welcome.
__________________
Burningslash12
"We may rise and fall, but in the end, we'll meet our fate together..."

Don't Click Me!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
.csv file splitting
.csv file splitting
.csv file splitting .csv file splitting
.csv file splitting
.csv file splitting
.csv file splitting .csv file splitting .csv file splitting .csv file splitting .csv file splitting .csv file splitting .csv file splitting
.csv file splitting
.csv file splitting
 
.csv file splitting
.csv file splitting
 
-->