C# 填充多维图像数组时出现空引用异常
我在KartenClass类中创建了数组图像C# 填充多维图像数组时出现空引用异常,c#,image,visual-studio-2012,multidimensional-array,nullreferenceexception,C#,Image,Visual Studio 2012,Multidimensional Array,Nullreferenceexception,我在KartenClass类中创建了数组图像 public Image[][][] cardImages = new Image[9][][]; 我编写了一个名为arrbef()的方法来填充它 public void arrbef() { this.cardImages[0][0] = new Image[3] { global::WindowsFormsApplication4.Properties.Resources.Card, global::WindowsFormsAppli
public Image[][][] cardImages = new Image[9][][];
我编写了一个名为arrbef()的方法来填充它
public void arrbef()
{
this.cardImages[0][0] = new Image[3] { global::WindowsFormsApplication4.Properties.Resources.Card, global::WindowsFormsApplication4.Properties.Resources.CardBack, global::WindowsFormsApplication4.Properties.Resources.CardSet };
this.cardImages[0][1] = new Image[3] { global::WindowsFormsApplication4.Properties.Resources.Card, global::WindowsFormsApplication4.Properties.Resources.CardBack,
etc....
在我的表单中,我调用方法arrbef并尝试填充它
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Karten : Form
{
KartenClass karten = new KartenClass();
int standort = 0;
public Karten()
{
InitializeComponent();
KartenClass.karten[0].arrbef();
}
但是,当我单击链接到此表单的按钮时,出现以下错误:
System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
bei WindowsFormsApplication4.KartenClass.arrbef() in c:\Users\david.kresse\Documents\Visual Studio 2012\Projects\WindowsFormsApplication5\KartenClass.cs:Zeile 24.
bei WindowsFormsApplication4.Karten..ctor() in c:\Users\david.kresse\Documents\Visual Studio 2012\Projects\WindowsFormsApplication5\Karten.cs:Zeile 22.
bei WindowsFormsApplication4.Start.btnStartGoToKarten_Click(Object sender, EventArgs e) in c:\Users\david.kresse\Documents\Visual Studio 2012\Projects\WindowsFormsApplication5\Start.cs:Zeile 394.
bei System.Windows.Forms.Control.OnClick(EventArgs e)
bei System.Windows.Forms.Button.OnClick(EventArgs e)
bei System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
bei System.Windows.Forms.Control.WndProc(Message& m)
bei System.Windows.Forms.ButtonBase.WndProc(Message& m)
bei System.Windows.Forms.Button.WndProc(Message& m)
bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
bei System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
我做错了什么?我对另一个数组(一维数组)也做了同样的操作
我希望你能帮忙 这是:
public Image[][][] cardImages = new Image[9][][];
。。。创建包含9个元素的顶级数组。每个元素值都是空的。你需要:
for (int i = 0; i < cardImages.Length; i++) {
cardImages[i] = new Image[???][]; // What length do you want?
}
for(int i=0;i
然后,您可以在执行时填充cardImages[0][0]
等
就我个人而言,我会尽量避免使用三维数组(在本例中是数组的数组),这样会变得很混乱。但在这种情况下可能是合适的;没有更多的信息很难说
编辑:有了更多信息,可以将其建模为
类别[]
(或列表
),其中类别
有卡[]
或列表
,而卡
有图像[]
或列表
。然后,在顶层,您只需要一组类别。此.cardImages[0]:)的长度是多少?最好在调试器设置中启用“一次抛出中断”。然后,当问题发生时,您可以得到一个调用堆栈,您可以学习如何自己调试它。欢迎使用堆栈溢出!几乎所有NullReferenceException
的情况都是相同的。请参阅“”,以获得一些提示。该数组包含9个类别,20张卡片,每个卡片都有3个条件。@user3086972:请参阅我的编辑,了解我当时如何建模-通过使每个级别的含义清晰,整个代码变得更清晰。好的,谢谢,我将尝试。不过,我想我还需要一些教程。但我在上面!非常感谢你,我的朋友!:)我用for循环试过了,效果非常好。一旦我学会如何做,我会用列表再试一次。:)