-1

I took this error in line I commented, what's the problem?

private void pictureBox34_Click(object sender, EventArgs e)
{
    if (pictureBox34.Image == chess9.Properties.Resources.siyahsah2)
    {
        f();
    }
}

public void picarray()
{
    pic[0, 0] = pictureBox54;
    pic[0, 1] = pictureBox64;
    pic[0, 2] = pictureBox48;
    pic[0, 3] = pictureBox42;
    pic[0, 4] = pictureBox34;
    pic[0, 5] = pictureBox26;
    pic[0, 6] = pictureBox18;
    pic[0, 7] = pictureBox8;
    pic[1, 0] = pictureBox1;
    pic[1, 1] = pictureBox2;
    pic[1, 2] = pictureBox3;
    pic[1, 3] = pictureBox4;
    ...
}

public void f()
{
    // int i = 0, j = 0;
    int x = 3;
    int y = 3;

    for (int i = 1; i < x; i++)
    {
        for (int j = 1; j < y; j++)
        {
            pic[i, j] = new PictureBox();
            // pic[i, j] = pic[i + 1, j + 1];
            pic[i, j].Image = chess9.Properties.Resources.siyahsah2;
        }
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Arash
  • 3,013
  • 10
  • 52
  • 74
  • check out the path of your image... – Javed Akram Dec 05 '10 at 09:54
  • 4
    Your code is insane. Why are you assigning 3 times to the same array entry just after each other. The first two assignments are no-ops. And your `picarray` function is just sad. Why are you using 64 pre-created pictureboxes instead of just creating them in a loop at runtime? – CodesInChaos Dec 05 '10 at 09:56
  • 1
    And your path is relative to the working directory, instead of relative to the application-directory. This will lead to unexpected errors whenever your program is started with the working-directory differing from the application directory. – CodesInChaos Dec 05 '10 at 10:01
  • 1
    @CodeInChaos: Agreed, with one caveat: Winforms doesn't automatically `Dispose` images inside the PictureBoxes. So, having only one pre-created instance of each image can be advantageous over creating them whenever needed, since you won't run out of window/GDI handles as quickly (since you end up creating less fresh `PictureBox` instances). But of course that doesn't fix the real issue, (which would of course be to take care of correctly disposing the images). – stakx - no longer contributing Dec 05 '10 at 10:08
  • 1
    I'd create 64 pictureboxes on startup into an array. And one Image per piece into another array. Then I'd just assign the image from the image array to the desired picturebox. (Or just write rendering code with a single picturebox, but that's way over the OPs head). – CodesInChaos Dec 05 '10 at 10:15
  • @Codeinchaos:i edited my code as your answer,but i dont know how to use pictureboxes in loop in picarray function,can you help me?thanks so much – Arash Dec 05 '10 at 12:09
  • @stakx:thanks for your comment,what do you mean about pre-created instance of image,you mean i save them in resource?i did it,if i understood bad please explain me more.thanks so much – Arash Dec 05 '10 at 12:14
  • (@arash: I mentioned a detail that probably at this point you shouldn't worry too much about; you've probably more important issues to deal with. Basically, what I said is that you should call `Dispose` on all instances of `Image` when you no longer need them. This includes calling `Dispose` on an `Image` in a `PictureBox` when you replace it with another `Image`.) – stakx - no longer contributing Dec 05 '10 at 12:22
  • @arash, I reformatted your code... again. (Btw. you're missing a closing curly bracket.) – stakx - no longer contributing Dec 05 '10 at 12:27
  • @stakx:thanks,i mistaked(added two public f())and closing curly bracket was for it – Arash Dec 05 '10 at 12:43
  • Possible duplicate of [NullReferenceException was unhandled](https://stackoverflow.com/questions/8527377/nullreferenceexception-was-unhandled) – Cœur Dec 26 '18 at 05:06
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Cœur Dec 26 '18 at 06:23

3 Answers3

6

I happened to read your previous question, so if I understand correctly, this is an implementation for a chess game.

Instead of trying to fix the exception in this code, I would suggest that you make some changes to your design.

The main change: separate the representation of your game-board status, from the UI layer. If I understand correctly, you are using an array of PictureBox objects to represent the pieces on the board. You would probably be better off if you write classes that represent the board, pieces and all their actions, and then write seperate code that can display these classes in a window, and take commands from the user through UI.

When you are implementing piece movements and board updates through manipulation of UI objects, you are bound to make more errors and spend much more time debugging.

I hope that helps...

Ran
  • 5,989
  • 1
  • 24
  • 26
  • i know this is good for writing a complete chess game, but im writing codes for chess game that have 3 pieces,a king and a queen one side and other side just a king,that side1 must mate the king with less movements – Arash Dec 05 '10 at 12:03
  • Regardless of the game you are developing, it is always a good idea to separate the "game world" from the graphical representation and UI. Even if the game has a simplified chess board with only a few pieces. – Ran Dec 05 '10 at 12:05
  • yes,you say correct,i will separate gameboard status from UI,but just i want to correct this movement of king piece and see that it works then i will do it.thanks so much for your suggestion – Arash Dec 05 '10 at 12:21
4
  • Make sure the file indicated actually exists. Also, preferably change / to \\ (or \ in a @"..." string) in the file path, if you're running this on a Windows system. (Though that shouldn't be the cause of your issue.)

    ... = Image.FromFile( @"pic\siyahsah2.jpg" );

  • Make sure the indices i and j are correct. Your for loops indicate that they will be any combination of 1 and 2.

Besides, inside your for loop:

pic[i, j] = new PictureBox();            //  <-- will get overwritten by (*)
pic[i, j] = pic[i + 1, j + 1];           //  <-- will get overwritten by (*)
pic[i, j] = new PictureBox();            //  <-- (*)
pic[i, j].Image = Image.FromFile(...);

That is, you can delete the first two lines.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • thanks,i did what you said and now it doesnt give any error but it doesnt any movement – Arash Dec 05 '10 at 11:55
  • excuse me sir,i didnt say clear,i mean movements of king piece in the chess game,ok i will ask another question,thanks – Arash Dec 05 '10 at 12:36
  • 1
    @arash: No worries. I admit I *did* know you were talking about movement of game figures -- just take the above comment as a clue that mixing different questions makes answering and voting here on SO difficult. ;) – stakx - no longer contributing Dec 05 '10 at 12:41
1

I don't know why you're trying to do it so complicated, but assuming the knight is at picturebox34 or in other words at pic[0, 4] and you want to move it to the right and up and thus end up at pic[2, 3] then it's nothing more than doing this

// Move knight image from 0,4 to 2, 3
pic[2, 3].Image = pic[0, 4].Image;

// Make old knight position empty
pic[0, 4].Image = null;

I have no idea why're you doing the for loops and such..

Doggett
  • 3,364
  • 21
  • 17
  • thanks,i know this,but i dont want to do this manually,it should do automatically,for example now when the king come to pic[2,3] it should automatically find the ways that it can go when i click on king piece,if you didnt understand tell me to explain more,thanks – Arash Dec 05 '10 at 13:05
  • a knight has 8 possible moves though, how do you decide which one it should take? – Doggett Dec 05 '10 at 18:17
  • its king not knight,yes it has 8 possible movement i want when i click on king picturebox it shows the possible movements of king,how is it?then i want to implement queen and king for the black team,and their movements,and then i would decide about which movement it should take,as i said,it has just pieces,king and queen one side and other side just a king,queen and king should mate king in less movements.what do you suggest me?thanks so much – Arash Dec 05 '10 at 20:30
  • 1
    That's a little bit to complex to only use PictureBoxes, it would be a lot easier if you did what Ran suggested. If you don't know how to do that I'd suggest to at least try and just make separate questions for the problems you run into along the way – Doggett Dec 05 '10 at 21:57