• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Window arrays

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm trying to make an application that has a toolbox. The toolbox has a file menu in which there are(or will be) the standard commands of New, Open, Save, etc...

My problem is that I want to be able to have multiple document windows open and visible at a time. I'm familar with VB in which you would use an object array, however, I tried this technique in Java using the following line of code, and the command prompt filled with text while the app did nothing:



Any ideas?
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Derek,

Welcome to JavaRanch!

That's all pretty close; the only problem is that declaring an array variable -- or any variable that's not a primitive type like int, double, float, short, char -- creates only a reference to an object, not the object itself. Your array variable is "null", meaning it points to no array at all. The "console filling with text" is stack dumps resulting from a NullPointerException being thrown when you try to add an element to that non-existent array object.

You have to actually create the array:

public ImageFrame[] canvas = new ImageFrame[10];

That creates an array that would hold ImageFrames. Java arrays have a fixed size. If you need something that might grow over time, look at the java.util.ArrayList class.
 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought a bout doing that, but logically it seems like the array would then be filled when it's initialized. I wasn't sure if that was the case or not.

Thanks for clearing it up for me.
    Bookmark Topic Watch Topic
  • New Topic