Array of JLabels

K

KDawg44

Hi,

I want to create an array of JLabels like this:

private JLabel lblBoard[][] = new JLabel[5][5];

iterate through the array and set some properties like this:

for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
lblBoard[j].setFont(new java.awt.Font("Dialog", 1,
18));
lblBoard
[j].setBorder(javax.swing.BorderFactory.createLineBorder(new
java.awt.Color(0, 0, 0)));
}
}


However, I am getting an error everytime I run this app.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at knightgui.MainJFrame.initComponentsMyWay(MainJFrame.java:
40)
at knightgui.MainJFrame.<init>(MainJFrame.java:31)
at knightgui.MainJFrame$2.run(MainJFrame.java:219)
at
java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:227)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:603)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:276)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:201)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:191)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:186)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:178)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:
139)


Line 40 is this line:
lblBoard[j].setFont(new java.awt.Font("Dialog", 1,
18));

in the above snippet.

Its saying that there is nothing in the array. I am sure I am
instantiating it incorrectly but not exactly sure how.

Thanks for your help.

Kevin
 
K

KDawg44

Hi,

I want to create an array of JLabels like this:

private JLabel lblBoard[][] = new JLabel[5][5];

iterate through the array and set some properties like this:

for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
lblBoard[j].setFont(new java.awt.Font("Dialog", 1,
18));
lblBoard
[j].setBorder(javax.swing.BorderFactory.createLineBorder(new
java.awt.Color(0, 0, 0)));
}
}

However, I am getting an error everytime I run this app.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at knightgui.MainJFrame.initComponentsMyWay(MainJFrame.java:
40)
at knightgui.MainJFrame.<init>(MainJFrame.java:31)
at knightgui.MainJFrame$2.run(MainJFrame.java:219)
at
java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:227)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:603)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:276)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:201)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:191)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:186)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:178)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:
139)

Line 40 is this line:
lblBoard[j].setFont(new java.awt.Font("Dialog", 1,
18));

in the above snippet.

Its saying that there is nothing in the array. I am sure I am
instantiating it incorrectly but not exactly sure how.

Thanks for your help.

Kevin


Nevermind. I am an idiot. Never actually instantiated the
labels....

Thanks.
 
A

Andrea Francia

KDawg44 said:
Hi,

I want to create an array of JLabels like this:

private JLabel lblBoard[][] = new JLabel[5][5];

iterate through the array and set some properties like this:

for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
lblBoard[j].setFont(new java.awt.Font("Dialog", 1,
18));
lblBoard
[j].setBorder(javax.swing.BorderFactory.createLineBorder(new
java.awt.Color(0, 0, 0)));
}
}


However, I am getting an error everytime I run this app.

Its saying that there is nothing in the array. I am sure I am
instantiating it incorrectly but not exactly sure how.


You instantatied the array not the array contents.
With the line
private JLabel lblBoard[][] = new JLabel[5][5];
you create a set of 25 reference holder that can hold a reference to a
JLabel but you are not constructing JLabels.

In C++ a line like that will call the default constructor but in Java it
just initialize the reference to null.

To solve the problem you simply needs to put:
lblBoard[j] = new JLabel(....);
at the beginning of the for cicle body
 
R

RedGrittyBrick

KDawg44 said:
Hi,

I want to create an array of JLabels like this:

private JLabel lblBoard[][] = new JLabel[5][5];

iterate through the array and set some properties like this:

for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {


lblBoard[j] = new JLabel();
lblBoard[j].setFont(new java.awt.Font("Dialog", 1,
18));
lblBoard
[j].setBorder(javax.swing.BorderFactory.createLineBorder(new
java.awt.Color(0, 0, 0)));
}
}


However, I am getting an error everytime I run this app.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
snip


Its saying that there is nothing in the array. I am sure I am
instantiating it incorrectly but not exactly sure how.


You instantiated an array of 25 JLabel references that were all null,
you didn't instantiate any JLabels.
 
C

Chase Preuninger

By default all values of type object are assigned to null.

JLabel[] lblBoard = new JLabel[5][5];
for(int i = 0; i < lblBoard.length; i++)
{
for(int j = 0; j < lblBoard.length; j++)
{
lblBoard[j] = new JLabel(...);
}
}
 
C

Chase Preuninger

I was thinking that how could someone advanced enough to make a gui
make such a stupid mistake.
 
L

Lew

Chase said:
I was thinking that how could someone advanced enough to make a gui
make such a stupid mistake.

No one is ever too advanced for stupid mistakes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top