NullPointerException
次のコードで を取得しています。コードの主な目的は、あるクラスでユーザーからの入力を取得し、それを別のクラスに渡してGridLayout
.
これが私の最初のクラスのコードです...
public class A{
int N;
JLabel label;
JPanel panel;
JButton button;
JFrame frame;
JTextField text;
public A(){
frame=new JFrame();
panel=new JPanel();
text=new JTextField(20);
label=new JLabel("Enter the number of states");
button=new JButton("Submit");
panel.add(label);
panel.add(text);
panel.add(button);
frame.add(panel,BorderLayout.CENTER);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
N=Integer.parseInt(text.getText());
B1 page=new B1(N);
page.frame.setVisible(true);
}
});
frame.setSize(320,240);
frame.pack();
frame.setVisible(true);
}
public static void main(String arg[]){
A ab=new A();
}
}
これが私の2番目のクラスのコードです...
public class B1 extends A {
JFrame frame;
JPanel pan;
JButton button;
double transition[][];
JTextField tf[][];
B1(int N){
this.N=N;
frame=new JFrame("Transition matrix");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pan=new JPanel();
pan.setLayout(new GridLayout(N+1,N));
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
tf[i][j]=new JTextField();
pan.add(tf[i][j]);
}
}
button=new JButton("Submit");
pan.add(button);
frame.add(pan);
frame.pack();
}
}