Welcome, Guest. Please login or register.
Did you miss your activation email?
May 20, 2013, 12:39:38 AM

Login with username, password and session length
Search:     Advanced search
The look of the forum is still being worked on. Thank you for your patience.
193705 Posts in 16235 Topics by 17049 Members
Latest Member: honeyzephyr89

* Home Help Login Register
AppleGeeks.com  |  Help / Advice  |  Programming  |  Topic: error 0 Members and 1 Guest are viewing this topic.
Pages: [1] Go Down Print
Author Topic: error  (Read 1925 times)
paxa
Sr. Member
****
Posts: 449


« on: October 21, 2005, 11:41:59 AM »

this is the problem i'm trying to edit a file right! it's a ini file so what i want to do is replace the values in the ini file for the ones i want. this is the contents of the ini file
[Options]
SECURITYMODE="@sql" ->replace @ for what i want
DISABLENETWORKPROTOCOLS=0
sapwd="@pwd"-> the same
instancename="@instn" -> also the same
datadir=@datd-> the same


i'm using vb.net to do this, i want the contents of a textbox to replace those values.
this is the code i'm using
right after "windows form designer generated code"
i've wrote this

Dim objword As Object
Dim i As Integer


following this

Try
'Instancia a Aplicação Word.
objword = CreateObject("notepad.Application")
'Abre o documento aviso.doc do Microsoft Word.
objword.Documents.Open("c:\Documents and Settings\jpfcardoso\Ambiente de trabalho\setup.ini\")

SubstituiVariavel("@sql", TextBoxtseg.Text)
SubstituiVariavel("@pwd", TextBoxpassw.Text)
SubstituiVariavel("@instn", TextBoxinstn.Text)
SubstituiVariavel("@datad", TextBoxdatadir.Text)

If MsgBox("Confirma a escrita no documento?", MsgBoxStyle.YesNo, "notepad - setup.ini") = MsgBoxResult.Yes Then
'torna o Word visivel
objword.visible = False
'habilita o botão para fechar o word sem salvar
' btnFechar.Enabled = True
Else
fechaWord()
End If
Catch ex As Exception
objword.ActiveDocument.Close(True)
objword.Quit()
objword = Nothing
MsgBox(ex.Message)
End Try

End Sub
Public Sub SubstituiVariavel(ByVal Achar As String, ByVal Substituir As String)

'procura a variável e substitui o valor
With objword.Selection.Find
.Text = Achar.ToString
.Replacement.Text = Substituir.ToString
.Forward = True
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

While .Execute = True
objword.Selection.Select()
System.Windows.Forms.Clipboard.SetDataObject(Substituir)
objword.Selection.Paste()
End While

End With

End Sub

Public Sub fechaWord()

'Fecha o documento Word sem salvar
objword.ActiveDocument.Close(False)
'Fecha o Word
objword.Quit()
objword = Nothing
'#
End Sub


and this is what i get
http://img440.imageshack.us/img440/5638/erro3ww.jpg
Logged
paxa
Sr. Member
****
Posts: 449


« Reply #1 on: October 21, 2005, 07:07:27 PM »

so...anyone?Huh?
Logged
Hawk
Administrator
Hero Member
*****
Posts: 3212


Get A Mac!


WWW
« Reply #2 on: October 24, 2005, 11:06:43 AM »

sorry, no idea.

try this for help. http://www.experts-exchange.com/Programming/
Logged

"Saying Windows is like Macintosh is like finding a potato that looks like Jesus and believing you've witnessed the second coming."
auric
Sr. Member
****
Posts: 498



« Reply #3 on: October 31, 2005, 01:22:49 PM »

and this is what i get
System.NullReferenceException

I base my explanation on Java: Probably NullReferenceException is the same as a NullPointerException.  This means that or objword is Nothing, or ActiveDocument is Nothing

A very important thing to know is what the last thing was that has been executed, before the exception is thrown. Since you didn't put your entire code inhere and I don't know enough about VB.net, I can only guess... What happens when objword = CreateObject("notepad.Application") fails? What happens when objword.Documents.Open("...") fails?

There's a really good chance that one of those things fail.
My guess: CreateObject fails and keeps objword = Nothing.
Then you call a field within objword: you get objword.Documents and perform .Open on that. But since objword = Nothing, there is no .Documents to call, so this will generate an NullReferenceException.
You catch that, that's ok, but what do you do then? You call objword.ActiveDocument.Close(), AGAIN ON THE NOTHING OBJECT

since objword is still Nothing, .ActiveDocument can't be read out, and this will generate a NullReferenceException AGAIN. Since you don't catch that, the exception is thrown to the system and the system will show you the "crash" because basically that's what your "program" does at that point.

Suggestion
Check on every value that could be Nothing:

Code:
objword = CreateObject("notepad.Application")
If objword is Nothing then
  MessageBox("Could not create notepad.Application");
  System.Exit()
End If

' at this point, you can be sure that objword is not Nothing,
' otherwise the program would be shut down.


You should do things like that on every object that could be possibly Nothing.

Oh and don't copy-paste my code snip, because it's probably no good VB. As I said: I don't know the exact syntax of VB.net, but you get the point. If you don't, consider a programming course first Cheesy

EDIT:
Perhaps it helps too if you comment in English, especially when you ask for help. Not everyone can speak Spanish - I'm one of them! Smiley
« Last Edit: October 31, 2005, 01:29:12 PM by auric » Logged

"In a world without walls and fences, who needs Windows and Gates ?" -- Dino Esposito
paxa
Sr. Member
****
Posts: 449


« Reply #4 on: November 03, 2005, 09:54:32 PM »

so sorry but i figured it...instead of replacing the @thingys...i decided to create the file instead.
 i should have gone this way for starters..instead of trying to do that
here's the code if someone wants it...it's a simple streamwritter thing
Code:
Private Sub parammsde()
 Dim streama As New System.IO.StreamWriter("C:\admincfg\setupmsde\setup.ini", True)
        Try
            streama.WriteLine("[Options]")
            streama.WriteLine("SECURITYMODE=" & TextBoxtseg.Text.ToString & " ")
            streama.WriteLine("DISABLENETWORKPROTOCOLS=0")
            streama.WriteLine("sapwd=" & TextBoxpassw.Text.ToString & " ")
            streama.WriteLine("instancename=" & TextBoxinstn.Text.ToString & "")
            streama.WriteLine("datadir= C:\admincfg\msde")
        Finally
            streama.Close()
        End Try

    End Sub

it creates the file and replaces certain spaces for the contents of a textbox





Logged
Pages: [1] Go Up Print 
AppleGeeks.com  |  Help / Advice  |  Programming  |  Topic: error
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.069 seconds with 19 queries.