problem in 2D array of TextField
I have a problem in this code.I can't display the array of Text Field.
Code Java:
import javax.swing.*;
import java.awt.*;
public class Projct extends JFrame {
int x=3;
public Projct(){
Container c=getContentPane();
c.setLayout(new BorderLayout());
JPanel p=new JPanel();
//new GridLayout(x,x)
c.add(p,BorderLayout.CENTER);
JTextField [][] array=new JTextField[x][x];
for(int i=0;i<x;i++){
for(int j=0;j<x;j++){
array[i][j].setText(" ");
p.add(array[i][j]);
}
}
int m=x;
double [][] data=new double [m][m];
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
if (i == j) {
data[i][j]=1;
array[i][j].setText("1");
array[i][j].setEnabled(false);
}
else{
data[i][j]=Double.parseDouble(array[i][j].getText());
data[j][i]=(1/data[i][j]);
}}}
}
public static void main(String[]arg){
Projct f=new Projct();
f.setTitle("AHP");
f.setVisible(true);
f.setSize(500, 500);
f.setLocation(40,60);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Re: problem in 2D array of TextField
Define can't display...does it compile? Are there any exceptions? (hint: see Common Java Mistakes)
Re: problem in 2D array of TextField
Quote:
Originally Posted by
copeg
no it doesn't compile
Exception in thread "main" java.lang.NullPointerException
at Projct.<init>(Projct.java:20)
at Projct.main(Projct.java:52)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
i don't know the order of writing the arrays
1st array is array of Text Field
2nd array is array of values that will enter by user
I want the result to be like this pic
http://www3.0zz0.com/2011/01/07/00/961803741.jpg
Re: problem in 2D array of TextField
Quote:
no it doesn't compile
Exception in thread "main" java.lang.NullPointerException
Based upon that it sure looks like it compiles...a NullPointerException is a runtime exception ;) . Did you have a look at the link I posted above? You've created an array without initializing its elements.
Re: problem in 2D array of TextField
Quote:
Originally Posted by
copeg
Based upon that it sure looks like it compiles...a NullPointerException is a runtime exception ;) . Did you have a look at the link I posted above? You've created an array without initializing its elements.
thanks for this information.
I read
Problem description: Forgetting to initialize a variable
Problem description: Array Indices/Off by 1 errors
but my problem is how can I initialize the variable of 2D array
I want blank text fields then the user only type the values of upper triangle then the program will assign this values to the inverted triangle , diagonal will be always = 1 by setTextField
I have an exam & I want to find the solve
Re: problem in 2D array of TextField
Quote:
but my problem is how can I initialize the variable of 2D array
Do you mean something like this?
Code :
String[] strings = new String[1];
int len = strings[0].length();//NullPointerException, the value strings[0] was never initialized
string[0] = new String("Testing");
len = strings[0].length();//Now it has been initialized
The same principle holds true for all objects. Nowhere in the code you posted above is there a "new JTextField()", so all the members of the array will be null until they are instantiated.
Re: problem in 2D array of TextField
Thank you very much
array[ ][ ] is work and the frame is appear , now I have to complete my code then data [ ] [ ] will work