-
Urgent Help!!
Code:
/**
* @(#)Project.java
*
*
* Han Wei'An 0800680A
* @version 1.00 2009/1/13
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Date;
import java.text.*;
public class Project extends JFrame {
JPanel pNorth,pSouth,TutorPanel;
JButton btn1,btn2;
JTextField tfUser;
JPasswordField tfPass;
JLabel lblUser, lblPass;
JTextArea area = new JTextArea();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String [] userName = {"0811111A","0822222B","08333333C", "tutor"};//for the list of predetermined users
String [] userPassword = {"p1", "p2","p3","123"};//for the list of predetermined users
String [] userType = {"Student", "Tutor"};//for checking user type
private ProjectController controller;
public Project() {
setTitle("Student Attendence System");
setSize(300,150);
setResizable(false);
setLayout(new BorderLayout());
area = new JTextArea(5,5);
area.setEditable(true);
JScrollPane scrollPane = new JScrollPane(area);
ProjectController controller = new ProjectController();
for (int i=0;i<3;i++){
controller.createProjectRec(userName[i], userPassword[i], "Student");//insert data into controller
}
{
controller.createProjectRec("tutor", "123", "Tutor");
}
pNorth = new JPanel();
pSouth = new JPanel();
TutorPanel = new JPanel();
pNorth.setLayout(new GridLayout(2,2));
pSouth.setLayout(new GridLayout(1,3));
lblUser = new JLabel("user ID");
tfUser = new JTextField();
lblPass = new JLabel("Password");
tfPass = new JPasswordField();
btn1 = new JButton("Login");
btn2 = new JButton("Clear");
btn1.addActionListener(new LoginHandler(controller));
btn2.addActionListener(new ClearHandler());
pNorth.add(lblUser);
pNorth.add(tfUser);
pNorth.add(lblPass);
pNorth.add(tfPass);
pSouth.add(btn1);
pSouth.add(btn2);
TutorPanel.add(area);
add(pNorth, "Center");
add(pSouth, "South");
setVisible(true);
}
class LoginHandler implements ActionListener
{
private ProjectController controller = null;
public LoginHandler(ProjectController c){
controller = c;
}
public void actionPerformed(ActionEvent evt)
{
String User = tfUser.getText();
String pass = "";
try{ pass = tfPass.getText();}
catch(Exception ex){
}
if (checkPassword(User,pass)){
Date date = new Date();
String datetime = dateFormat.format(date);
String[] info = controller.getProjectInfo(User);//check the type
String loginShow = "Student ID: "+User +" Login at "+datetime;
JOptionPane.showMessageDialog(null, loginShow , "Message", JOptionPane.PLAIN_MESSAGE);
if (info[1].equals("Student")){
controller.createProjectRec(User, pass, "Student");//insert data into controller
} else if (info[1].equals("Tutor")) {
String[] abc = controller.getProjectInfo(User);
area.append(abc[0] + " " + abc[2]);
JFrame jf = new JFrame();
jf.add(TutorPanel);
jf.setSize(300,150);
jf.setVisible(true);
}
} else {
JOptionPane.showMessageDialog(null, "Invalid user & Password!" , "Message", JOptionPane.PLAIN_MESSAGE);
}
}
}
class ClearHandler implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
tfUser.setText(""); // clear the output fields
tfPass.setText("");
}
}
public boolean checkPassword(String nm, String pwd)
{
int userIndex = -1;
for(int i = 0; i<userName.length; i++)
{
if(userName[i].equals(nm))
{
userIndex = i;
}
}
// check if user exists
if (userIndex == -1) return false;
if(userPassword[userIndex].equals(pwd))
{
// user exists and password is correct
return true;
}
// user exist and password is incorrect
return false;
}
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
Project gui = new Project();
}
}
Hi, May i know what this sentence means.=>
private ProjectController controller = null;
-
1. Use an appropriate title please, urgent help will make people less inclined to help, not more.
2. Only post the code that is giving an error, or atleast isolate it so we don't have to look through it all.
3. Be more specific with what you want. No specific question asked, no specific answers given.
What do you mean? I mean you wrote it didn't you? It means your declaring a new ProjectController and initialising it as null, so basically not initializing it. It would be the exact same thing if you just declared it, and left out the ' = null', so like masijade posted, assigning null is redundant and not necessary.
I really don't see how this is urgent..
-
It is a declaration, of course.
The " = null" is redundant, as any class/instance variable that is not explicitly defined at declaration will get a default value, which for non-primitives, is null.
-
I wonder that, if our thread starter don't know what that line means, how did he/she write that code. ;)
-
Yeah that was what I was thinking too.
-
May be he's try to have an idea about the code. But this is not the way, and still no any comments too.