Home arrow static arrow Java Programming [Archive] - There is a problem in my application......................
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - There is a problem in my application......................
This topic has 58 replies on 4 pages.    1 | 2 | 3 | 4 | Next »

Posts:49
Registered: 6/17/04
There is a problem in my application......................  
Jul 15, 2004 10:04 AM



 
The following code is a Login Class code that I mentioned Earlier thanks for guiding me through but the problem is this that the code wouldn't go on after the isUser() method in this code.
(Remember I use JBuilder 3.5 Professional for building Java Applications.)
Please Help me out with this application...
package dailyExpense; import java.io.*;import java.io.Serializable;import java.awt.*;import javax.swing.*;import java.awt.event.*; public class LoginExpense extends JFrame {  JLabel LoginLbl = new JLabel();  //Panels  JPanel contentPane = (JPanel)this.getContentPane();  JPanel User = new JPanel();  JPanel btnPane = new JPanel();  //Other things  JLabel usrNmeLbl = new JLabel();  JTextField userName = new JTextField();  JLabel pasLbl = new JLabel();  TextField passWord = new TextField(18);  UIManager.LookAndFeelInfo looks[];  JButton newUsrbtn = new JButton();  JButton lgnbtn = new JButton();  //File  RandomAccessFile out;  File f = new File("C:/Program Files/Expense.usr");//File Name      public LoginExpense() {    enableEvents(AWTEvent.WINDOW_EVENT_MASK);    try {      jbInit();       }    catch(Exception e) {      e.printStackTrace();    }  }   private void jbInit() throws Exception {    looks = UIManager.getInstalledLookAndFeels();    contentPane.setLayout(new BorderLayout());    LoginLbl.setFont(new java.awt.Font("Serif", 1, 25));    LoginLbl.setForeground(Color.red);    LoginLbl.setHorizontalAlignment(SwingConstants.CENTER);    LoginLbl.setHorizontalTextPosition(SwingConstants.CENTER);    LoginLbl.setText("LOGIN");    usrNmeLbl.setText("User Name: ");    userName.setPreferredSize(new Dimension(150, 21));    User.setPreferredSize(new Dimension(210, 31));    pasLbl.setMaximumSize(new Dimension(80, 17));    pasLbl.setPreferredSize(new Dimension(70, 17));    pasLbl.setText("Password: ");    passWord.setEchoChar('*');    this.setDefaultCloseOperation(3);    this.setResizable(false);    this.setTitle("Daily Expenses [LOGIN]");    newUsrbtn.setFont(new java.awt.Font("Dialog", 1, 15));    newUsrbtn.setText("New User");    newUsrbtn.addActionListener(new java.awt.event.ActionListener() {       public void actionPerformed(ActionEvent e) {        newUser(e);      }    });    //lgnbtn    lgnbtn.setFont(new java.awt.Font("Dialog", 1, 15));    lgnbtn.setPreferredSize(new Dimension(103, 31));    lgnbtn.setText("LOGIN");    lgnbtn.addActionListener(new java.awt.event.ActionListener() {       public void actionPerformed(ActionEvent e) {        usrlgn(e);      }    });     //Add all things on the Panel    btnPane.setPreferredSize(new Dimension(207, 41));    contentPane.add(LoginLbl, BorderLayout.NORTH);    User.add(usrNmeLbl, null);    User.add(userName, null);    User.add(pasLbl, null);    User.add(passWord, null);    contentPane.add(User, BorderLayout.CENTER);    btnPane.add(newUsrbtn, null);    btnPane.add(lgnbtn, null);    contentPane.add(btnPane, BorderLayout.SOUTH);    UIManager.setLookAndFeel(looks[2].getClassName());     SwingUtilities.updateComponentTreeUI(this);  }//Open Fileprivate void OpenFile(){    try{    out = new RandomAccessFile(f, "rw");//Open File in Read Write Mode      }  catch(IOException e){e.printStackTrace();}     }//Open File in ReadOnlyprivate void OpenReadOnly(){    try{    out = new RandomAccessFile(f, "r");//Open File in Read Mode      }  catch(IOException e){e.printStackTrace();}     }//Close Filepublic void CloseFile(){  try{    if (out != null)      out.close();      }  catch(IOException e){e.printStackTrace();}}public void addRecord(){  if (!(userName.getText() == "")){    UserRecord urec = new UserRecord(userName.getText(), passWord.getText());   try{      out.seek(f.length()+1);      urec.write(out);        }    catch(IOException e){e.printStackTrace();}    finally{      CloseFile();      }  }  else    JOptionPane.showMessageDialog(this,"Please enter User Name", "Incomplete",    JOptionPane.ERROR_MESSAGE);   } public boolean isUser(User us){boolean flag = true;UserRecord usr = new UserRecord("","");UserRecord ck = new UserRecord(us.getUserName(), us.getPwd());try{  do{    OpenReadOnly();    usr.read(out);}    while(usr.getUserName() == "");    if (usr.getUserName() == ck.getUserName() &&                usr.getPwd() == ck.getPwd())          flag = true;    else          flag = false;          CloseFile();    }    catch(EOFException e){e.printStackTrace();    CloseFile();    }    catch(IOException e){e.printStackTrace();}    return flag;  }  void newUser(ActionEvent e) {  OpenFile();  addRecord();  CloseFile();  }    void usrlgn(ActionEvent e) {     Expense expense = new Expense();   UserRecord u = new UserRecord (userName.getText(), passWord.getText());   if(isUser(u)){      expense.setVisible(true);      this.setVisible(false);   }   }public static void main(String[] args) {    LoginExpense login = new LoginExpense();    Expense expense = new Expense();    login.setSize(new Dimension(250, 200));    login.setVisible(true);    expense.setSize(new Dimension(600, 250));    expense.setVisible(false);   }  }class User implements Serializable{  private String UserName;  private String Password; public User(String usr, String pwd){  UserName = usr;  Password = pwd;  }  public User(){    } public User(User usr){  this.UserName = usr.UserName;  this.setPassWord(usr.Password);  }   public void setUserName(String name){    this.UserName = name;    }  public String getUserName(){    return UserName;    }  public String getPwd(){    return Password;    }  public void setPassWord(String pwd){    Password = pwd;    }  public boolean equalto(User usr){   if(this.UserName.equals(usr.UserName))    return true;    else return false;    }  }class UserRecord extends User{ public UserRecord(String UserName, String Pwd){   super(UserName, Pwd);    } private String padName(RandomAccessFile file) throws IOException{  char name[], temp;  name = new char[15];   for(int i=2; i<name.length; i++){     temp=file.readChar();     name[i] = temp;      } return new String(name).replace('\0',' '); } private void writeString(RandomAccessFile file, String txt) throws IOException{ StringBuffer buf = null; if (txt!=null)   buf = new StringBuffer(txt); else   buf = new StringBuffer(15);   buf.setLength(15);  file.writeChars(buf.toString());   }  public void read(RandomAccessFile file)throws IOException{   setUserName(padName(file));   setPassWord(padName(file));   } public void write(RandomAccessFile file)throws IOException{  writeString(file, getUserName());  writeString(file, getPwd()); } public static int size(){   return 65;   }}


Help me out with this
 

Posts:6,750
Registered: 1/25/04
Re: There is a problem in my application......................  
Jul 15, 2004 10:09 AM (reply 1 of 58)



 
The following code is a Login Class code that I
mentioned Earlier thanks for guiding me through but
the problem is this that the code wouldn't go on after
the isUser() method in this code.

Well, threads don't just stop for no reason. Does it seem to go into an infinite loop (never stops processing) or are you seeing some kind of error message?
 

Posts:343
Registered: 7/13/04
Re: There is a problem in my application......................  
Jul 15, 2004 10:10 AM (reply 2 of 58)



 
usually its easier for people to help here if you narrow down your coding to the problem, then ask a spacific question about it.
 

Posts:49
Registered: 6/17/04
Re: There is a problem in my application......................  
Jul 16, 2004 5:12 AM (reply 3 of 58)



 
Well Basically nothing happens.
The code just will not execute the following portion
public boolean isUser(User us){boolean flag = true;UserRecord usr = new UserRecord("","");UserRecord ck = new UserRecord(us.getUserName(), us.getPwd());try{  do{    OpenReadOnly();    usr.read(out);}    while(usr.getUserName() == "");    if (usr.getUserName() == ck.getUserName() &&                usr.getPwd() == ck.getPwd())          flag = true;    else          flag = false;          CloseFile();    }    catch(EOFException e){e.printStackTrace();    CloseFile();    }    catch(IOException e){e.printStackTrace();}    return flag;  }

I am calling it when the User presses the Login button as follows
void usrlgn(ActionEvent e) {     Expense expense = new Expense();   UserRecord u = new UserRecord (userName.getText(), passWord.getText());   if(isUser(u)){      expense.setVisible(true);      this.setVisible(false);   }   }

Now is it easier............
 

Posts:49
Registered: 6/17/04
Re: There is a problem in my application......................  
Jul 16, 2004 5:14 AM (reply 4 of 58)



 
Well Basically nothing happens.
The code just will not execute the following portion
public boolean isUser(User us){boolean flag = true;UserRecord usr = new UserRecord("","");UserRecord ck = new UserRecord(us.getUserName(), us.getPwd());try{  do{    OpenReadOnly();    usr.read(out);}    while(usr.getUserName() == "");    if (usr.getUserName() == ck.getUserName() &&                usr.getPwd() == ck.getPwd())          flag = true;    else          flag = false;          CloseFile();    }    catch(EOFException e){e.printStackTrace();    CloseFile();    }    catch(IOException e){e.printStackTrace();}    return flag;  }

I am calling it when the User presses the Login button as follows
void usrlgn(ActionEvent e) {     Expense expense = new Expense();   UserRecord u = new UserRecord (userName.getText(), passWord.getText());   if(isUser(u)){      expense.setVisible(true);      this.setVisible(false);   }   }

Now is it easier............?????????????????????????????????
 

Posts:24,036
Registered: 2/3/03
Re: There is a problem in my application......................  
Jul 16, 2004 6:07 AM (reply 5 of 58)



 
Read this: [url=http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html]Code Conventions for the Java™ Programming Language[/url]

Follow its rules. It will make it much easier to decipher your code.

Also, are you saying isUser() never gets called? Are you sure? Put in some System.out.println() statements (like the first line of your isUser() method) to let you know exactly what is getting called.
 

Posts:24,036
Registered: 2/3/03
Re: There is a problem in my application......................  
Jul 16, 2004 6:11 AM (reply 6 of 58)



 
Why are you passing the ActionEvent to userlgn()?
Why are you extending JFrame (what JFrame methods are you overriding/changing/extending)?
 

Posts:10,972
Registered: 10/23/03
Re: There is a problem in my application......................  
Jul 16, 2004 6:38 AM (reply 7 of 58)



 
Why are you extending JFrame (what JFrame methods are you overriding/changing/extending)?

There must be some IDE that automatically extends JFrame, JPanel, etc... in its generated code.
JBuilder? In any case, I've given up trying to convince people that there is a basic OOP principle
being trampled here: (and yes, I got my granny to crossstitch it on a throw pillow)

KEEP IT SIMPLE: ONLY SUBCLASS IF YOU MUST OVERRIDE METHODS.

 

Posts:24,036
Registered: 2/3/03
Re: There is a problem in my application......................  
Jul 16, 2004 6:51 AM (reply 8 of 58)



 
In any case, I've given up trying to convince people that there is a basic OOP principle
being trampled here...

Keep up the good fight.

"Never never never quit."
- Winston Churchill
 

Posts:1,160
Registered: 7/24/97
Re: There is a problem in my application......................  
Jul 16, 2004 7:10 AM (reply 9 of 58)



 
I don't know of any IDE's that don't override JFrame when creating an Application.

Creating concrete classes that extend other concrete classes makes it easier to reuse your code.

In the case of a Swing Application it is standard practice to encapsulate your application as a subclass of JFrame. While this might break some basic OOP principle this is not an hard and fast rule. Not all 'principles' help in the real world.

I have done both, I found that having the Application GUI based on a subclass of JFrame made things a lot easier to maintain and extend.

I never have my main class as a subclass of JFrame however.

Subclassing a concrete class in order to implement interfaces is what you are supposed to do.

If you want to subclass JFrame, create an Interface that exposes the API you want for your application and have your subclass of JFrame implement that.

That is not only sensible OOP it is correct OOP.
 

Posts:49
Registered: 6/17/04
Re: There is a problem in my application......................  
Jul 17, 2004 3:21 AM (reply 10 of 58)



 
What I want is a method that can read from the RandomAccessFile if the User exists or not I m catching exceptions for the isUser() method but it just doesn't gives any..............?

Can you make me a method that Validates the UserName and Password from the File
 

Posts:49
Registered: 6/17/04
Re: There is a problem in my application......................  
Jul 17, 2004 3:39 AM (reply 11 of 58)



 
Well I tried to put some System.out.println() methods and checked the flags but it always comes out false even if I am trying a Valid User It isn't allowing the Valid User to paas thorugh you can see the method I m using a RandomAccessFile the source code is at the top of this topic please do correct it!
 

Posts:49
Registered: 6/17/04
Re: There is a problem in my application......................  
Jul 17, 2004 9:14 AM (reply 12 of 58)



 
Well can any one tell me Why the isUser() method always returns false.
 

Posts:329
Registered: 1/6/04
Re: There is a problem in my application......................  
Jul 17, 2004 11:37 AM (reply 13 of 58)



 
Well can any one tell me Why the isUser() method
always returns false.

Probably because you're using the '==' operator instead of the 'equals' method in the String class to test for string equality:
...    while(usr.getUserName() == "");   if (usr.getUserName() == ck.getUserName() && usr.getPwd() == ck.getPwd())...

Sun has a tech tip article on testing for string equality that might interest you: http://java.sun.com/developer/JDCTechTips/2004/tt0504.html#2
 

Posts:24,036
Registered: 2/3/03
Re: There is a problem in my application......................  
Jul 17, 2004 12:26 PM (reply 14 of 58)



 
Don't confuse String's equals() method with the equality operator '=='. The == operator checks that two String references refer to the same object. If you want to compare the contents of Strings (whether two strings contain the same characters), use equals(), e.g. if (str1.equals(str2))...

Example:
String s1 = "foo";String s2 = new String("foo");System.out.println("s1 == s2: " + (s1 == s2)); // falseSystem.out.println("s1.equals(s2): " + (s1.equals(s2))); // true

For more information, check out [url=http://www-106.ibm.com/developerworks/java/library/j-ebb0917a.html]Comparison operators: equals() versus ==[/url]
 
This topic has 58 replies on 4 pages.    1 | 2 | 3 | 4 | Next »