Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Object reference not set to an instance of an object.

Hey guys

I'm making a function to retrieve all drives from a system and make a diskpart script for it.

Problem is when I try to write the drive number to a text file I get
'Object reference not set to an instance of an object.'

Here's my code.
Code
Friend Function CleanAllDrives()
        Dim drives As Integer = 0
        Dim ord As Integer
        Dim order As String
        Dim query As New SelectQuery("Win32_DiskDrive")
        Dim search As New ManagementObjectSearcher(query)
        Dim info As ManagementObject
        ord = -1
        For Each info In search.Get()
            Dim fs As StreamWriter
            order = ord + 1.ToString

            Dim random As String = "random"
            Dim script As String = "C:\temp\script.txt"
            My.Computer.FileSystem.OpenTextFileWriter(script, True)
            fs.WriteLine(order)
            fs.Close()
        Next
        Return ord
    End Function

Any help is appreciated, I can't see where I've gone wrong. If I do Msgbox(order) it will popup for every drive in the system.

Ignore the fact I have ord and order, they were used for debugging
You are not catching if the file exists or is writable.

Is the file writable and exists?

What line number does the error occur exactly?
(Jul 12, 2023 04:49 AM)-Scrub Wrote:  i fucked my friend in the ass while he sucked off his friend and then switched and then my friend fucked his friend in the ass and i fucked my friends friend in the ass and we all cummed on my friends friend..
(Nov 23, 2015 11:25 AM)Lrrr Wrote:  You are not catching if the file exists or is writable.

Is the file writable and exists?

What line number does the error occur exactly?

The error occurs on 'fs.WriteLine(order)'

Streamwriter creates the file. During debugging it does create the file but it's empty because it errors out.
u work in system dir u need admin rights use try catch as said
I'm a java dude but I can at least recognize the error. When you get this error, it simply means you declared some object variable but never actually initialized it.
I want to suspect this line "Dim fs As StreamWriter" because you later try to write a line to it, but normally if it's anything like a FileWriter, you need to define some path.
Such as
Dim fs As StreamWriter = New StreamWriter ("dir")
But maybe not that exact syntax.

In other words your variable is null so it doesn't know wtf to do when you write to it

Just a guess.
(Nov 23, 2015 11:30 AM)Yuikoro Wrote:  I'm a java dude but I can at least recognize the error. When you get this error, it simply means you declared some object variable but never actually initialized it.
I want to suspect this line "Dim fs As StreamWriter" because you later try to write a line to it, but normally if it's anything like a FileWriter, you need to define some path.
Such as
Dim fs As StreamWriter = New StreamWriter ("dir")
But maybe not that exact syntax.

In other words your variable is null so it doesn't know wtf to do when you write to it

Just a guess.

Thanks for the help but no go.

My path is initialized here:
My.Computer.FileSystem.OpenTextFileWriter("C:\temp\script.txt", False)

If I Msgbox(ord) instead of doing filestream it will popup message boxes for every drive in the system. The file is created, but not written too.
[Image: Xv9OVsf.png]

you never gave the streamwriter anything to write to.
(Nov 23, 2015 11:33 AM)nathan72389 Wrote:  [Image: Xv9OVsf.png]

you never gave the streamwriter anything to write to.

That worked, can't believe I forgot that -.-
Thanks man.
(Nov 23, 2015 11:32 AM)Tɦɛ Alex Wrote:  Thanks for the help but no go.

My path is initialized here:
My.Computer.FileSystem.OpenTextFileWriter("C:\temp\script.txt", False)

If I Msgbox(ord) instead of doing filestream it will popup message boxes for every drive in the system. The file is created, but not written too.

Sure, but that doesn't change the fact that your StreamWriter variable is null. In programming you need to actually initialize the object variable to mean anything, otherwise you're referencing only the static methods within the class as you haven't actually made the object.
Henceforth, the WriteLine function requires initialization more than just declaring.

If you did fs = My.Computer.jibberjabber.blah I'm sure it'd work. If not then clearly I fail at programming, in which case refer to this google search
https://msdn.microsoft.com/en-us/library/hxwfzt61.aspx
(Nov 23, 2015 11:36 AM)Yuikoro Wrote:  Sure, but that doesn't change the fact that your StreamWriter variable is null. In programming you need to actually initialize the object variable to mean anything, otherwise you're referencing only the static methods within the class as you haven't actually made the object.
Henceforth, the WriteLine function requires initialization more than just declaring.

If you did fs = My.Computer.jibberjabber.blah I'm sure it'd work. If not then clearly I fail at programming, in which case refer to this google search
https://msdn.microsoft.com/en-us/library/hxwfzt61.aspx

This is correct, I was being ignorant even though I've written that same line of code about 10 times this week xD

Thanks again, I am very grateful
(Nov 23, 2015 11:38 AM)Tɦɛ Alex Wrote:  This is correct, I was being ignorant even though I've written that same line of code about 10 times this week xD

Thanks again, I am very grateful

Good to see you got it working. Good luck with whatever it is you're making :)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by AeS0pDec 21, 2020 06:19 AM 0 200 Dec 21, 2020 06:19 AM
Last Post: AeS0p
[C] by saltycrackerMay 29, 2020 04:36 AM 0 140 May 29, 2020 04:36 AM
Last Post: saltycracker
[.NET] by Shadow ReignSep 15, 2019 01:40 PM 3 168 Sep 15, 2019 01:40 PM
Last Post: Claw