• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initializing and (later) accessing arrays

 
Greenhorn
Posts: 15
Eclipse IDE Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//I have a major problem with initializing and (later) accessing arrays. I start with an applet declaration; e.g.,





 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this line



where do you actually assign the references to refer to arrays?
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch.

Please use code tags as follows:



Looks better doesn't it?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which IDE are you using?
 
Jim Anderson
Greenhorn
Posts: 15
Eclipse IDE Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot 3 lines in my earlier post. My init() file should read:

public void init(){
//... followed by array initialization
int delta_x[] = new int[numDeltas];
int delta_y[] = new int[numDeltas];
int alpha[] = new int[numDeltas];

int delta_x[]= new int[numDeltas];
int delta_y[]= new int[numDeltas];
int alpha[]= new int[numDeltas];

for(int kk=0;kk<numDeltas;kk++){
delta_x[kk]=1;
delta_y[kk]=1;
alpha[kk]=1;
myRan = Math.random();
if(myRan>0.5){delta_x[kk]=-1;}
myRan = Math.random();
if(myRan>0.5){delta_y[kk]=-1;}
// end kk loop
}
// end init()
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please learn to UseCodeTags (<-click) when posting source code.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe your issue is shadowing. You have the three arrays declared as instance variables. But in the init method, you create local variables with the same name. The instance variables are still null.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote: . . . Looks better doesn't it?

Not a lot better. Not even when I added code tags to the original post.

That code had not been correctly indented; there is too much writing crammed into too small a space. Please spread it out, Jim Anderson, so we can actually read it.
 
Yup, yup, yup. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic