Ошибка java.lang.NullPointerException
Формулировка задачи:
Подскажите пожалуйста, как исправить
Вот код
Листинг программы
- package test;
- import javax.swing.JButton;
- import javax.swing.JTextField;
- public class Test {
- private JButton b;
- private JTextField t;
- private MyFrame f;
- public static void main(String[] args) {
- Test test = new Test();
- test.bb();
- test.tt();
- test.ff();
- }
- private void bb(){
- b=new JButton("{ff");
- ab();
- }
- private void tt(){
- t = new JTextField(10);
- }
- private void ff(){
- f=new MyFrame("ee", 130, 100);
- f.add(t);
- f.add(b);
- }
- private void ab(){
- Lis l = new Lis(b,t);
- b.addActionListener(l);
- }
- }
- package test;
- import java.awt.FlowLayout;
- import javax.swing.JFrame;
- public class MyFrame extends JFrame{
- public MyFrame(String title, int wigth, int higth ){
- super.setTitle(title);
- super.setSize(wigth, higth);
- super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- super.setLayout(new FlowLayout());
- super.setLocationRelativeTo(null);
- super.setVisible(true);
- }
- }
- // в слушателе не хочет получить текст, хотя кнопку обрабатывает
- package test;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JTextField;
- public class Lis implements ActionListener{
- private JButton b;
- private JTextField t;
- public Lis (JButton bb, JTextField tt){
- b=bb;
- t=tt;
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(e.getSource() == b){
- System.out.println(t.getText());
- }
- }
- }
Решение задачи: «Ошибка java.lang.NullPointerException»
textual
Листинг программы
- Lis l = new Lis(b,t);
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д