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
|