Object reference not set to an instance of an object

H

hansiman

Coming from classic asp, I'm a bit in the dark here.

I want to dispaly the content of a file in a literal server control.

I've created a class utils.vb

Imports System.IO

Public Class Utils
Inherits System.Web.UI.Page
Public Function ReadFile(ByVal pFile) ' As String
Dim ForReading As Integer = 1
Dim fso As Object
Dim ts
Dim s
fso = CreateObject("Scripting.FileSystemObject")
ts = fso_OpenTextFile(MapPath(pFile), ForReading)
s = ts.ReadAll
ts.Close()
ReadFile = s
End Function
End Class

on the aspx page I use

Dim utl As Utils = New Utils
Dim lBody As String = utl.ReadFile( strFile )

litContent.Text = lBody

If I have the ReadFile function in the code behind page of the calling
aspx page it works.

When I move the function to the utils.vb class I get the following
error:

System.NullReferenceException: Object reference not set to an instance
of an object

Line 11: Dim s
Line 12: fso = CreateObject("Scripting.FileSystemObject")
Line 13: ts = fso_OpenTextFile(MapPath(pFile), ForReading)
Line 14: s = ts.ReadAll
Line 15: ts.Close()

Source File: c:\inetpub\wwwroot\project\Utils.vb Line: 13

I figure its the dim ts part of ReadFile function. But ...???

/Morten
 
M

Martin Dechev

Hi, hainsiman,

It is the call to MapPath() that is throwing the exception. Try:

HttpContext.Current.Server.MapPath()

instead.

Greetings
Martin
 
H

hansiman

Hi Martin

I still get the error even when I change the mappath part (see below).
The strange thing is that if I put the ReadFile function in the code
behind page of the aspx file it works with simply MatPath (pFile)...

utils.vb

Public Function ReadFile(ByVal pFile)
Dim ForReading As Integer = 1
Dim fso As Object = CreateObject("Scripting.FileSystemObject")
Dim ts, s
ts =
fso_OpenTextFile(MapPath(HttpContext.Current.Server.MapPath(pFile)),
ForReading)
s = ts.ReadAll
ts.Close()
ReadFile = s
End Function


Call from aspx file:

Dim utl As Utils = New Utils
Dim lBody As String = utl.ReadFile( strFile )
litContent.Text = lBody


Error:
Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:

Line 11: Dim ts, s
Line 12: 'ts = fso_OpenTextFile(MapPath(pFile), ForReading)
Line 13: ts =
fso_OpenTextFile(MapPath(HttpContext.Current.Server.MapPath(pFile)),
ForReading)
Line 14: s = ts.ReadAll
Line 15: ts.Close()

Source File: c:\inetpub\wwwroot\customersurveyanalysis\mmaUtils.vb
Line: 13
 
M

Martin Dechev

fso_OpenTextFile(MapPath(HttpContext.Current.Server.MapPath(pFile)),

should be:

fso_OpenTextFile(HttpContext.Current.Server.MapPath(pFile)),

Greetings
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top