Невозможно вызвать прослушиватель действий, поскольку кнопка имеет нулевое исключение.JAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Невозможно вызвать прослушиватель действий, поскольку кнопка имеет нулевое исключение.

Сообщение 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




Подробнее здесь: https://stackoverflow.com/questions/792 ... -exception
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»