"NullPointerException". (was Re: ArrayOutOfbounds, how come?)  
Author Message
V S Rawat





PostPosted: 2004-6-19 1:22:00 Top

java-programmer, "NullPointerException". (was Re: ArrayOutOfbounds, how come?) Mark Haase wrote:
>
> What exception, exactly?? Your subject says
> "ArrayOutOfBounds" but the catch block is
> "NullPointerException".

you got it right. it is indeed "NullPointerException".
mea culpa.

> You shouldn't be getting ArrayOutOfBoundsException, but
> your code will generate a NPE everytime because of a
> noobie mistake that you're making.

what is noobie?

> Below, you initialize a new array of JButton references,
> but each reference is still null.
>
>> button = new JButton[8][14];
>
> You need to put an object in each one of them before you
> start making calls like:
>
>
>> button[gridY1][gridX1].setText(button_text);
>
> How can you call setText on an object that doesn't exist?
> You can't. The runtime lets you know by throwing NPE.
>
> Do this:
>
> for (int i=0; i<8; i++)
> for (int j=0; j<14; j++)
> button[i][j] = new JButton();
>
> In the future, be more specific when you say "I have an
> error!! help!"..Name the exact error.

done.

no way. After changing to the following, it compiled pretty
well, but is still giving npe at = line.
-------------
JButton[][] button;
-----------------
for (int gridY1 = 0; gridY1 < d_rows; gridY1++) {
for (int gridX1 = 0; gridX1 < d_columns;
gridX1++) {
button[gridY1][gridX1] = new JButton();
}
}
-------------
when I changed the above block according to what was
suggested by Mr John in another thread of Mr Mike:
-------------
JButton[][] button = new JButton[d_rows][d_columns];
for (int gridY1 = 0; gridY1 < d_rows; gridY1++) {
for (int gridX1 = 0; gridX1 < d_columns;
gridX1++) {
button[gridY1][gridX1] = new JButton();
}
}
-------------
it again got compiled flawlessly, but I got npe at a further
line where I am refering a button for the first time:
------------
button[gridY1][gridX1].setText(button_text);
------------

what next?

-rawat


 
Roedy Green





PostPosted: 2004-6-19 3:56:00 Top

java-programmer >> "NullPointerException". (was Re: ArrayOutOfbounds, how come?) On Fri, 18 Jun 2004 22:52:21 +0530, V S Rawat <email***@***.com>
wrote or quoted :

>what is noobie?

see http://mindprod.com/jgloss/newbie.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.