Click here to Skip to main content
16,017,608 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Extract Day(s),Month(s) from a Year using VB.NET program Pin
Member 196874827-Jun-05 0:03
Member 196874827-Jun-05 0:03 
Generalpushing a command button by program code Pin
TheSolid26-Jun-05 11:50
TheSolid26-Jun-05 11:50 
GeneralRe: pushing a command button by program code Pin
Dr_Lomax26-Jun-05 13:16
Dr_Lomax26-Jun-05 13:16 
GeneralRe: pushing a command button by program code Pin
Christian Graus26-Jun-05 16:10
protectorChristian Graus26-Jun-05 16:10 
GeneralPresentations to Video Converter Pin
Murtuza Husain Miyan Patel26-Jun-05 3:59
professionalMurtuza Husain Miyan Patel26-Jun-05 3:59 
GeneralRe: Presentations to Video Converter Pin
Dave Kreskowiak26-Jun-05 8:54
mveDave Kreskowiak26-Jun-05 8:54 
General.NET & Host Integration Server Pin
Ratnamsub25-Jun-05 22:02
Ratnamsub25-Jun-05 22:02 
GeneralNeed Serious help or i'll go crazy!!! Pin
gui3916boy25-Jun-05 17:37
gui3916boy25-Jun-05 17:37 
I have been working on a problem but cant seem to find a solution. We have to read a textfile- extract the data plot them on a graph, find the centroids and so on. The file looks like this:
2.1 3.5 +1
10.2 8.3 -1
3.5 5.5 +1
9.3 7.3 -1

The first column is x, the second y, and the third tells us which class it belongs to.

Everytime I run my program it says

" An unhandled exception of type 'System.NullReferenceException' occurred in GUI-Project.exe

Additional information: Object reference not set to an instance of an object." I think the problem occurs when trying to read the data array. Could someone please show me a way??? Thanks.

I cant seem to find the solution. HEre is my code. I have a module.

Imports System.IO<br />
Imports System.Drawing<br />
<br />
Module Module1<br />
<br />
    Dim projfilename As String<br />
    Dim projfile As file<br />
    Dim file As StreamReader<br />
    Dim sentence As String<br />
    Dim xsentence As Integer<br />
    Dim n1, k1, n2, k2 As Double<br />
    Dim data(,) As Double<br />
    Dim xdata, xclass, dimdata, len, xloc, xloc1, xloc2 As Integer<br />
<br />
    Public Sub setprojfileName(ByVal proj As String)<br />
        projfilename = proj<br />
    End Sub<br />
<br />
    Public Sub paintdata(ByVal g As System.Drawing.Graphics)<br />
        readFile()<br />
        showDataArray()<br />
<br />
        Dim r As Integer = 3<br />
        Dim c0 As New Pen(Color.Green, 1)<br />
        Dim c1 As New Pen(Color.Red, r)<br />
        Dim c2 As New Pen(Color.Blue, r)<br />
<br />
        g.DrawLine(c0, 20, 20, 20, 250)<br />
        g.DrawLine(c0, 0, 250, 300, 250)<br />
<br />
        n1 = k1 = n2 = k2 = 0<br />
        Dim x1 As Integer = 0<br />
        Dim x2 As Integer = 0<br />
<br />
        Dim i As Integer<br />
        For i = 0 To xdata - 1<br />
            If data(i, 0) = 1 Then<br />
                x1 = x1 + 1<br />
                n1 += data(i, 1)<br />
                k1 += data(i, 2)<br />
                drawpoint(g, c1, data(i, 1), data(i, 2), r)<br />
            Else<br />
                x2 += 1<br />
                n2 += data(i, 2)<br />
                k2 += data(i, 2)<br />
                drawpoint(g, c2, data(i, 1), data(i, 2), r)<br />
            End If<br />
        Next<br />
        n1 = x1 / x1<br />
        k1 = k1 / x1<br />
        n2 = n2 / x2<br />
        k2 = k2 / x2<br />
<br />
        drawpoint(g, c0, n1, k1, r)<br />
        drawpoint(g, c1, n1, k1, r - 2)<br />
        drawpoint(g, c0, n2, k2, r)<br />
        drawpoint(g, c2, n2, k2, r - 2)<br />
<br />
<br />
<br />
    End Sub<br />
    Public Sub drawpoint(ByVal g As Graphics, ByVal c As Pen, ByVal n As Double, ByVal k As Double, ByVal r As Integer)<br />
        g.DrawEllipse(c, changeN(n), changeK(k), r, r)<br />
<br />
    End Sub<br />
    Public Function changeN(ByVal n As Double) As Integer<br />
        Return n * 20<br />
<br />
    End Function<br />
    Public Function changeK(ByVal k As Double) As Integer<br />
        Return (250 - k * 20)<br />
<br />
<br />
    End Function<br />
    Public Sub showDataArray()<br />
        Dim i As Integer<br />
        Dim da As String = "Data Array :" + Chr(13)<br />
        For i = 0 To xdata - 1<br />
            da += data(i, 1).ToString + " , " + data(i, 2).ToString + " , " + data(i, 0).ToString + Chr(13)<br />
        Next<br />
        MsgBox(da)<br />
<br />
<br />
    End Sub<br />
<br />
    Public Sub readFile()<br />
        Try<br />
            file = projfile.OpenText(projfilename)<br />
<br />
            sentence = file.ReadLine()<br />
            sentence.Trim()<br />
            len = sentence.Length<br />
            xloc = sentence.IndexOf(" ")<br />
            xdata = sentence.Substring(0, xloc)<br />
            dimdata = sentence.Substring(xloc, len - 1)<br />
            ReDim data(xdata, dimdata)<br />
<br />
<br />
            For xsentence = 0 To xdata - 1<br />
                sentence = file.ReadLine()<br />
                sentence.Trim()<br />
                len = sentence.Length<br />
                xloc1 = sentence.IndexOf(" ")<br />
                xloc2 = sentence.LastIndexOf(" ")<br />
<br />
                data(xsentence, 1) = sentence.Substring(0, xloc1)<br />
                data(xsentence, 2) = sentence.Substring(xloc1 + 1, xloc2 - xloc1 - 1)<br />
<br />
                Dim sign As String = sentence.Substring(xloc2 + 1, len - xloc2 - 1)<br />
                If (sign = "+1") Then<br />
                    data(xsentence, 0) = 1<br />
                Else<br />
                    data(xsentence, 0) = -1<br />
                End If<br />
            Next<br />
            file.Close()<br />
<br />
<br />
<br />
<br />
        Catch ex As Exception<br />
            MsgBox("Error when reading File")<br />
<br />
        End Try<br />
    End Sub<br />
End Module


And I have a form:

Public Class Form1<br />
    Inherits System.Windows.Forms.Form<br />
<br />
#Region " Windows Form Designer generated code "<br />
<br />
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br />
        OpenFileDialog1.ShowDialog()<br />
<br />
    End Sub<br />
<br />
    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk<br />
      <br />
        TextBox1.Text = OpenFileDialog1.FileName<br />
        setprojfilename(TextBox1.Text)<br />
<br />
<br />
<br />
    End Sub<br />
<br />
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click<br />
        Dim g As System.Drawing.Graphics<br />
        g = Me.panel1.createGraphics()<br />
        paintdata(g)<br />
<br />
    End Sub<br />
End Class

GeneralRe: Need Serious help or i'll go crazy!!! Pin
toxcct25-Jun-05 22:47
toxcct25-Jun-05 22:47 
GeneralRe: Need Serious help or i'll go crazy!!! Pin
Fernando Soto26-Jun-05 8:32
Fernando Soto26-Jun-05 8:32 
Generalpublisher configuration file Pin
lagumaster25-Jun-05 16:27
lagumaster25-Jun-05 16:27 
GeneralGrid with different color rows Pin
Storas25-Jun-05 13:55
Storas25-Jun-05 13:55 
GeneralDisplay Date only from Dataset Pin
E. Ward25-Jun-05 13:20
E. Ward25-Jun-05 13:20 
GeneralRe: Display Date only from Dataset Pin
Madni Abbasi26-Jun-05 6:24
Madni Abbasi26-Jun-05 6:24 
GeneralRe: Display Date only from Dataset Pin
gharryh30-Jun-05 5:59
gharryh30-Jun-05 5:59 
GeneralNumber Pin
ADY00725-Jun-05 9:23
ADY00725-Jun-05 9:23 
GeneralRe: Number Pin
Colin Angus Mackay25-Jun-05 11:39
Colin Angus Mackay25-Jun-05 11:39 
GeneralRe: Number Pin
toxcct25-Jun-05 22:51
toxcct25-Jun-05 22:51 
GeneralRe: Number Pin
Madni Abbasi26-Jun-05 6:21
Madni Abbasi26-Jun-05 6:21 
GeneralRe: Number Pin
toxcct26-Jun-05 7:30
toxcct26-Jun-05 7:30 
GeneralRe: Number Pin
Madni Abbasi26-Jun-05 23:15
Madni Abbasi26-Jun-05 23:15 
GeneralCrystal Report, Parameter promting Pin
Madni Abbasi25-Jun-05 6:51
Madni Abbasi25-Jun-05 6:51 
GeneralILMerge Problems Pin
Dr_Lomax25-Jun-05 6:02
Dr_Lomax25-Jun-05 6:02 
GeneralRe: ILMerge Problems Pin
Dave Kreskowiak26-Jun-05 8:34
mveDave Kreskowiak26-Jun-05 8:34 
GeneralRe: ILMerge Problems Pin
Dr_Lomax26-Jun-05 8:42
Dr_Lomax26-Jun-05 8:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.