Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Window arrays

 
Derek Boring
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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic