Results 1 to 10 of 10
- 12-11-2008, 08:49 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 28
- Rep Power
- 0
[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)
Java 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); } } } }
Java 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(); } }); } }
Java 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)); } }
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!Last edited by hendrix79; 12-15-2008 at 02:41 PM. Reason: Solved
- 12-11-2008, 10:22 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
you have declared
Java Code:public static JDesktopPane sfondo;
Java Code:public static JDesktopPane sfondo = whatever;
- 12-11-2008, 10:35 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 28
- Rep Power
- 0
Ok, solved with this:
Java Code:public static JDesktopPane sfondo = new JDesktopPane();
Thank you so muuuch! :)Last edited by hendrix79; 12-11-2008 at 10:54 PM.
- 12-12-2008, 05:36 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
Well, to tell you the truth, sfondo should not be static. Remove that keyword. and in your Login class simply do
Java Code:Sfondo sfond = new SFondo(); graficaLogin(sfondo.sfondo);
- 12-12-2008, 09:27 PM #5
Member
- Join Date
- Dec 2008
- Posts
- 28
- Rep Power
- 0
Why it shouldn't be static? It won't be never modified by any class... if i got right the "static" concept. In addiction, if i make it non-static, i get the error "Cannot make a static reference to the non-static field Sfondo.sfondo"...
- 12-13-2008, 11:30 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
If you want it to be static, then don't initialise it in the constructor. Either initialise it with the declaration, or initialise it in a static block, but not in either the constructor or an instance message.
One rule of thumb to follow, if it doesn't need to be static, it shouldn't be.
- 12-13-2008, 12:43 PM #7
Member
- Join Date
- Dec 2008
- Posts
- 28
- Rep Power
- 0
Many thanks for the tip, i'll keep it like treasure.
But, i think it should be static: "sfondo" means "background" in english, so i think that the background will never be modified by any class, and it is called by every class, i don't need to construct it everytime... but (and this is a great probability) i kinda suck at Java, so i'm quite sure that i don't got right what static really means. BTW, i'm learning it day by day, so it's great to receive tips. ;)
- 12-13-2008, 12:58 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 12-13-2008, 02:37 PM #9
Member
- Join Date
- Dec 2008
- Posts
- 28
- Rep Power
- 0
Many thanks! Really appreciated. :cool:
- 12-14-2008, 07:18 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
If you have solve the problem please mark it as solved. It's really helpful to all other members.
Similar Threads
-
NullPointerException
By Aika in forum New To JavaReplies: 8Last Post: 11-19-2008, 12:34 AM -
NullPointerException
By adeeb in forum AWT / SwingReplies: 3Last Post: 06-11-2008, 09:42 AM -
NullPointerException
By mensa in forum Java 2DReplies: 5Last Post: 05-04-2008, 12:19 AM -
NullPointerException
By ravian in forum New To JavaReplies: 2Last Post: 12-07-2007, 05:20 PM -
NullPointerException
By Feng in forum New To JavaReplies: 5Last Post: 11-24-2007, 08:51 PM
Bookmarks