im getting into learning Java, and its slow going, but im gradually getting there with it etc, but im stumped at the mo!

ive got a project that has 2 files, 1 is the main file, and the other is a seperate class (for a starfield)

the problem is the 2nd file (a class) as whenever I run the main program, i get a java.lang.OutOfMemoryError error

my main class is this:

1

publicclassfscreenextendsJFrameimplementsKeyListener

my stars class is this:

1

publicclassstarsextendsfscreen

and for my star file I do this which worked on other projects (where the extends was Canvas etc unlike this project) I set it up like this:

1 2 3 4 5

// This is in the 'global' areastarsmystarfield;

//In the fscreen bitmystarfield = newstars();

Now as SOON as I try to do the mystarfield = new stars(); thats when i get the memory error! I cant see whats wrong with it, apart from the only thing im trying different is the extends JFrame bit as with looking at other code etc, found more ppl use that than things like extends Canvas etc (tried that in a previous project, and i had probs trying to get the keylistener working!)

sorry to sound like a complete Newbie, but thats me!

PS. If i copy the whole stars class into the main .java file and remove the class info and make them functions the program works fine!

I'm not going to tell you what's happened (but I'll give you a hint...see below), simply because it's not worth you doing any more programming until you change the way you look at your code.

You are not reading your own code. You need to: 1. get a pen + paper 2. step through your code line by line from the line that is broken 3. write down what's happening 4. keep going until you get to the point where you go "ohmygod"

Of course, 99% of people do the pen-paper part in their head .

HINT: What happens, precisely, when you instantiate ("new [something or other]") a class? You know that as well as calling the class's constructor, it FIRST calls the superclass's constructor (if it has one)...

(if you don't know what a constructor is, or what a superclass is, or that "A extends B" marks B as a superclass of A, then you need to go read the free Sun tutorials on programming java. then come back here if you still dont undrestand)

your stars class extends the fscreen class (why ??) and doesn't define a constructor of its own. So when you instantiate an fscreen object, in the constructor it tries to create a new stars object, which calls the fscreen constructor, which tries to create a new stars object, which calls the fscreen constructor which .....

well you get the picture ...eventually you run out of mem.

D.

-edit- sorry about that blahblahblah, was obviously writing this as you were replying ... -edit-

hehe I WAS wondering why whenever I ran the code my machine sounded like it was crying!

as you can probably tell from my code, im more used to been sat in front of photoshop and 3ds max rather than writing code! but I thought id expand and learn Java to get a break from the gfx stuff for a bit.

been scanning through this forum and reading up a fair bit and it is gradualyl sinking in (honest! )

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org