Results 1 to 10 of 10
- 02-23-2012, 05:59 PM #1
Member
- Join Date
- May 2011
- Posts
- 42
- Rep Power
- 0
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(); } }
- 02-23-2012, 06:03 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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 :-)
- 02-23-2012, 06:07 PM #3
Member
- Join Date
- May 2011
- Posts
- 42
- Rep Power
- 0
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);}
}
- 02-23-2012, 06:09 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Nullpointer exception in my code..please help me.....
Yes but the line 34 is invoked previously - and at this time, its still null!
- 02-23-2012, 06:16 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 02-23-2012, 06:17 PM #6
Member
- Join Date
- May 2011
- Posts
- 42
- Rep Power
- 0
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.
- 02-23-2012, 06:25 PM #7
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Nullpointer exception in my code..please help me.....
I'll give you a good council : learn the basics before programming gui applications! scnr!
- 02-23-2012, 06:30 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 02-23-2012, 06:40 PM #9
Member
- Join Date
- May 2011
- Posts
- 42
- Rep Power
- 0
Re: Nullpointer exception in my code..please help me.....
ok..got u,.. :D
- 02-23-2012, 07:05 PM #10
Member
- Join Date
- May 2011
- Posts
- 42
- Rep Power
- 0
Similar Threads
-
Nullpointer exception
By naveen516 in forum New To JavaReplies: 2Last Post: 12-19-2011, 04:36 AM -
Nullpointer Exception???
By kipcorn91 in forum AWT / SwingReplies: 5Last Post: 10-28-2010, 11:19 PM -
NullPointer exception
By bdario1 in forum New To JavaReplies: 15Last Post: 03-17-2010, 04:44 AM -
nullpointer exception in jsp
By fiero in forum JavaServer Pages (JSP) and JSTLReplies: 6Last Post: 11-07-2008, 01:44 PM -
NullPointer Exception
By Preethi in forum New To JavaReplies: 8Last Post: 02-06-2008, 03:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks