I create the Array cardImages in Class KartenClass
public Image[][][] cardImages = new Image[9][][];
I wrote a method called arrbef() to fill it
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....
and in my Form i call the method arrbef and try to fill it.
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();
}
But when i click on the button which links to this form, i get the following Error:
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)
What did i do wrong? I did exactly the same with another array (which was one dimensonal).
I hope you can help.
This:
public Image[][][] cardImages = new Image[9][][];
... creates a top level array with 9 elements in. Every element value is null. You need:
for (int i = 0; i < cardImages.Length; i++) {
cardImages[i] = new Image[???][]; // What length do you want?
}
Then you can populate cardImages[0][0]
etc as you're doing.
Personally I'd try to avoid 3-dimensional arrays (or arrays of arrays of arrays in this case) - it can get messy. It may be appropriate in this case though; it's hard to say without more information.
EDIT: With more information, it may make sense to model this as a Category[]
(or List<Category>
) where a Category
has a Card[]
or List<Card>
, and a Card
has an Image[]
or List<Image>
. Then at the top level you just have a collection of categories.
See more on this question at Stackoverflow