|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Mar 2005
Città: Napoli
Messaggi: 2942
|
[JAVA] NullPointerException con JBotton
Codice :
private JButton jBotton1; // Bottoni public Mondo() { ..... javax.swing.JButton jBotton1 = new JButton(); getContentPane().add(jBotton1); jBotton1.setText("Tira il dado..."); jBotton1.setBounds(650,80, 130,20); jBotton1.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.LOWERED, new java.awt.Color(51, 51, 255), new java.awt.Color(102, 102, 255), new java.awt.Color(102, 102, 255), new java.awt.Color(51, 51, 255))); jBotton1.addMouseListener(new java.awt.event.MouseAdapter() { // Evento click del pulsante public void mouseClicked(java.awt.event.MouseEvent evt) { PressionejBotton1(evt); } }); ..... } private void PressionejBotton1(java.awt.event.MouseEvent evt) { ...... jBotton1.setVisible(false); <-- Qua mi da errore ...... } Questo errore : Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Game.Mondo.PressionejBotton1(Mondo.java:184) .... Perchè ? Non posso cambiare lo stato del bottone ?! Esiste un metodo alternativo per cambiare lo stato a non_visibile quando lo premo ? |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Dec 2002
Messaggi: 418
|
Ho creato un bottone in un JFrame con il tuo stesso codice e funziona perfettamente...clicco ed il bottone sparisce.
Il problema quindi temo sia altrove...forse da qualche parte nel codice jBotton1 viene puntato a null a causa di qualche operazione? |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: May 2005
Città: Roma
Messaggi: 7938
|
metti un file java, in modo che vediamo tutto il codice.
__________________
My gaming placement |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Sep 2004
Città: Ovunque
Messaggi: 5804
|
Prova ad usare un semplice ActionListener invece del MouseListener.
__________________
:: Le mie trattative :: LOL >> http://www.hwupgrade.it/forum/showth...9#post31430289 |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 12:57.




















