English 中文(简体)
c# 即时自定义对象
原标题:c# instantiating custom objects
  • 时间:2012-05-25 00:53:17
  •  标签:
  • c#

Hey so far I have and I m getting a null pointer error in my for loop anybody know why? thanks

这里就是错误消息

对象引用未设置为对象实例。

    board = new BoardSquare[15][];

    String boardHtml = "";

     for (int i = 0; i < 15; i++) {
            for (int k = 0; k < 15; k++) {
                //if (board[i][k] == null)
                    board[i][k] = new BoardSquare(i, k);
                boardHtml += board[i][k].getHtml();//null pointer error here
            }
        }

     /**
     * A BoardSquare is a square on the FiveInARow Board
     */
    public class BoardSquare {
        private Boolean avail; //is this square open to a piece
        private String color;//the color of the square when taken
        private int x, y; //the position of the square on the board

        /**
         * creates a basic boardSquare
         */
        public BoardSquare(int x, int y) {
            avail = true;
            this.x = x;
            this.y = y;
            color = "red";//now added (thanks)
        }

        /**
         * returns the html form of this square
         */
        public String getHtml(){
            String html = "";

            html = "<div x= " + x + "  y= " + y + "  class= " + (avail ? "available" : color) + " ></div>";

            return html;
        }

        /**
         * if true, sets color to red
         * if false, sets color to green
         */
        public void takeSquare(Boolean red){
            if(red)
                color = "red";
            else 
                color = "green";
        }
    }
问题回答

字符串字段 < 强度 > 彩色 < /强度 > 是否即时化? 似乎不像 。

那是你的纽尔。

 html = "<div x= " + x + "  y= " + y + "  class= " + (avail ? "available" : color) + " ></div>";

构造器应该是这样的:

public BoardSquare(int x, int y) {
            avail = true;
            this.x = x;
            this.y = y;
            Color = "white";
        }

您正在创建一个 < a href= > 。 http:// msdn. microsoft.com/ en- us/library/2s05feca. aspx" rel = “ nofollow” > 数组 < / a > (又称 jloadtals 数组), 我并不认为这是您想要的。 您可能正在寻找一个 < a href= > 。 http:// msdn. microsoft. com/ en- us/library/2yd9wz4% 28v=vs. 71% 29. aspx" rel=“ nofolvection” > 多层面 < / a > 数组 :

BoardSquare[,] board = new BoardSquare[15,15];

然后,当你们分给它的时候,

board[i,k] = new BoardSquare(i, k);
boardHtml += board[i,k].getHtml();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签