0

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.

3
  • What is the length of this.cardImages[0] :) ? Commented Dec 10, 2013 at 13:28
  • It's always best to turn on break on throw in the debugger settings. Then you can get a callstack when the problem happens, and you can learn how to debug it yourself. Commented Dec 10, 2013 at 13:31
  • Welcome to Stack Overflow! Almost all cases of NullReferenceException are the same. Please see "What is a NullReferenceException in .NET?" for some hints. Commented Dec 10, 2013 at 14:03

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

the array contains 9 categories with 20 cards, each with 3 conditions.
@user3086972: See my edit for how I'd model it then - by making the meaning of each level clear, the whole code becomes clearer.
Okay thank you, i will try that. I think i will have to hit a few tutorials for that though. But i´m on it! Big thanks to you my friend! :)
I tried it with the for-loop and it works perfectly. I will try it again with lists as soon as i learned how to do it. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.