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:-
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();
}
}
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 :-)
Re: Nullpointer exception in my code..please help me.....
but i gave code in else block
Quote:
else{
lbl4=new Label("Login Failed Please check...",Label.LEFT);}
}
Re: Nullpointer exception in my code..please help me.....
Yes but the line 34 is invoked previously - and at this time, its still null!
Re: Nullpointer exception in my code..please help me.....
As eRaaa says, until that else block executes then lbl4 is null.
Hence your NullPointerException.
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??
Re: Nullpointer exception in my code..please help me.....
I'll give you a good council : learn the basics before programming gui applications! scnr!
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.
Re: Nullpointer exception in my code..please help me.....
Re: Nullpointer exception in my code..please help me.....
@tolls problem solved thnx dude... :)