Я пытаюсь открыть Jframe с панелью вкладок из другого jframe, но нажимаю первую кнопку, но когда я пытаюсь это сделать, я получаю следующую ошибку:
Исключение в потоке «AWT-EventQueue-0» java. lang.NullPointerException: невозможно вызвать «javax.swing.JButton.addActionListener(java.awt.event.ActionListener)», потому что «this.jButton1» имеет значение null.
Это код: ```package com.mycompany.taxiservice;
import javax.swing.JOptionPane ;
публичный класс HomePage расширяет javax.swing.JFrame {
/**
* Creates new form HomePage
*/
public HomePage() {
initComponents();
}
@SuppressWarnings("unchecked")
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMaximumSize(new java.awt.Dimension(600, 600));
setPreferredSize(new java.awt.Dimension(600, 600));
setSize(new java.awt.Dimension(600, 600));
jLabel1.setBackground(new java.awt.Color(204, 204, 204));
jLabel1.setFont(new java.awt.Font("Rockwell Extra Bold", 2, 36)); // NOI18N
jLabel1.setText("Taxi Driver Service");
jButton1.setFont(new java.awt.Font("Segoe UI", 1, 24)); // NOI18N
jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel2.setFont(new java.awt.Font("Segoe UI", 2, 14)); // NOI18N
jLabel2.setText("If you do not have an account please register by clicking the button below.");
jButton2.setFont(new java.awt.Font("Segoe UI", 1, 24)); // NOI18N
jButton2.setText("Register");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(207, 207, 207)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(313, 313, 313)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(158, 158, 158)
.addComponent(jLabel2))
.addGroup(layout.createSequentialGroup()
.addGap(313, 313, 313)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(176, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(86, 86, 86)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(79, Short.MAX_VALUE))
);
setSize(new java.awt.Dimension(816, 509));
setLocationRelativeTo(null);
}//
//This method is triggered when jButton1 (Login button) is clicked
//It opens the Login page and disposes the current window (closes it)
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Login login = new Login();
login.setVisible(true);
dispose();
}
//This method is triggered when jButton2 (Create New User button) is clicked
//It prompts the user to select their user type (Customer or Driver)
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String[] options = {"Customer", "Driver"};
int choice = JOptionPane.showOptionDialog(this, "Please select your user type", "User Type",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
if (choice == 0) { // Customer
CreateNewUser newUser = new CreateNewUser("Customer");
newUser.setVisible(true);
} else if (choice != 1) { } else {
// Driver
CreateNewUser newUser = new CreateNewUser("Driver");
newUser.setVisible(true);
}
dispose();
}
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
// End of variables declaration
Подробнее здесь: https://stackoverflow.com/questions/792 ... -exception
Невозможно вызвать прослушиватель действий, поскольку кнопка имеет нулевое исключение. ⇐ JAVA
Программисты JAVA общаются здесь
1732750347
Anonymous
Я пытаюсь открыть Jframe с панелью вкладок из другого jframe, но нажимаю первую кнопку, но когда я пытаюсь это сделать, я получаю следующую ошибку:
Исключение в потоке «AWT-EventQueue-0» java. lang.NullPointerException: невозможно вызвать «javax.swing.JButton.addActionListener(java.awt.event.ActionListener)», потому что «this.jButton1» имеет значение null.
Это код: ```package com.mycompany.taxiservice;
import javax.swing.JOptionPane ;
публичный класс HomePage расширяет javax.swing.JFrame {
/**
* Creates new form HomePage
*/
public HomePage() {
initComponents();
}
@SuppressWarnings("unchecked")
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMaximumSize(new java.awt.Dimension(600, 600));
setPreferredSize(new java.awt.Dimension(600, 600));
setSize(new java.awt.Dimension(600, 600));
jLabel1.setBackground(new java.awt.Color(204, 204, 204));
jLabel1.setFont(new java.awt.Font("Rockwell Extra Bold", 2, 36)); // NOI18N
jLabel1.setText("Taxi Driver Service");
jButton1.setFont(new java.awt.Font("Segoe UI", 1, 24)); // NOI18N
jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel2.setFont(new java.awt.Font("Segoe UI", 2, 14)); // NOI18N
jLabel2.setText("If you do not have an account please register by clicking the button below.");
jButton2.setFont(new java.awt.Font("Segoe UI", 1, 24)); // NOI18N
jButton2.setText("Register");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(207, 207, 207)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(313, 313, 313)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(158, 158, 158)
.addComponent(jLabel2))
.addGroup(layout.createSequentialGroup()
.addGap(313, 313, 313)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(176, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(86, 86, 86)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(79, Short.MAX_VALUE))
);
setSize(new java.awt.Dimension(816, 509));
setLocationRelativeTo(null);
}//
//This method is triggered when jButton1 (Login button) is clicked
//It opens the Login page and disposes the current window (closes it)
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Login login = new Login();
login.setVisible(true);
dispose();
}
//This method is triggered when jButton2 (Create New User button) is clicked
//It prompts the user to select their user type (Customer or Driver)
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String[] options = {"Customer", "Driver"};
int choice = JOptionPane.showOptionDialog(this, "Please select your user type", "User Type",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
if (choice == 0) { // Customer
CreateNewUser newUser = new CreateNewUser("Customer");
newUser.setVisible(true);
} else if (choice != 1) { } else {
// Driver
CreateNewUser newUser = new CreateNewUser("Driver");
newUser.setVisible(true);
}
dispose();
}
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
// End of variables declaration
Подробнее здесь: [url]https://stackoverflow.com/questions/79232169/cannot-invoke-action-listener-because-button-is-null-exception[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия