0

当我在类KartenClass创建阵列cardImages空引用异常填充多维图像阵列

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) 

我做了什么错?我做了与另一个阵列(这是一个dimensonal)完全相同。

我希望你能帮上忙。

+0

'this.cardImages [0]':)的长度是多少? –

+0

最好在调试器设置中打开break。然后当问题发生时你可以得到一个callstack,你可以学习如何自己调试它。 –

+0

欢迎来到Stack Overflow!几乎所有的'NullReferenceException'都是一样的。请参阅“[什么是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)”的一些提示。 –

回答

1

此:

public Image[][][] cardImages = new Image[9][][]; 

...创建与9种元素的顶层阵列的每个元素的值是空值。您需要:

for (int i = 0; i < cardImages.Length; i++) { 
    cardImages[i] = new Image[???][]; // What length do you want? 
} 

然后你就可以填充cardImages[0][0]等作为你在干什么。

就我个人而言,我会尽量避免3维数组(或在这种情况下数组的数组) - 它可以变得混乱。在这种情况下,这可能是适当的;没有更多的信息很难说。

编辑:随着越来越多的信息时,它可能是有意义的模拟这种作为Category[](或List<Category>)其中a Category具有Card[]List<Card>Card具有Image[]List<Image>。然后在顶层,您只需收集一些类别。

+0

该数组包含9个类别,包含20个卡片,每个卡片具有3个条件。 – user3086972

+0

@ user3086972:查看我的编辑,了解如何对它进行建模 - 通过使每个层次的含义清晰,整个代码变得更加清晰。 –

+0

好的,谢谢,我会试试。我想我将不得不为这个几个教程。但我就在它上面!非常感谢你我的朋友! :) – user3086972