Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Out of Bounds Exception returns and the eradication thereof.

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Programmers,

In the following code, which as it stands - with forward slashes included -the program compiles and runs okay, but when the first of the comment slashes are removed, I start getting 'Out of Bounds Exception' returns on compiler. I have included subsequent lines to show where I want to take the program.

Can anyone out there advise me as to where I am going wrong?
Thanks for your help.

Yours Simon, Code follows:-
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An out of bounds exception displays the file and line number of the location -- along with the index that caused the exception.

Instead of giving us a program with the offending line commentted out, maybe it would be better to give us the exception? And maybe the location in the program where the exception is happening (as pointed to by the exception)?

Henry
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if this is the line, when uncommented, causes the error
//UsersNames[i] = usersNames[i].length();

what do you think is happening in these prior lines (hint: look at 'i')
int i = 0;
...
int [] UsersNames = new int [i];
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Michael Dunn, Henry Wong and all other programmers,

I have recoded the program thus :-

Which gives to the input of my full name 'Simon Angus Evans' the following:-


This, I was hoping, would provide a basis for rearranging previous program'FT'(see 'MouseEventListener' thread) so that the input for Name would establish the number of rows, ie the JPanels comprising the GUI, and each of the returns for 'userNames[i].length()'
would provide the JLabels that would be arranged - by the instanced JPanel's object's gridLayout's (args of value 1, by usersNames[i]length()).

Anyway, I have tried to join the two programs, and thus came up with the following code :-

However it seems that I've got the instancing of the JPanel wrong - I haven't tried instancing JPanel in a loop before- but anyhow its the only line the compiler rejects, giving :-

Can several JPanels be instanced in a loop, in the same manner as JLabels can?
Is this a viable way to get input to establish the Gui ie: get the number of names input to instance its constituent JPanels and get the length of
the sucessive names input to instance the number of JLabels ?
All advice gratefully received,
Yours, hoping to hear,
Simon.
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can several JPanels be instanced in a loop, in the same manner as JLabels can?



Well, you can declare a JPanel array like so...

JPanel p[] = new JPanel[jpsize];

And then assign an element like so...

p[i] = new JPanel(new GridLayout(1,userNames[i].length()));

As for this....

JPanel p[i] = new JPanel(new GridLayout(1,userNames[i].length()));

I am not sure what this is, but you can't declare an element of an array (assuming that is what you are attempting, as it is not valid syntax).

Henry
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Henry Wong,
Thank you for your reply.
I realise I had been 'jumping the gun' not only by trying to instance an array element without pre-declaring an array of Jpanel objects, but also by (trying to) create a program which instances a gui using input defined multiple panels and multiple labels therein, when I ought perhaps first try to create a program using predefined values for a gui of multiple panels and their labels.
So I've tried recoding the program FT,thus deriving FT2 with the use of multiple panels;-

but having declared the JPanel array,
compiler rejects the second suggested line of code(in the loop), whereby I'd hoped to assign elements to the declared JPanel array- as you'd
suggested in your response.
Hence compiler returns:-


Where am I going wrong, please?
Thanks for reading.
Yours
Simon.
 
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have already been told about this. Don't declare a member of an array. You have already called p a JPanel[] array, so the compiler knows p[i] is a JPanel. Don't try to tell it the same thing twice like that.
[ October 01, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and please indent your code as well as using code tags; it is quite difficult to read.
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Campbell Ritchie, Henry Wong,

Thank you for your advice.

I haven't encountered a program in any of my texts using a gui that uses multiple JPanel objects, so for me this is treading new ground.

I will put gaps between the methods of programs, if that renders code more legible, I will do whatever is required.

Having followed your advice,I have still encountered difficulty, with the JPanel, upon compiling 'FT2'.
I modified the program (which runs okay): FT's lines from :

to FT2'S :-


With the intention of creating a gui of 6 JPanel objects of 1 by 7 JLabels, instead of a gui of 1 JPanel object of
6 by 7 JLabels.
However compiler doesn't like the last line, returning: -


I was wondering if the syntax changes from when adding a JLabel object to a JPanel object, to when adding a JLabel object to a JPanel object as an element in an array?

As said I wanted to create a gui using multiple Jpanels of a predetermined amount such as this -before trying to create a program which would do the same, but using an amount of JPanels and JLabels determined
by users input.

Thank you for your help, and for reading.

Yours,
Hoping to hear
Simon.
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


As with your previous error, this error also has nothing to do with JPanels -- it is also an error with your use of the language.

In this case, as stated by the error message, your variable i is out of scope. Notice that the line in question is not in the for loop, hence, i is out of scope.

Henry
 
Campbell Ritchie
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code isn't much easier to read; the extra spaces make only a little difference, but what you want is indentation.

By the time you get to the add call for the labels, "i" has gone out of scope. That is why it doesn't compile. You need to go back to basics; there is a description of local variables on this Java Tutorials page and it tells you briefly what the scope of a variable is.
 
Campbell Ritchie
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, looking at that page again, it seems rather less useful about scope than I thought at first. Sorry.
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:

In this case, as stated by the error message, your variable i is out of scope.



Readability goes a long way in debugging. The longer I've been programming the more I find that it isn't a waste of time, I think you will find it to be the same.

For example, Scope problems are easily seen if proper indention is a priority.
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Henry Wong and Campbell Ritchie,

Thank you for your advice.

I put curly brackets around the statement, as you advise.
Upon compiling and running the gui thus produced is of only one row of seven JLabels, from that I take it that only one
of the panels was instanced in the loop discussed.
I was wondering why it doesn't go through the entire loop, ie in:-

i was wondering Whats stopping the instanced JPanels in the first loop, either from adding JLabels to the entire
six panel object in the second loop, and if that isn't the problem, whats stopping in:-


The JPanel objects thus made from being put into the JFrame object ?
Perhaps that is not the problem though, as the compiler gives the following output:-

The null pointer is given at the object.method call of new FT2().buildGUI() near the end of the program. This is the
same syntax as used, and worked alright in program FT, ie: newFT().buildGUI();

When I close the gui window the console reverts to: -


This is the code to FT2, if the fault is not in any of the above lines:-


Could you give me some idea of where the program is going wrong?

Thank you for reading.

Yours
Simon.
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The null pointer exception is occuring at / near line 66 of the FT2.java file. So... Can you show use the buildGUI() method, and point out exactly which line is line 66?

Henry
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Henry Wong,

The method buildGui() is in the program FT2, as given in the last post, situated from the seventh line down to the closing curly brace before the
public static void main (String args[] ) method.

The line 66 in FT2 seems to correspond with the line :-
label[i].addMouseListener(mousie);
- but I don't think that is the line compiler is referring to - as the line it quotes (re:"at FT2.buildGUI(FT2.java:66)) is on line 75,which it goes on to mention in the next line(re:"at FT2$3.run(FT2.java:75") and which is in the run()method, which in turn is in the public static void main (String args[] ) method.

This line 75 that the compilers NullPointerException refers to is in fact the last full line of code in the program, at the bottom, which reads:-
[CODE]
new FT2().buildGUI();
[\CODE]
As said, I am not sure why compiler takes exception to this, as with program FT, the same syntax ie: new FT().buildGUI(); compiled and ran okay -
FT2 does compile and run - but only generates a gui of one panel - not the full six panel gui that I wanted to get.

Thank you for your help.

Yours

Simon
[ October 02, 2008: Message edited by: Simon Evans ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the line
label[i].addMouseListener(mousie);

so, basic debugging, add to the looop
System.out.println(i);
and you'll see it falls over when i > 6

then you go back to your creation of the labels
label[i] = new JLabel(images[0]);

the problem is you create the labels 0 to 6, but you do it 6 times,
never creating a label[] higher than element 6.

so, the i of
label[i] = new JLabel(images[0]);
has to include the value of j (to go from 7 to < 42)

I'll leave it for you to work out your algorithm
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As said, I am not sure why compiler takes exception to this, as with program FT, the same syntax ie: new FT().buildGUI(); compiled and ran okay -
FT2 does compile and run - but only generates a gui of one panel - not the full six panel gui that I wanted to get.



You are not reading it right. The listing is a stack. Line 75 is not causing a null pointer exception, it is the line that calls the method that gets you to line 66. In other words, the first method is the one that causes the exception, the second method is the one that called the first, the third method is the one that called the second, and so on...

The line 66 in FT2 seems to correspond with the line :-
label[i].addMouseListener(mousie);
- but I don't think that is the line compiler is referring to...



You can't just rationalize it away. You have to prove it one way or the other. Either it is null or it is not. Add a system out call for label, the label[i] element, mousie, etc. to prove that it is null or not. Be careful, while doing this though, because if label is null, then label[i] won't work, etc.

Henry
[ October 02, 2008: Message edited by: Henry Wong ]
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Michael Dunn and Henry Wong,

Thank you for your advice.

I have made the JLabel array object an element of a double array, so's it can includethe incrementing value of j aswell as i in the nested loop.

I also made line 66 a nested loop so's all labels will have a mouse listener, hence



Was that what you mean when you write :-


so, the i of
label[i] = new JLabel(images[0]);
has to include the value of j (to go from 7 to < 42)


Anyway, the compiler doesn't return any 'Null pointer exception' now.
On the other hand it still produces a gui of only one panel of seven labels - so it isn't going
through the entire loop.
Incidentally I apologise for misreading the compiler - so it was the addMouseListener()-line 66- and
the 'line 75' was just referring to the line that called it ie: FT2().buildGui() - I understand now.

Is the algorithm above right?

I enclose the readjusted program below :-


If the alterations are alright, I was wondering what's stopping the program from running as it ought?

Thank you for reading, and for your assistance.

Yours
Simon
 
Campbell Ritchie
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please find a text editor which supports automatic indentation; since you use Windows you can use NotePad++ or NotePad2 (I usually use the ++ version if on Windows). Set it to convert tabs to spaces and tab = 4 spaces, and follow an accepted indenting convention. Your half-indented code is no easier to read.
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Campbell Ritchie,
I have downloaded and installed Notepad 2 Version 5.0.2 (of the Chameleon icon).

I opened program 'FT2' in it, and copied and pasted it to here but it looked the same as before.

Could you tell me what menu items in Notepad 2 will adjust the tab spacing and indentation as referred to in your post, so it will the transfigure program code as required?

Thank you for your help.

Yours
Simon
 
Campbell Ritchie
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notepad2 and Notepad++ will help put indentation into new code, but I am afraid they won't sort out copied and pasted code. For that you can either do it by hand, or use a full-blown IDE. In Eclipse you can use ctrl-A ctrl-I to correct indentation for the whole class file. Don't try it if the file is more than 1000 lines, though.

What you do is save the file with the name and .java extension very early in the process.
Then write code, put a {, then "enter" and } (possibly with a //end something comment). Then you go back one line and hit enter. If you are lucky you will be 4 spaces to the right immediately. If not push tab; your lines will remain indented at the same level until you push tab again, or backspace at the beginning of a line.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> If the alterations are alright,...

no, they're not.

JLabel label[][] = new JLabel[7][42];

you now have 7 x 42 labels = 294 labels

you can do it with a 2D array, but it's not required, so stick with what you had.

this is the problem area
(changed so it just prints out a number)

you want to print out 0 to 41

so, when the loop starts, j = 0
then, if you just had it to print 'i', it would print 0 to 6
now the outer loop loops, j = 1, and i starts again at 0
so how do you make it print out '7' (i = 0, j = 1)

[EDIT]
just in case you think the answer to the above is just adding 7,
expand your thinking to all the other numbers.

you only need a single System.out.println() in the loop
print out '7' (i = 0, j = 1)
print out '8' (i = 1, j = 1)
print out '14' (i = 0, j = 2)
etc.
do you see a relationship in the above?
[ October 04, 2008: Message edited by: Michael Dunn ]
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Campbell Ritchie,

I take it I am stuck with Notepad for program FT.
I will try to stick to the formats of Eckel, Horton, Savitch etc. for this program.

Dear Michael Dunn,

I can see no way (with my limited knowledge) to get int i to increment beyond 6 in any loop that permits no higher incrementation of i than 6, so in view of what you say:



you only need a single System.out.println() in the loop
print out '7' (i = 0, j = 1)
print out '8' (i = 1, j = 1)
print out '14' (i = 0, j = 2)
etc.
do you see a relationship in the above?



I can only get for instance in a nested loop's current valuation of say i at 0 and j at 2 to
output 14 if the 14 is of another int name, say 'k' whereby it is not affected by loop's scope.

Hence in the following program the above is achievable:-

gives:-

Where the i is at 0 and the j at 1 ie: first output on second row, output is '7' and 2nd output on second row is '8' and first output on third row is
'14' as suggested by you.

Is this the right approach?

Hoping to hear,
Thank you for your help.
Yours
Simon
[ October 04, 2008: Message edited by: Simon Evans ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Is this the right approach?

well, if it works OK, and you understand it, go with it.

but for other programs you may need to understand the other approach
print out '0' (i = 0, j = 0)
print out '7' (i = 0, j = 1)
print out '14' (i = 0, j = 2)
print out '21' (i = 0, j = 3)

the relationship here is a multiplication of 'j'
do you see it now?

so the action of the inner loop is
label[i + (j * ??)] = new JLabel(images[0]);
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Michael Dunn,

Thank you for your advice.
I take it that the ?? would be the number 7 ?
So that the statement reads label[i + (j*7] = new JLabel(images[0]);
Thus for the first loop label[i +(j*7] would read only the incrementation of 'i' as j*7 when j is at 0 will give only 0.
On the second loop [i +(j*7] will be first: [0 + (1*7)] = 7, then [1 + (1*7)] = 8, and so forth (brilliant, aint I?)
If that is the unknown quantity, then I'll use that - as in fact using int 'k' was chucking up new problems.
I hope I am right.
Thank you for reading.
Yours
Simon.
 
Campbell Ritchie
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Download another text editor, and use that to open your old .java files. You see you get different colours for keywords etc, and if you put your mouse cursor on a ([{ it highlights the paired )]}. You might have to go through the text by hand with the tab key to get the indentation in, but there might be options to use ctrl-T or ctrl-shift-T to increase or reduce indentation. You can see how much easier it is to read the code, and you can see when your i goes out of scope.
 
Campbell Ritchie
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have finally found my Eckel and I see he uses a sort of compressed indentation with {} in. That turns up in quite a few books, and is good for saving paper, but on screen you don't need to save paper, so use lots of space. There is a possible convention here on JavaRanch, or a different convention from Sun. [There are some bits in Sun which nobody seems to understand; just ignore them.]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> I take it that the ?? would be the number 7 ?
> ...
> I hope I am right.

you shouldn't 'hope', but test and confirm, to be certain.

if the modified code prints out 0 to 41 (test several times),
then be confident that it is correct.


the next thing to work on is rewriting your program to reduce the number of for loops.
you should only need 2 'for loops' (and even they can be combined)
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Michael Dunn,
I have tried out the program:-

which gives:-


However, seemingly this still gives in the following code in
program FT2 a gui of one panel with seven labels - instead
of six panels with seven labels each.

ie:-

or alternatively:-


Have I got it right? If so why isn't it giving the gui I want?

Hope you can help.
Thank you for reading.
Yours
Simon.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Have I got it right?

the part/s that have been giving you problems to date, look OK.

> If so why isn't it giving the gui I want?

next part of the learning curve

default layout for a JFrame is BorderLayout
default 'position' for BorderLayout is CENTER
BorderLayout only allows one component per position (nesting is OK)

so, what's happening is you do, in fact, add each panel to the frame,
but each new 'addition' effectively knocks out the prior one, so you
end up with only the final panel showing.

fix is to change the frame's BorderLayout to something more suitable.
have a go, see if you can get it working OK.
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No offense, but is it that hard to format code? And I don't mean finding an editor that does it automatically. I mean manually formatting it...



Isn't this much more readable?

One day, if you become a professional programmer, you will actually have to work with other developers. And if you think we are complaining now, it will be nothing compared to what your colleagues will say during a code review.

Henry
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Michael Dunn,
Thank you for your help.
I have tried changing the layout of Frame object f, as you suggested.

This changes the gui a little - the seven labels appear at the bottom of the
gui upon running program FT2,instead of at the middle, but the rest of the gui is still blank.
Is this anything like what you had in mind?
I will continue try tinkering with it, but this is best or least bad I have come up with in the interim.

Dear Henry Wong,
Thank you for your advice regarding code format. I will try the format you suggest, as

it seems to clarify the difficulties that braces usually present.

Yours
Hoping to hear,
Simon.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my last post said the label j*7 stuff looked OK.
actually, there's an error in it (when adding the label to the panel)


should work OK now.
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Michael Dunn,
I tried what you suggested on top of the code I put to you last - and it works - Hoorah - much thanks,
I am going to try and adopt this program to take user input as in the NameLayout program further up.
I will post it here when it is finished.
Again thank you for your kind help.
Bye for now,
Yours
Simon
[ October 09, 2008: Message edited by: Simon Evans ]
 
Simon Evans
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Campbell Ritchie, Michael Dunn, Henry Wong and other programmers

I have made a program based on the previous one FT2, and NameLayout, to create a gui of a number of panels of labels, the number of those panels and of labels being decided by users input of how many names they have, and how many letters they have in each name.

The program FT3 compiles and runs, but has a runtime 'out of bounds exception' perhaps the code and compiler response would show the current situation more clearly :-

gives the JOption pane requesting number of names user has, so I put in 3, and press Enter.
Then JOption pane comes back requesting those names, so I key in 'Simon Angus Evans' and press Enter.
Then a runtime error occurs, and compiler crashes, after giving:-


The line 32 is: for (int j = 0; j< Names; j++)
The int Names would've been intialised to 3 by my input, and j would'nt increment beyond 3, it'd cover 0,1,2, ie: three places before ending the outer and inner loops. It should'nt go to 4 - therefore I was wondering why it quoted 4 as the oob exception ?

I hope you can offer me some help with this, I will continue to tinker with this program in the meantime, but this is how it is performing at the present, and as said, I don't get why it should be going out of bounds.
Yours
Hoping to hear,
Yours
Simon
[ October 10, 2008: Message edited by: Simon Evans ]
 
Campbell Ritchie
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I tried it I got the Exception at a different place

[Campbell@queeg applications]$ javac FT3.java
[Campbell@queeg applications]$ java FT3
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2
at FT3.buildGUI(FT3.java:33)
at FT3$3.run(FT3.java:97)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
[Campbell@queeg applications]$

The line appears to be
for (int i = 0; i< usersNames[i].length(); i++)

Look through that very carefully and work out which length you want and which you are getting.
 
Paul Yule
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe your logic is off at this spot:


The inner loop is currently changing which name.length it looks at each iteration of the inner loop instead of each iteration of the outter loop. Perhaps your second loop was suppose to be using j.length instead of
i.length since i isn't related to the number of Names you have but is instead related to the length each individual name.

and something really nitpicky and each person probably has their own opinions but when I saw the variable 'Names' I immediately thought of an Enum that had Name values in them. Personally I stay away from declaring variables uppercase so that you can tell when you have static Classes by glancing at them. I would have probably named it numOfNames.
[ October 10, 2008: Message edited by: Paul Yule ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, you're all over the place with this lot, and you've reverted to your original problem



using your example given
Names = 3
usernames = Simon Angus Evans
the above becomes
for (int j = 0; j< Names; j++) to
for (int j = 0; j< 3; j++)
and
for (int i = 0; i< usersNames[i].length(); i++) to
for (int i = 0; i< "Simon".length(); i++) which is
for (int i = 0; i< 5; i++)

so, when i is > 2
p[j] = new JPanel(new GridLayout(1,usersNames[i].length()));
there is no 4th element, and all you're doing is re-creating p[j] at
each iteration of the for loop - your original problem.

when you finally get this working, this bit
usersNames[i] = JOptionPane.showInputDialog("Dear user, Please enter your names");
multiple times is annoying to users. use a multi-line optionPane, so that
all names can be entered at the same time
 
Campbell Ritchie
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better still to get users to enter full name straight into the JOptionPane, split with the String#split method and use the array to work out how many names they have.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic