Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-11-13
3
Medium Priority
?
859 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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

877 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