如何将文件中的锯齿状数组元素打印为矩阵或网格 [英] How to print jagged array elements on a file as matrix or grid

查看:74
本文介绍了如何将文件中的锯齿状数组元素打印为矩阵或网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在下图中为每个单元格定义对角线边缘的x和y位置:



格子网格



我不知道我的获取方式这些位置是对还是不对,所以我需要在txt文件上打印输出锯齿状数据作为矩阵来检查,我写了下面的代码:

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

Grid of Cells

I do not know if my way to obtain these positions is right or not so I need to print the output jagged 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 :


Error after clicking the button

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.





我尝试了什么:



我需要知道问题在哪里以及如何正确计算位置



What I have tried:

I need to know where is the problem and how to compute locations correctly

推荐答案

信息的重要部分是:

The important part of the message is this:
System.NullReferenceException: Object reference not set to an instance of an object.



告诉您其中一个对象为null。确定哪一个是在调试器中运行代码并检查发生异常的行上的每个变量的最佳方法。一旦找到哪个变量(或变量)为null,您就应该能够确定应该在哪里创建并修复它。


It is telling you one of your objects is null. The best way to figure out which one is to run your code in the debugger and inspect each variable on the line where the exception occurs. Once you find out which variable (or variables) is null you should be able to figure out where it should have been created and fix it.


这篇关于如何将文件中的锯齿状数组元素打印为矩阵或网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆