Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 19th, 2004, 01:17 PM
Registered User
 
Join Date: Dec 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Compile buts crashes

Hello everyone, new user to the forums, and to the c# language. As a result, forgive me if the answer to the following query is painfully obvious to the reader!

I am writing a program in which a user gives a single string, which is then processed to perform certain actions. In the following case, this routine is supposed to allow someone to type "set winamp c:\foo\bar\winamp.exe" in order to set a variable in the program to it's path.

However, upon enabling this routine, the program will happily compile, but crashes upon any input from the user, even if it doesn't (shouldn't?) trip either of the IF statements.

//words[] is an array holding each word in the given command as an individual object in the array
//LogCommand() is a routine that logs the command given to the program, and works fine for all other commands, so the problem isn't there.
//winamp is a string variable holding the path to winamp's executable
//log is a string value holding the log to be written out later. Again, this works fine for every other command.

private void CommandSetWinampPath()
{
if (words[1] == "SET")
{
if (words[2] == "WINAMP")
{
LogCommand();
winamp = "";
for (int a = 3; a < (words.Length); a++)
{
winamp = winamp + words[a];
}
log = log + "Winamp path set as " + winamp;
}
}
}

Any ideas?

 
Old December 19th, 2004, 07:27 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

When you say crash do you mean you get an unhandled exception?

The only problem I can see in your code is that you are referencing the words array from item 1. In C# all array's are based at 0 (zero) (someone let me know if this isn't the case). So your code should be...
Code:
if(words[0] == "SET")...
if(words[1] == "WINAMP")...
for(int a = 2;a < (words.Length);a++)...

Kep.
 
Old December 20th, 2004, 04:12 AM
Registered User
 
Join Date: Dec 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply,

I noticed my rather silly error with the array element numbers and corrected it, but unfortunately this doesn't solve the problem.

As for what I mean by crash, I do indeed get an unhandled exception. When testing the file, I get message;

An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll

Additional information: Object reference not set to an instance of an object.


When simply building the file and running the exe, I get;

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at Helper.Form1.CommandSetWinampPath() in c:\documents and settings\adrian wood\my documents\visual studio projects\helper\helpermain.cs:line 449
   at Helper.Form1.button1_Click(Object sender, EventArgs e) in c:\documents and settings\adrian wood\my documents\visual studio projects\helper\helpermain.cs:line 208
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.PerformClick()
   at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData)
   at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
   at System.Windows.Forms.TextBoxBase.ProcessDialogKey( Keys keyData)
   at System.Windows.Forms.Control.PreProcessMessage(Mes sage& msg)
   at System.Windows.Forms.ThreadContext.System.Windows. Forms.UnsafeNativeMethods+IMsoComponent.FPreTransl ateMessage(MSG& msg)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 1.0.5000.0
    Win32 Version: 1.1.4322.2032
    CodeBase: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
Test
    Assembly Version: 1.0.1815.14353
    Win32 Version: 1.0.1815.14353
    CodeBase: file:///C:/Documents%20and%20Settings/Adrian%20Wood/My%20Documents/Visual%20Studio%20Projects/Helper/bin/Debug/Test.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 1.0.5000.0
    Win32 Version: 1.1.4322.2032
    CodeBase: file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
    Assembly Version: 1.0.5000.0
    Win32 Version: 1.1.4322.2032
    CodeBase: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
    Assembly Version: 1.0.5000.0
    Win32 Version: 1.1.4322.2032
    CodeBase: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
System.Xml
    Assembly Version: 1.0.5000.0
    Win32 Version: 1.1.4322.2032
    CodeBase: file:///c:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
----------------------------------------
Accessibility
    Assembly Version: 1.0.5000.0
    Win32 Version: 1.1.4322.573
    CodeBase: file:///c:/windows/assembly/gac/accessibility/1.0.5000.0__b03f5f7f11d50a3a/accessibility.dll
----------------------------------------

************** JIT Debugging **************
To enable just in time (JIT) debugging, the config file for this
application or machine (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the machine
rather than being handled by this dialog.


Apologies for not including this information in my original posting.

My current code, after corrections for array numbering (and an additional IF statement to check if the rest of the array is empty, is as follows;

private void CommandSetWinampPath()
        {
            if (words[0] == "SET")
            {
                if (words[1] == "WINAMP")
                {
                    if (words.Length >2)
                    {
                        LogCommand();
                        winamp = "";
                        for (int a = 2; a < (words.Length); a++)
                        {
                            winamp = winamp + words[a];
                        }
                        log = log + "Winamp path set as " + winamp;
                    }
                }
            }


 
Old December 20th, 2004, 06:29 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Where do you initialize the words array?
How do you ensure that there are at least 2 items in the array?

The fact that you are getting a NullReferenceException means that you are trying to access an object that has not yet been created.

Does the exception occur on the line if(words[0] == "SET")?


Kep.
 
Old December 22nd, 2004, 05:27 PM
Registered User
 
Join Date: Dec 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Apologies for the delay, I have been away from my computer for the past few days.

The words[] array is initialised under;

public class Form1 : System.Windows.Forms.Form

along with all the other variables used throughout the program.

Once the main form button is pressed, I run a command SplitCommand(), which does;

string[] words = command.Split(' ');

where command is the initial input textBox that the user fills with data.

SplitCommand() also writes out the original command, and each element of the words[] array, into a log file, so I am positive the array is being initialised and filled correctly.

I wasn't previously checking the size of the array, and even with the revised code in my previous post, it didn't check the array until after it had checked the first two elements... but now, I have changed it once again, and it checks the length of the array immediately after calling CommandSetWinampPath().

One interesting part of this puzzle is that the error occurs immediately upon the forms' main button being pressed; a step-by-step debug confirms this. It doesn't perform any of the actions supposed to be called upon the button being pressed. It doesn't get as far as the command being called (which occurs after several other actions,) let alone used.

It's worth noting that merely commenting out the call to the action is enough to get rid of the error; commenting out the code for the action itself is not necessary. This would make sense, if only the exception didn't occur long before the call was reached!

I am quite willing to post up my entire source if necessary, or make it available for download, if this will aid in diagnosing the problem; it's intended for this program to become open source eventually anyway, once I've laid enough groundwork and feel confident enough in the work I've produced.

 
Old December 28th, 2004, 05:48 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Posting the code might be a good idea along with the statement that causes the exception because it sounds like the problem is with another part of your code.

Kep.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel crashes sunny76 Excel VBA 0 July 2nd, 2008 08:25 PM
My VB 6 crashes saikrishnan Beginning VB 6 2 January 18th, 2005 12:22 PM
VC++6 crashes when compiling cardin Visual C++ 1 January 13th, 2005 09:07 PM
Server Crashes chilluk Classic ASP Databases 1 December 17th, 2004 05:43 AM
VB 5 crashes Alkap Beginning VB 6 1 October 24th, 2003 10:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.