473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Jagged array problem

Hi, i am haveing problems with this code

namespace test
{
class Program
{
class MyClass{
public int x;
int y;
}
static void Main(string[] args)
{
MyClass[][] myclass=new MyClass[4][];
myclass[0]=new MyClass[5];

myclass[0][0].x=10; // error here
}
}
}
I am getting this error

Exception System.NullReferenceException was thrown in debuggee:
Object reference not set to an instance of an object.

What am i doing wrong?
May 16 '07 #1
3 1753
On Wed, 16 May 2007 06:15:10 GMT, "azz131" <bi********@ntlworld.com>
wrote:
>Hi, i am haveing problems with this code

namespace test
{
class Program
{
class MyClass{
public int x;
int y;
}
static void Main(string[] args)
{
MyClass[][] myclass=new MyClass[4][];
myclass[0]=new MyClass[5];

myclass[0][0].x=10; // error here
}
}
}
I am getting this error

Exception System.NullReferenceException was thrown in debuggee:
Object reference not set to an instance of an object.

What am i doing wrong?

MyClass[][] myclass=new MyClass[4][];
myclass[0]=new MyClass[5];
myclass[0][0] = new MyClass(); <----
myclass[0][0].x=10; // no error here

--
Ludwig
http://www.coders-lab.be
May 16 '07 #2
"azz131" <bi********@ntlworld.comwrote in message
news:Oj******************@newsfe6-win.ntli.net...
Hi, i am haveing problems with this code
[...]
What am i doing wrong?
You have not created any instance of MyClass, only of an array of
MyClass. Here is the fixed code:

namespace test
{
class Program
{
class MyClass{
public int x;
int y;
}
static void Main(string[] args)
{
MyClass[][] myclass=new MyClass[4][];
myclass[0]=new MyClass[5];
myclass[0][0]=new MyClass(); //<--You missed this
myclass[0][0].x=10; // error here
}
}
}

May 16 '07 #3
On May 16, 8:15 am, "azz131" <binarys...@ntlworld.comwrote:
Hi, i am haveing problems with this code

namespace test
{
class Program
{
class MyClass{
public int x;
int y;
}
static void Main(string[] args)
{
MyClass[][] myclass=new MyClass[4][];
myclass[0]=new MyClass[5];

myclass[0][0].x=10; // error here
}
}}

I am getting this error

Exception System.NullReferenceException was thrown in debuggee:
Object reference not set to an instance of an object.

What am i doing wrong?
When you create an array, its elements are initialized to the default
value of the type of elements. For reference types, such as your
class, it is null. It would be zero for any numeric type.

What you must do is to create objects to store in your array:

myclass[0][0] = new MyClass();
myclass[0][0].x = 5;

--
Marcin Hoppe
http://devlicio.us/blogs/marcin_hoppe/

May 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jannis Linxweiler | last post by:
Hi NG! I have a problem with my unsafe code. I'd like to get a pointer on a jagged array like this: double arr = new double; for (int i....); arr = new double;
1
by: James dean | last post by:
I done a test and i really do not know the reason why a jagged array who has the same number of elements as a multidimensional array is faster here is my test. I assign a value and do a small...
3
by: Ravi Singh (UCSD) | last post by:
Hello all I am trying to use jagged and multi-dimensional arrays in C++. In C# these work fine // for jagged arrays string jaggedArray = new string ; //for multidimensional arrays string...
1
by: xllx.relient.xllx | last post by:
Hi, I have two questions: 1.)Is it true that an rectangular array is really just an single dimensional array that lets itself be treated as a multi-dimensional array? For example the...
4
by: Alan Foxmore | last post by:
Hi everyone, I'm new to C# and I was hoping I could get some clarification on the syntax for jagged and multidimensional arrays. Here is my question: The following syntax is correct for...
2
by: tiberiu.motoc | last post by:
Hi. I've asked this question on the MSDN forums, but I've had no replies. I'm really stuck, so I'm gonna try my luck here. I have a Java web service which contains a simple function; the...
5
by: TS | last post by:
is there some code somewhere that does this? i have a jagged array that is not jagged, it has an equal number of rows and columns in each array so it should convert but want to get the code to do...
0
by: =?Utf-8?B?TWljaGFlbA==?= | last post by:
Hello, i have a problem with jagged arrays and a webservice. I try to call a webservice with an array of a class that contains jagged arrays. Here is an example: class MyClass{ string...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
3
by: pinkfloydfan | last post by:
Hi there I have a C# program that produces a lot of data that I currently store in a jagged array and I have a couple of questions that I was hoping someone might shed some light on for me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.