🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: aus.computers.java
Article #848

Re: Trouble accessing values in a 2D array

#848
From: Paul Murray
Date: Thu, 11 May 2000 00:00
61 lines
3103 bytes
> I have a class which stores doubles in a 2D array.
> I declare these:
>
> private double [][] distance;
> private double [][] coords;
>
> I read data from a file placing it into coords array:

Uh huh, let me guess - you get a null pointer exception, right?

> Now, why do I get a Null Pointer Exception when
> I have already stored the values into coords array?
> This is very strange!

You have two problems. Firstly, you are declaring a new coords variable
inside the enterDoubleXY method, which hides the class variable. Change
this:
>         double [ ][ ] coords = new double[10][2];

to this
>         coords = new double[10][2];


Second problem: you are a C programmer. Repeat after me: "java does not have
mutidimensional arrays".

This:
> private double [][] coords;

Declares a reference to an array of references to arrays of double. It is
initialised to null.

This:
>         coords = new double[10][2];

Sets coord to point to a new array *of 10 references*. Each of those 10
references is null. The "2" is ignored. You will need to set each of those
10 refernces to a new array of 2 doubles if you want to use the coords
variable like this.

On a more general note, you might be better off using the Collections API
rather than explicitly putting things in arrays. If you do that, you may as
well use a class for a floating-point XY coord. This allows you to attach
methods etc. java.util has integer Point and Dimension classes - why not
model your floating-point classes on those?

And are you going for a full "best route" solution to the traveling salesman
problem, or an algorithim to find a "reasonably good" route? You know that
it's impossible to solve it in better than exp(n) time on any sort of
computer (as far as anyone knows) - or if you do find a quicker solution,
the whole world of cryptography will alter, and the FBI will whisk you away?

If you are using real map data (latitude and longtitude) for the city
co-ords, remember the earth is not flat. You might be better off typing in a
"distance between major cities" chart that you get on some roadmaps,
although you may want to keep the coords if you are displaying the routes
graphically.

--------------------------------------------------------------
http://www.users.bigpond.com/pmurray
ICQ: 26066755
Technical Details
Message-ID: <K3uS4.8857$X4.17863@newsfeeds.bigpond.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <39189457.3527647@news.melbpc.org.au> <8fbd5b$kbd$1@nnrp1.deja.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10d6db,1002517bab792639
X-Google-Attributes: gid10d6db,public
From: "Paul Murray" <pmurray@nospam.bigpond.com>
Subject: Re: Trouble accessing values in a 2D array
Date: 2000/05/11
Message-ID: <K3uS4.8857$X4.17863@newsfeeds.bigpond.com>
X-Deja-AN: 621970130
References: <39189457.3527647@news.melbpc.org.au> <8fbd5b$kbd$1@nnrp1.deja.com>
X-Priority: 3
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600
X-Trace: newsfeeds.bigpond.com 958033258 139.134.124.8 (Thu, 11 May 2000 18:20:58 EST)
Organization: Telstra BigPond Internet Services (http://www.bigpond.com)
X-MSMail-Priority: Normal
NNTP-Posting-Date: Thu, 11 May 2000 18:20:58 EST
Newsgroups: aus.computers.java