Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to add a two dimension array JTextField to a JPanel?

Posted on 2003-11-13
3
Medium Priority
?
857 Views
Last Modified: 2012-08-13
Hi, I'm writing an assignment which is almost the same as the little game "Master Mind".
I'm supposed to design a text field which contains 10 rows and 6 columns for users to input their guess (in my case, different kinds of colors), and there's a "OK" botton at the end of each row.
In order to make the checking job easier, I used "static JTextField[][] input_color = new JTextField[10][6]". and I wrote sth as follows:
public testFrame () {
        trial.setLayout(new GridLayout(10, 6));
        for(int i = 0; i < 10; i ++)
          for(int j = 0; j < 6; j ++){
            trial.add(input_color[i][j]);        
        }
}
It compiled well, but the problem came when running it. The error message said "Exception in thread "main" java.lang.NullPointerException".
How should I deal with it? What's the right way to add this stuff to the panel?
Thanks!!!
 
0
Comment
Question by:Valley617
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 60 total points
ID: 9741036
You have to initialize each row with new array and each entry with new JTextField():
for(int i = 0; i < 10; i ++){
    input_color[i] = new JTextField[6];
    for(int j = 0; j < 6; j ++){
        input_color[i][j] = new JTextField();        
    }
}
0
 

Author Comment

by:Valley617
ID: 9741430
Yup!! Thanks!!
btw, can I simply use "input_color[i].setEditable(false)" to make every element in that row is non-editable?
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 9747030
Same 2-dimentional loop with input_color[i][j].setEditable(false), after you have input_color elements initialized.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question