Sign in to follow this  
xyuri

java arrays

Recommended Posts

I am trying to learn java at the moment, and although for the most part i find it to be very easy to understand, there are some things that bite me in the ass. Maybe its just me, but anyways ... i need to create an array of objects ... well, object references as we are forced into with java ... and the following code leaves the array pointing to null ? Object[] ta = new Object[1]; ta[0] = new Country("Country4"); and i have no idea why ... anyone care to shed some light on this for me? Any help appreciated, thanks :)

Share this post


Link to post
Share on other sites
Hmm java isn't my thing but shouldn't you typecast it then to an Country object? Why don't you just make a Country[], would work then...

Share this post


Link to post
Share on other sites
Quote:
Original post by mbruenjes
If Country extends Object, you shouldn´t have any problems.

Every class extends Object in Java.

I also do not see anything wrong with this code that would ta null...

Share this post


Link to post
Share on other sites
Quote:
Original post by xyuri
Object[] ta = new Object[1];
ta[0] = new Country("Country4");

This is perfectly legal code (assuming Country is defined well). The problem must be buried elsewhere. Perhaps you have a variable hiding? Please post more of the relevant code.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sorry to do this but i found the problem ... slight oversight ...

I learned that java initiallises array's to null so thats what i thought it was, but in fact the way i was testing it (System.out.println(ta[0].toString());) was the problem ... i had disabled some properties in the Country class which the toString() function was using, and it was one of those value that was null, not the array value :-)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this