Results 1 to 10 of 10
  1. #1
    hendrix79 is offline Member
    Join Date
    Dec 2008
    Posts
    28
    Rep Power
    0

    Default [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);
    				}
    			}
    		
    		}
    }
    This is just the Avvio.java class, which simply contains the main method that calls Login class.

    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();
    				
    			}
    		});
    	}
    }
    and, last, i have this Login.class that shows a simple label.

    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));
    		
    	}
    
    }
    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!
    Last edited by hendrix79; 12-15-2008 at 02:41 PM. Reason: Solved

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    you have declared
    Java Code:
    public static JDesktopPane sfondo;
    but you haven't defined it. I.E.
    Java Code:
    public static JDesktopPane sfondo = whatever;

  3. #3
    hendrix79 is offline Member
    Join Date
    Dec 2008
    Posts
    28
    Rep Power
    0

    Default

    Ok, solved with this:

    Java Code:
    public static JDesktopPane sfondo = new JDesktopPane();
    and modifying my Login() function by adding a call to new Sfondo();

    Thank you so muuuch! :)
    Last edited by hendrix79; 12-11-2008 at 10:54 PM.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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);

  5. #5
    hendrix79 is offline Member
    Join Date
    Dec 2008
    Posts
    28
    Rep Power
    0

    Default

    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"...

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  7. #7
    hendrix79 is offline Member
    Join Date
    Dec 2008
    Posts
    28
    Rep Power
    0

    Default

    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. ;)

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  9. #9
    hendrix79 is offline Member
    Join Date
    Dec 2008
    Posts
    28
    Rep Power
    0

    Default

    Many thanks! Really appreciated. :cool:

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. NullPointerException
    By Aika in forum New To Java
    Replies: 8
    Last Post: 11-19-2008, 12:34 AM
  2. NullPointerException
    By adeeb in forum AWT / Swing
    Replies: 3
    Last Post: 06-11-2008, 09:42 AM
  3. NullPointerException
    By mensa in forum Java 2D
    Replies: 5
    Last Post: 05-04-2008, 12:19 AM
  4. NullPointerException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 05:20 PM
  5. NullPointerException
    By Feng in forum New To Java
    Replies: 5
    Last Post: 11-24-2007, 08:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •