Results 1 to 10 of 10
- 12-11-2008, 07: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)
This is just the Avvio.java class, which simply contains the main method that calls Login class.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); } } } }
and, last, i have this Login.class that shows a simple label.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(); } }); } }
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.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 01:41 PM. Reason: Solved
- 12-11-2008, 09:22 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
you have declared
but you haven't defined it. I.E.Java Code:public static JDesktopPane sfondo;
Java Code:public static JDesktopPane sfondo = whatever;
- 12-11-2008, 09:35 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 28
- Rep Power
- 0
Ok, solved with this:
and modifying my Login() function by adding a call to new Sfondo();Java Code:public static JDesktopPane sfondo = new JDesktopPane();
Thank you so muuuch! :)Last edited by hendrix79; 12-11-2008 at 09:54 PM.
- 12-12-2008, 04:36 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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, 08: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, 10:30 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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, 11:43 AM #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, 11:58 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-13-2008, 01:37 PM #9
Member
- Join Date
- Dec 2008
- Posts
- 28
- Rep Power
- 0
Many thanks! Really appreciated. :cool:
- 12-14-2008, 06:18 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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-18-2008, 11:34 PM -
NullPointerException
By adeeb in forum AWT / SwingReplies: 3Last Post: 06-11-2008, 08:42 AM -
NullPointerException
By mensa in forum Java 2DReplies: 5Last Post: 05-03-2008, 11:19 PM -
NullPointerException
By ravian in forum New To JavaReplies: 2Last Post: 12-07-2007, 04:20 PM -
NullPointerException
By Feng in forum New To JavaReplies: 5Last Post: 11-24-2007, 07:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks