Solved

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

Posted on 2003-11-13
3
850 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 20 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java: anonymous class 4 55
Print Rhino Java Array in Javascript 1 59
ArrayList Adding/settign data 3 39
Java regex 5 22
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

710 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