View Single Post
  #1 (permalink)  
Old 07-17-2007, 05:28 PM
warship warship is offline
Member
 
Join Date: Jul 2007
Posts: 9
warship is on a distinguished road
NullPointerException problem
hello.
i am trying to log on to the video library system that i just developed. when i type "Administrator" in the username text field and 0 in the password field and press OK i get a lot of NullPointerException problems (the appropriate pieces of code are shown below). i had to press End on the jGRASP console in order to end the program. how can this problem be solved?

Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class HomeEntertainment extends JPanel implements ActionListener{ private JTabbedPane jtp = new JTabbedPane(); private JPanel cP1 = new JPanel(); private JPanel cP2 = new JPanel(); private JPanel cP3 = new JPanel(); private JPanel cP4 = new JPanel(); private JPanel bP1 = new JPanel(); private JPanel bP2 = new JPanel(); private JPanel bP3 = new JPanel(); private JPanel bP4 = new JPanel(); private JButton jbAdministratorLogOn = new JButton("Log On"); private JButton jbNewUserRegister = new JButton("Register"); private JButton jbExistingUserLogOn = new JButton("Log On"); private JButton jbExitTheSystem = new JButton("Exit the system"); public HomeEntertainment(){ cP1.setLayout(new BorderLayout()); cP2.setLayout(new BorderLayout()); cP3.setLayout(new BorderLayout()); cP4.setLayout(new BorderLayout()); bP1.setBorder(new TitledBorder("Make a choice")); bP2.setBorder(new TitledBorder("Make a choice")); bP3.setBorder(new TitledBorder("Make a choice")); bP4.setBorder(new TitledBorder("Make a choice")); bP1.add(jbAdministratorLogOn); bP2.add(jbNewUserRegister); bP3.add(jbExistingUserLogOn); bP4.add(jbExitTheSystem); cP1.add(bP1, BorderLayout.SOUTH); cP2.add(bP2, BorderLayout.SOUTH); cP3.add(bP3, BorderLayout.SOUTH); cP4.add(bP4, BorderLayout.SOUTH); jtp.addTab("Administrator", cP1); jtp.addTab("New User", cP2); jtp.addTab("Existing User", cP3); jtp.addTab("Exit the system", cP4); JFrame jf = new JFrame("Home Entertainment"); jf.getContentPane().add(jtp, BorderLayout.CENTER); jf.setSize(500, 500); jf.setVisible(true); jbAdministratorLogOn.addActionListener(this); jbNewUserRegister.addActionListener(this); jbExistingUserLogOn.addActionListener(this); jbExitTheSystem.addActionListener(this); } public void actionPerformed(ActionEvent e){ if(e.getSource() == jbAdministratorLogOn){ LogOn logOn = new LogOn(); logOn.setVisible(true); } if(e.getSource() == jbNewUserRegister){ RegistrationForm r = new RegistrationForm(); r.setVisible(true); } if(e.getSource() == jbExistingUserLogOn){ LogOn logOn = new LogOn(); logOn.setVisible(true); } if(e.getSource() == jbExitTheSystem){ System.exit(0); } } public static void main(String[] args){ new HomeEntertainment(); } } import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LogOn extends JFrame{ JPanel pnlBody, pnlFooter; JLabel unLabel = new JLabel("Username: "); JLabel pwLabel = new JLabel("Password: "); JTextField jtfUN = new JTextField(20); JPasswordField jtfPW = new JPasswordField(20); JButton jbOK = new JButton("OK"); JButton jbCancel = new JButton("Cancel"); Container contentpane; public LogOn(){ super("Welcome to Home Entertainment"); contentpane = getContentPane(); contentpane.setLayout(new BorderLayout()); pnlBody = new JPanel(); pnlFooter = new JPanel(); pnlBody.add(unLabel); pnlBody.add(jtfUN); pnlBody.add(pwLabel); pnlBody.add(jtfPW); pnlFooter.add(jbOK); pnlFooter.add(jbCancel); contentpane.add(pnlBody,BorderLayout.NORTH); contentpane.add(pnlFooter,BorderLayout.CENTER); pack(); setVisible(true); jbOK.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ User u = new User(); u.doLogOn(); u.username = jtfUN.getText().trim(); u.password = new String(jtfPW.getPassword()); Administrator a = new Administrator(); a.doLogOn(); a.username = jtfUN.getText().trim(); a.password = new String(jtfPW.getPassword()); setVisible(false); } }); jbCancel.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ setVisible(false); } }); } } class User{ String username; String password; String[] userUsernameArray = {"Ann Smyth", "John Murphy"}; String[] userPasswordArray = {"1", "2"}; public void doLogOn(){ int i, j; for(i = 0; i < userUsernameArray.length; i++){ if(username.equals(userUsernameArray[i])){ UserMainMenu umm = new UserMainMenu(); umm.setVisible(true); } else{ JOptionPane.showMessageDialog(null, "Error\n\nYou have entered an incorrect username and/or password\nPlease try again", null, JOptionPane.ERROR_MESSAGE); } } for(j = 0; j < userPasswordArray.length; j++){ if(password.equals(userPasswordArray[j])){ UserMainMenu umm = new UserMainMenu(); umm.setVisible(true); } else{ JOptionPane.showMessageDialog(null, "Error\n\nYou have entered an incorrect username and/or password\nPlease try again", null, JOptionPane.ERROR_MESSAGE); } } } } class Administrator{ String username; String password; String[] adminUsernameArray = {"Administrator"}; String[] adminPasswordArray = {"0"}; public void doLogOn(){ int k, l; for(k = 0; k < adminUsernameArray.length; k++){ if(username.equals(adminUsernameArray[k])){ AdminMainMenu amm = new AdminMainMenu(); amm.setVisible(true); } else{ JOptionPane.showMessageDialog(null, "Error\n\nYou have entered an incorrect username and/or password\nPlease try again", null, JOptionPane.ERROR_MESSAGE); } } for(l = 0; l < adminPasswordArray.length; l++){ if(password.equals(adminPasswordArray[l])){ AdminMainMenu amm = new AdminMainMenu(); amm.setVisible(true); } else{ JOptionPane.showMessageDialog(null, "Error\n\nYou have entered an incorrect username and/or password\nPlease try again", null, JOptionPane.ERROR_MESSAGE); } } } }
Reply With Quote
Sponsored Links