Results 1 to 3 of 3
  1. #1
    jacksons5 is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Post java.lang.NullPointerException for Jlabel

    Hi All,

    In my GUI Window, I have given user name, password Textfields and Login and cancel buttons, I have also added a Jlabel which needs to show login failed when the credentials fail.

    My program is working fine when user enters correct credentials but when user enters wrong username or password it throws

    "java.lang.NullPointerException " error on my Status Jlabel text fields. Please advise. Thanks in advance !


    Java Code:
     package com.teamspace.login;
    
    import com.teamspace.mainwindow.*;
    import com.teamspace.iff.*;
    import com.teamspace.service.UserService;
    import com.teamspace.mainwindow.MainWindow;
    import javax.swing.*;
    import javax.swing.border.EtchedBorder;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Login implements ActionListener{
    	
    	private JLabel lblUserName;
        private JFrame frame;
    	private JTextField textField;
    	private JPasswordField passwordField;
    	private JLabel lblStatus;
    	private JPanel panel_2;
    
    
    	
    	/** This is the login class which the users logs in and 
    	 * gets the appropriate window from User or Leader class
    	 */
    	public Login() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{
    		
    		frame = new JFrame();
    		frame.setVisible(true);
    		
    		frame.getContentPane().setBackground(new Color(102, 205, 170));
    		frame.setBounds(100, 100, 594, 476);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().setLayout(null);
    		
    		JPanel panel = new JPanel();
    		panel.setBorder(new EtchedBorder(EtchedBorder.RAISED, new Color(0, 0, 0), null));
    		panel.setBackground(Color.LIGHT_GRAY);
    		panel.setBounds(79, 115, 270, 120);
    		frame.getContentPane().add(panel);
    		panel.setLayout(null);
    		
    		JLabel lblUserName = new JLabel("Username");
    		lblUserName.setFont(new Font("Utsaah", Font.PLAIN, 22));
    		lblUserName.setBounds(10, 11, 75, 31);
    		panel.add(lblUserName);
    		
    		textField = new JTextField();
    		textField.setToolTipText("enter your global id");
    		textField.setBounds(113, 8, 86, 20);
    		panel.add(textField);
    		textField.setColumns(10);
    		
    		JLabel lblPassword = new JLabel("Password");
    		lblPassword.setFont(new Font("Utsaah", Font.ITALIC, 22));
    		lblPassword.setBounds(10, 69, 75, 17);
    		panel.add(lblPassword);
    		
    		passwordField = new JPasswordField();
    		passwordField.setBounds(113, 69, 104, 20);
    		panel.add(passwordField);
    		
    		JPanel panel_1 = new JPanel();
    		panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(0, 0, 0), null));
    		panel_1.setBackground(Color.LIGHT_GRAY);
    		panel_1.setBounds(262, 324, 270, 52);
    		frame.getContentPane().add(panel_1);
    		
    		JButton btnNewButton = new JButton("Login");
    		btnNewButton.addActionListener(this);
    		btnNewButton.setFont(new Font("Viner Hand ITC", Font.PLAIN, 11));
    		
    		panel_1.add(btnNewButton);
    		
    		JButton btnCancel = new JButton("Cancel");
    		btnCancel.addActionListener(this);
    		
    		btnCancel.setFont(new Font("Viner Hand ITC", Font.PLAIN, 11));
          	panel_1.add(btnCancel);
    		
    		JLabel lblNewLabel = new JLabel("Computer Operations Team Space");
    		lblNewLabel.setForeground(Color.DARK_GRAY);
    		lblNewLabel.setFont(new Font("Vivaldi", Font.PLAIN, 30));
    		lblNewLabel.setBounds(25, 26, 378, 43);
    		frame.getContentPane().add(lblNewLabel);
    		
    		JPanel panel_2 = new JPanel();
    		panel_2.setBackground(Color.LIGHT_GRAY);
    		panel_2.setBorder(new EtchedBorder(EtchedBorder.LOWERED, Color.BLACK, null));
    		panel_2.setBounds(262, 393, 292, 33);
    
    		
    		JLabel lblStatus = new JLabel("status");
    		lblStatus.setToolTipText("Login Status is displayed here");
    		lblStatus.setFont(new Font("Utsaah", Font.ITALIC, 23));
    		panel_2.add(lblStatus);
    	
    		frame.getContentPane().add(panel_2);	    
    	    
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		//String s3;
    		
    		//s3 = lblStatus.getText();
    		//System.out.print(s3);
    		String s1;
    		String s2;
    		int i=0;
    		
    		s1 = textField.getText();
    		s2 = passwordField.getText();
    		
    		UserService object =new UserService();
    		i = object.userService(s1, s2);
    		if(i==1 || i==2){
    		frame.dispose();
    			try {
    				MainWindow reference = new MainWindow();
    			} catch (Exception e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    		}else lblStatus.setText("logon"); 
    		
    		
    	}
    
    		
    }

    detailed error message :

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.teamspace.login.Login.actionPerformed(Login.ja va:130)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown 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.mouseRe leased(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(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn 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$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: java.lang.NullPointerException for Jlabel

    look at line 97. To where are you assigning the JLabel object which you are creating?

    - to the class member

    or

    - to a local variable

    ?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jacksons5 is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: java.lang.NullPointerException for Jlabel

    Quote Originally Posted by gimbal2 View Post
    look at line 97. To where are you assigning the JLabel object which you are creating?

    - to the class member

    or

    - to a local variable

    ?
    Oops !!!..... Got it..... Thanks verymuch gimbal2 :)

Similar Threads

  1. Java.lang.NullPointerException at
    By bmbsage in forum Advanced Java
    Replies: 2
    Last Post: 05-24-2012, 10:38 AM
  2. Java.Lang.NullPointerException, but I am not sure why???
    By buildakicker in forum New To Java
    Replies: 22
    Last Post: 07-21-2011, 01:20 AM
  3. java.lang.NullPointerException
    By alaa in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 06:21 PM
  4. Replies: 15
    Last Post: 01-13-2011, 06:28 PM
  5. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 06:02 AM

Tags for this Thread

Posting Permissions

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