Results 1 to 3 of 3
  1. #1
    mathidioticz is offline Member
    Join Date
    Jan 2012
    Posts
    48
    Rep Power
    0

    Default NullPointerException Problems

    Hi, i am new to java and having some errors when running the program.

    Here is the code:
    Java Code:
    package OOPJ.ui;
    
    
    import javax.swing.JPanel;
    import java.awt.Color;
    import java.awt.Dimension;
    import javax.swing.JLabel;
    import java.awt.Rectangle;
    import javax.swing.ImageIcon;
    import javax.swing.SwingConstants;
    import java.awt.Point;
    import javax.swing.JButton;
    
    import Signup.entity.myApp;
    import Signup.entity.SignUp;
    import OOPJ.ui.LoginPanel;
    import OOPJ.ui.MainFrame;
    
    
    
    public class MenuPanel extends JPanel {
    
    	private static final long serialVersionUID = 1L;
    
    	private JLabel jLabelMenu = null;
    	private JButton jButtonProfile = null;
    	private JButton jButton1 = null;
    	private JButton jButton2 = null;
    	private JButton jButton3 = null;
    	private JButton jButton4 = null;
    	private JLabel jLabelLogo = null;
    	private JButton jButton5 = null;
    	private JLabel jLabelHello = null;
    	private MainFrame myFrame = null;
    	/**
    	 * This is the default constructor
    	 */
    	public MenuPanel() {
    		super();
    		initialize();
    	
    	}
    	
    	String username = null;  
    	public MenuPanel(MainFrame f) {
    		this();
    		myFrame = f; 
    		
    	// find out  the current login user
    	SignUp curUser = myFrame.getApp().getCurLoginUser();
    	
    	// set the name
    	jLabelHello.setText("Hello " + curUser.getUsername() );  //line 53: this is where the error are at. 
    	
    	}
    	/**
    	 * This method initializes this
    	 * 
    	 * @return void
    	 */
    	private void initialize() {
    		jLabelHello = new JLabel();
    		jLabelHello.setBounds(new Rectangle(16, 87, 80, 27));
    		jLabelHello.setText("Hello,");
    		jLabelLogo = new JLabel();
    		jLabelLogo.setText("JLabel");
    		jLabelLogo.setSize(new Dimension(136, 56));
    		jLabelLogo.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/logo.jpg")));
    		jLabelLogo.setLocation(new Point(12, 16));
    		jLabelMenu = new JLabel();
    		jLabelMenu.setBounds(new Rectangle(145, 37, 451, 108));
    		jLabelMenu.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/menutitle.png")));
    		jLabelMenu.setHorizontalAlignment(SwingConstants.LEADING);
    		jLabelMenu.setHorizontalTextPosition(SwingConstants.CENTER);
    		jLabelMenu.setText("");
    		this.setSize(700, 500);
    		this.setLayout(null);
    		this.setBackground(Color.white);
    		this.add(jLabelMenu, null);
    		this.add(getJButtonProfile(), null);
    		this.add(getJButton1(), null);
    		this.add(getJButton2(), null);
    		this.add(getJButton3(), null);
    		this.add(jLabelLogo, null);
    		this.add(getJButton4(), null);
    		this.add(getJButton5(), null);
    		this.add(jLabelHello, null);
    	}
    
    	/**
    	 * This method initializes jButtonProfile	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getJButtonProfile() {
    		if (jButtonProfile == null) {
    			jButtonProfile = new JButton();
    			jButtonProfile.setBounds(new Rectangle(22, 439, 120, 37));
    			jButtonProfile.setHorizontalAlignment(SwingConstants.LEFT);
    			jButtonProfile.setHorizontalTextPosition(SwingConstants.LEFT);
    			jButtonProfile.setText("Back To Profile");
    			jButtonProfile.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					JPanel panel = new UserProfile(myFrame);
    					myFrame.getContentPane().removeAll();
    					myFrame.getContentPane().add(panel);
    					myFrame.getContentPane().validate();
    					myFrame.getContentPane().repaint();
    				}
    			});
    		}
    		return jButtonProfile;
    	}
    
    	/**
    	 * This method initializes jButton1	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getJButton1() {
    		if (jButton1 == null) {
    			jButton1 = new JButton();
    			jButton1.setLocation(new Point(16, 282));
    			jButton1.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/Picture1.png")));
    			jButton1.setSize(new Dimension(180, 105));
    			jButton1.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					JPanel panel = new ForumPanel(myFrame); 
    					myFrame.getContentPane().removeAll(); 
    					myFrame.getContentPane().add(panel);
    					myFrame.getContentPane().validate(); 
    					myFrame.getContentPane().repaint(); 
    				}
    			});
    		}
    		return jButton1;
    	}
    
    	/**
    	 * This method initializes jButton2	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getJButton2() {
    		if (jButton2 == null) {
    			jButton2 = new JButton();
    			jButton2.setLocation(new Point(16, 160));
    			jButton2.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/Picture2.png")));
    			jButton2.setBackground(Color.white);
    			jButton2.setSize(new Dimension(180, 105));
    			jButton2.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					JPanel panel = new CancerPanel(myFrame); 
    					myFrame.getContentPane().removeAll(); 
    					myFrame.getContentPane().add(panel);
    					myFrame.getContentPane().validate(); 
    					myFrame.getContentPane().repaint(); 
    				}
    			});
    		}
    		return jButton2;
    	}
    
    	/**
    	 * This method initializes jButton3	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getJButton3() {
    		if (jButton3 == null) {
    			jButton3 = new JButton();
    			jButton3.setLocation(new Point(203, 158));
    			jButton3.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/journey.jpg")));
    			jButton3.setSize(new Dimension(294, 227));
    			jButton3.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					JPanel panel = new KBSPanel(myFrame); 
    					myFrame.getContentPane().removeAll(); 
    					myFrame.getContentPane().add(panel);
    					myFrame.getContentPane().validate(); 
    					myFrame.getContentPane().repaint(); 
    				}
    			});
    		}
    		return jButton3;
    	}
    
    	/**
    	 * This method initializes jButton4	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getJButton4() {
    		if (jButton4 == null) {
    			jButton4 = new JButton();
    			jButton4.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/Picture4.png")));
    			jButton4.setLocation(new Point(504, 160));
    			jButton4.setSize(new Dimension(180, 105));
    			jButton4.setBackground(Color.white);
    			jButton4.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					JPanel panel = new OrganicPanel(myFrame); 
    					myFrame.getContentPane().removeAll(); 
    					myFrame.getContentPane().add(panel);
    					myFrame.getContentPane().validate(); 
    					myFrame.getContentPane().repaint(); 
    				}
    			});
    		}
    		return jButton4;
    	}
    
    	/**
    	 * This method initializes jButton5	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getJButton5() {
    		if (jButton5 == null) {
    			jButton5 = new JButton();
    			jButton5.setLocation(new Point(506, 282));
    			jButton5.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/quiz.jpg")));
    			jButton5.setSize(new Dimension(180, 105));
    			jButton5.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					JPanel panel = new QuizPanel(myFrame); 
    					myFrame.getContentPane().removeAll(); 
    					myFrame.getContentPane().add(panel);
    					myFrame.getContentPane().validate(); 
    					myFrame.getContentPane().repaint(); 
    				}
    			});
    		}
    		return jButton5;
    	}
    
    }
    And here is the error message :
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at OOPJ.ui.MenuPanel.<init>(MenuPanel.java:53)
    	at OOPJ.ui.UserProfile$1.actionPerformed(UserProfile.java:83)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)


    How come there is an nullpointerexception problem?
    Please save me!
    Last edited by pbrockway2; 01-20-2012 at 06:56 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: NullPointerException Problems

    Hi mathidioticz, welcome to the forums!

    When you post code, use the "code" tags: put [code] at the start of the code and [/code] at the end. That way the code will be properly indented and readable when it appears here. Also it is a good idea to use spaces to indent code, especially when it is going to appear within a web browser as these can render the tabs quite wide.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: NullPointerException Problems

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at OOPJ.ui.MenuPanel.<init>(MenuPanel.java:53)
        at OOPJ.ui.UserProfile$1.actionPerformed(UserProfile.java:83)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    You read these things from the top down until you hit a line of your code which is a good place to start looking for the cause of the problem. (Which you have correctly identified as line 53). The rest of the stack trace might be useful if you want to know what was going on just before the error occurred.

    A NullPointerException occurs when you have a variable or other expression that has the value null and you try and use it as if it had a non null value. Typically this is because you use the . operator to dereference something that is null, or you use the [] operator on an array which is null:

    Java Code:
    int[] arr;
    arr[42];                 // bad! arr is null
    String foo;
    int len = foo.length(); // bad! foo is null
    Line 53 says:

    Java Code:
    jLabelHello.setText("Hello " + curUser.getUsername() );
    The two candidates for being null are jLabelHello and curUser. You can check which is null by using System.out.println()

    Java Code:
    System.out.println("About to set the name");
    System.out.println("jLabelHello=" + jLabelHello);
    System.out.println("curUser=" + curUser);
    jLabelHello.setText("Hello " + curUser.getUsername() );
    Once you have found which is null go back through your code to where you thought the variable had been given a nonnull value and find out why that didn't happen.

Similar Threads

  1. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 12:12 PM
  2. NullPointerException
    By GPB in forum New To Java
    Replies: 8
    Last Post: 02-21-2010, 04:05 PM
  3. NullPointerException
    By Juuno in forum New To Java
    Replies: 1
    Last Post: 02-11-2010, 06:43 PM
  4. Why do I get a NullPointerException?
    By nessa203 in forum New To Java
    Replies: 2
    Last Post: 01-12-2010, 05:02 PM
  5. Why do I get a NullPointerException?
    By nessa203 in forum New To Java
    Replies: 5
    Last Post: 01-07-2010, 02:14 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
  •