Solved

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

Posted on 2003-11-13
3
843 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 20 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Same 2-dimentional loop with input_color[i][j].setEditable(false), after you have input_color elements initialized.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now