Results 1 to 10 of 10
  1. #1
    danpotter is offline Member
    Join Date
    May 2011
    Posts
    43
    Rep Power
    0

    Exclamation Nullpointer exception in my code..please help me.....

    Well..i know java basics..but new to code something new...so please help me...im getting error

    Exception in Thread "main" java.lang.nullpointerException
    at lms.<init> (lms.java27)
    at lms.main (lms.java76)


    actually my main aim is to make a sample library management system and the member can go to management system[frame 2] only if the given username and password are same...if not a show a message[i.e,.label] will be displayed on the frame "Login Failed Please check..."..ofcourse..i still didnt write any code for frame 2 ..,but still within frame 1 only im getting this null pointer error...

    please help me..and also explain me why the error occured and how u removed that..pls...

    code is:-
    Java Code:
    //Library Management System//
    
    import java.awt.*;
    import java.awt.event.*;
    class lms extends Frame implements ActionListener{
    	TextField name,pswd;
    	Label lbl1,lbl2,lbl3,lbl4;
    	Button b1;
    	lms(){
    	System.out.println("Welcome to the Online Library Management System");
    	lbl1=new Label("Name : ",Label.LEFT);
    	lbl2=new Label("Password : ",Label.LEFT);
    	lbl3=new Label("Welcome To Library Management System",Label.CENTER);
    
    		name=new TextField(20);
    		pswd=new TextField(20);
    		pswd.setEchoChar('*');
    		b1=new Button("OK");
    		name.setBackground(Color.pink);
    		name.setForeground(Color.blue);
    		Font f=new Font("Arial",Font.BOLD,15);
    		Font z=new Font("Arial",Font.ITALIC+Font.BOLD,15);
    		name.setFont(f);
    		lbl1.setFont(f);
    		lbl2.setFont(f);
    		lbl3.setFont(z);
    		lbl4.setFont(f);
    		pswd.setFont(f);
    		b1.setFont(f);
    		
    		lbl1.setBounds(50,200,80,20);
    		lbl2.setBounds(50,250,80,20);
    		lbl3.setBounds(150,80,350,100);
    		lbl4.setBounds(150,80,350,100);
    		name.setBounds(150,200,120,25);
    		pswd.setBounds(250,350,100,35);
    		setLayout(null);
    		
    	
    		b1.setFont(f);
    		
    		b1.setBounds(200,300,50,30);
    		
    	
    		b1.addActionListener(this);
    		
    		add(lbl3);
    		add(lbl1);
    		add(name);
    		add(lbl2);
    		add(pswd);
    		add(b1);
    		add(lbl4);
    		setSize(1000,1000);
    		setTitle("LIBRARY MANAGEMENT SYSTEM");
    		setVisible(true);
    
    		addWindowListener(new WindowAdapter(){
    			public void windowClosing(WindowEvent e){
    				System.exit(0);
    			}
    		});	
    	
    	}
    
    	public void actionPerformed(ActionEvent ae){
                    String s1=name.getText();
    			String s2=pswd.getText();		
    if(ae.getSource()==b1 && s1.equals(s2)){
    			System.out.println("success");}
    else{
    lbl4=new Label("Login Failed Please check...",Label.LEFT);}
    		
    }
    	public static void main(String args[]){
    lms l1= new lms();
    	}
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Nullpointer exception in my code..please help me.....

    Exception in Thread "main" java.lang.nullpointerException
    at lms.<init> (lms.java27)

    Line 27 = lbl4.setFont(f);
    you realize yourself the error now?

    lbl1=new Label("Name : ",Label.LEFT);
    lbl2=new Label("Password : ",Label.LEFT);
    lbl3=new Label("Welcome To Library Management System",Label.CENTER);
    ...
    there is some line missing :-)

  3. #3
    danpotter is offline Member
    Join Date
    May 2011
    Posts
    43
    Rep Power
    0

    Default Re: Nullpointer exception in my code..please help me.....

    but i gave code in else block
    else{
    lbl4=new Label("Login Failed Please check...",Label.LEFT);}

    }

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Nullpointer exception in my code..please help me.....

    Yes but the line 34 is invoked previously - and at this time, its still null!

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Nullpointer exception in my code..please help me.....

    As eRaaa says, until that else block executes then lbl4 is null.
    Hence your NullPointerException.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    danpotter is offline Member
    Join Date
    May 2011
    Posts
    43
    Rep Power
    0

    Default Re: Nullpointer exception in my code..please help me.....

    so what i exactly need to do now???what modification?? how can i display warning mssge in frame when else condition occur??
    Last edited by danpotter; 02-23-2012 at 06:23 PM.

  7. #7
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Nullpointer exception in my code..please help me.....

    I'll give you a good council : learn the basics before programming gui applications! scnr!

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Nullpointer exception in my code..please help me.....

    Create a blank label at the same place as the others, then set the text when there's an error.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    danpotter is offline Member
    Join Date
    May 2011
    Posts
    43
    Rep Power
    0

    Default Re: Nullpointer exception in my code..please help me.....

    ok..got u,.. :D

  10. #10
    danpotter is offline Member
    Join Date
    May 2011
    Posts
    43
    Rep Power
    0

    Default Re: Nullpointer exception in my code..please help me.....

    @tolls problem solved thnx dude... :)

Similar Threads

  1. Nullpointer exception
    By naveen516 in forum New To Java
    Replies: 2
    Last Post: 12-19-2011, 04:36 AM
  2. Nullpointer Exception???
    By kipcorn91 in forum AWT / Swing
    Replies: 5
    Last Post: 10-28-2010, 11:19 PM
  3. NullPointer exception
    By bdario1 in forum New To Java
    Replies: 15
    Last Post: 03-17-2010, 04:44 AM
  4. nullpointer exception in jsp
    By fiero in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 11-07-2008, 01:44 PM
  5. NullPointer Exception
    By Preethi in forum New To Java
    Replies: 8
    Last Post: 02-06-2008, 03:40 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
  •