[SOLVED] I get a NullPointerException and don't know why
Hello folks!
As the subject, i get this error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
and i would like to know why.
This is my code
class Sfondo.java (a class that contains the background of the application)
Code:
public class Sfondo extends javax.swing.JFrame {
public static JDesktopPane sfondo;
private JLabel immagineAgenda;
private JLabel immagineSfondo;
public Sfondo() {
super();
setSfondo(sfondo);
}
public void setSfondo(JDesktopPane sfondo) {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
sfondo = new JDesktopPane();
getContentPane().add(sfondo, BorderLayout.CENTER);
{
immagineAgenda = new JLabel();
immagineSfondo = new JLabel();
sfondo.add(immagineSfondo);
sfondo.add(immagineAgenda);
immagineSfondo.setIcon(new ImageIcon(getClass().getClassLoader().getResource("immagini/sig.gif")));
immagineSfondo.setBounds(new Rectangle(510, 330, 400, 220));
immagineAgenda.setIcon(new ImageIcon(getClass().getClassLoader().getResource("immagini/agenda.png")));
immagineAgenda.setBounds(new Rectangle(0, 310, 315, 225));
sfondo.setForeground(new java.awt.Color(0,128,255));
sfondo.setBackground(new java.awt.Color(113,208,255));
pack();
this.setVisible(true);
this.setSize(760, 560);
this.setLocationRelativeTo(null);
}
}
}
}
This is just the Avvio.java class, which simply contains the main method that calls Login class.
Code:
package avvio;
import javax.swing.SwingUtilities;
import avvio.Login;
public class Avvio {
/**
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Login();
}
});
}
}
and, last, i have this Login.class that shows a simple label.
Code:
package avvio;
import javax.swing.JLabel;
import javax.swing.JDesktopPane;
import utilita.Sfondo;
public class Login {
protected JLabel nome_utente;
public Login() {
graficaLogin(utilita.Sfondo.sfondo);
}
private void graficaLogin(JDesktopPane sfondo) {
nome_utente = new JLabel();
sfondo.add(nome_utente);
nome_utente.setText("Nome utente");
nome_utente.setBounds(166, 86, 164, 16);
nome_utente.setFont(new java.awt.Font("Tahoma",0,20));
nome_utente.setPreferredSize(new java.awt.Dimension(164, 16));
}
}
I choosed (like a user on this forum told me to do) to pass the parameters so i would keep the background and i redraw it on every different part of my app. If anybody knows something lighter and easier, would be appreciated.
Anyway, i get a NullPointerException on line 19 of my Login.java class, which contains this
sfondo.add(nome_utente);
I think that the exception happens because sfondo not exists in memory. How can i solve this?
Thanks so much, i owe much to this forum... sorry for my bad english and for the dumb question and thanks again!