-2

I tried to define the x and y positions for the diagonal edges for each cell in the following picture:

here

I do not know if my way to obtain these positions is right or not so I need to print the output gagged array on txt file as matrix to check that, I wrote the following code :

    using System;
    using System.IO;
    using System.Text;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Threading;
    using AUV_Topology;
    using System.Collections.Generic;
 
    namespace AUVtopology
    {
        public partial class Form1 : Form
        {
 
        static int[] cellsCenters;     
        static int[] columnsXs;     
        static int[][] rows;
 

        public int[][] calculateCellsCenters(int intialPoint,int lastPoint)
        {     
            FileStream fs = new FileStream("C:/Users/Welcome/Desktop/testPositions.txt", FileMode.Append, FileAccess.Write);
 
            int d = lastPoint - intialPoint;     
            int cellSide = d /5;     
            int intialCell = intialPoint - cellSide;     
            columnsXs = new int[6];     
            columnsXs[0] = intialCell;
 
            rows = new int[6][]; // jagged array
 
            for (int i = 0; i < rows.Length; i++)
            {    
                rows[i] = new int[6];
 
                for (int k = 0; k < rows[i].Length; k++)
                {
                    columnsXs[k] = columnsXs[k] + cellSide;
     
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.Write(" "+columnsXs[k]+ " ");
                    }         
                }
 
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine();
                }     
            } 

            return rows;     
        }
 
        private void selectedPath_Click(object sender, EventArgs e)
        {
          calculateCellsCenters(200, 325);
        }
    } // end-class
} // end namespace

I got the following error in the following picture when I pressed the button that call the method which is responsible to make these calculations and print the gagged array :

here

The details of this error :

 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 AUVtopology.Form1.calculateCellsCenters(Int32 intialPoint, Int32 lastPoint, Int32 numOfcellsEachRow) in C:\Users\Welcome\Desktop\project\GAAPS\AUV_Topology\Form1.cs:line 935
       at AUVtopology.Form1.selectedPath_Click(Object sender, EventArgs e) in C:\Users\Welcome\Desktop\project\GAAPS\AUV_Topology\Form1.cs:line 975
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


    ************** Loaded Assemblies **************
    mscorlib
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1637.0 built by: NETFXREL3STAGE
        CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
    ----------------------------------------
    AUV_Topology
        Assembly Version: 1.0.0.0
        Win32 Version: 1.0.0.0
        CodeBase: file:///C:/Users/Welcome/Desktop/project/GAAPS/AUV_Topology/bin/Release/AUV_Topology.exe
    ----------------------------------------
    System.Windows.Forms
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1586.0 built by: NETFXREL2
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1637.0 built by: NETFXREL3STAGE
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Drawing
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1586.0 built by: NETFXREL2
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    Microsoft.VisualBasic.PowerPacks.Vs
        Assembly Version: 10.0.0.0
        Win32 Version: 10.0.30319.1
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualBasic.PowerPacks.Vs/10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.PowerPacks.Vs.dll
    ----------------------------------------
    Microsoft.VisualBasic
        Assembly Version: 10.0.0.0
        Win32 Version: 14.6.1586.0 built by: NETFXREL2
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualBasic/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
    ----------------------------------------
    System.Core
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1638.0 built by: NETFXREL3STAGE
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
    ----------------------------------------

    ************** JIT Debugging **************
    To enable just-in-time (JIT) debugging, the .config file for this
    application or computer (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 computer
    rather than be handled by this dialog box.

Also, is my code right in which to get the right positions as I mentioned before ??

Matt Hogan-Jones
  • 2,981
  • 1
  • 29
  • 35
isra
  • 5
  • 3
  • 2
    http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – ColinM May 08 '17 at 14:32
  • You've not initialised your static array variables to anything, (the error message is pointing to the line with `columnsXs[0] = intialCell;` and you can't set index 0 in the `columnsXs` array without the array being initialised) – George Duckett May 08 '17 at 14:37
  • @GeorgeDuckett : I've updated my code and still having the problem – isra May 08 '17 at 15:12
  • If you step through the code in debug mode, which line does it throw the exception on? – Matt Hogan-Jones May 08 '17 at 15:53
  • Also, I can't reproduce the error if I your code without all the file stuff. Please update your question to be more accurate and specify precisely which line the error is occurring on. – Matt Hogan-Jones May 08 '17 at 15:55
  • on print on the file (using ...) – isra May 08 '17 at 15:57
  • Please update **the question** with the new information - specify **in the question** the line that is causing the error and **include the error text**. Is it the same error as last time? Is it a new error? We don't know and so we can't help. There are two places you have `using` statements with a `StreamWriter` - which one is failing? – Matt Hogan-Jones May 08 '17 at 16:01
  • Are you now getting a `Stream was not writable` exception the second time you try to write to that stream? – Matt Hogan-Jones May 08 '17 at 16:36

1 Answers1

0

I think it's because you do this in the loop many times:

using (StreamWriter sw = new StreamWriter(fs))
{
    sw.Write(" " + columnsXs[k] + " ");
}

When the using block finishes the StreamWriter is disposed and closes the underlying FileStream. So the next time you try to write to the underlying FileStream you can't because it is closed.

Try putting all of your loop code inside a using statement so that you only dispose of the StreamWriter once:

using (StreamWriter sw = new StreamWriter(fs))
{
    for (int i = 0; i < rows.Length; i++)
    {
        rows[i] = new int[6];
        for (int k = 0; k < rows[i].Length; k++)
        {
            columnsXs[k] = columnsXs[k] + cellSide;
            sw.Write(" " + columnsXs[k] + " ");
        }
        sw.WriteLine();
    }
}
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
Matt Hogan-Jones
  • 2,981
  • 1
  • 29
  • 35