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 large NullPointerException problem (both the error + appropriate pieces of code are shown below). i had to press End on the jGRASP console in order to end the program. i don't know what to do. how can i fix these errors?

-jGRASP exec: java HomeEntertainment

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at User.doLogOn(LogOn.java:71)

at LogOn$1.actionPerformed(LogOn.java:42)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

-jGRASP: process aborted by user.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

publicclass HomeEntertainmentextends JPanelimplements 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);

}

publicvoid 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);

}

}

publicstaticvoid main(String[] args){

new HomeEntertainment();

}

}

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

publicclass LogOnextends 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(){

publicvoid 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(){

publicvoid actionPerformed(ActionEvent e){

setVisible(false);

}

});

}

}

class User{

String username;

String password;

String[] userUsernameArray ={"Ann Smyth","John Murphy"};

String[] userPasswordArray ={"1","2"};

publicvoid 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"};

publicvoid 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);

}

}

}

}

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass AdminMainMenuextends JFrame{

JPanel pnlBody;

JButton btnOrderSystem =new JButton("Order System");

JButton btnMaintenance =new JButton("Maintenance");

JButton btnAdminLogOff =new JButton("Log Off");

Container contentpane;

public AdminMainMenu(){

super("Main Menu");

contentpane = getContentPane();

contentpane.setLayout(new BorderLayout());

pnlBody =new JPanel();

pnlBody.add(btnOrderSystem);

pnlBody.add(btnMaintenance);

pnlBody.add(btnAdminLogOff);

contentpane.add(pnlBody,BorderLayout.CENTER);

pack();

setVisible(true);

btnOrderSystem.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

setVisible(false);

//OrderSystem os = new OrderSystem();

//os.setVisible(true);

}

});

btnMaintenance.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

setVisible(false);

//Maintenance m = new Maintenance();

//m.setVisible(true);

}

});

btnAdminLogOff.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

int result;

result = JOptionPane.showConfirmDialog(null,"Are you sure you want to log off?", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

if(result == JOptionPane.YES_OPTION){

setVisible(false);

}

}

});

}

}

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass UserMainMenuextends JFrame{

JPanel pnlBody;

JButton btnUserProductMenu =new JButton("Product Menu");

JButton btnUserMemberMenu =new JButton("Member Menu");

JButton btnUserRentalMenu =new JButton("Rental Menu");

JButton btnUserLogOff =new JButton("Log Off");

Container contentpane;

public UserMainMenu(){

super("Main Menu");

contentpane = getContentPane();

contentpane.setLayout(new BorderLayout());

pnlBody =new JPanel();

pnlBody.add(btnUserProductMenu);

pnlBody.add(btnUserMemberMenu);

pnlBody.add(btnUserRentalMenu);

pnlBody.add(btnUserLogOff);

contentpane.add(pnlBody,BorderLayout.CENTER);

pack();

setVisible(true);

btnUserProductMenu.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

setVisible(false);

//UserProductMenu upm = new UserProductMenu();

//upm.setVisible(true);

}

});

btnUserMemberMenu.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

setVisible(false);

//UserMemberMenu umm = new UserMemberMenu();

//umm.setVisible(true);

}

});

btnUserRentalMenu.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

setVisible(false);

//UserRentalMenu urm = new UserRentalMenu();

//urm.setVisible(true);

}

});

btnUserLogOff.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

int result;

result = JOptionPane.showConfirmDialog(null,"Are you sure you want to log off?", null, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

if(result == JOptionPane.YES_OPTION){

setVisible(false);

}

}

});

}

}

[22301 byte] By [james-mcfaddena] at [2007-11-27 10:50:05]
# 1

> . i don't know what to do. how can i fix these errors?

Did you ever consider

- looking at User.doLogOn(LogOn.java), line 71 to see what it does

- looking at the API docs for a NPE to see why it might be thrown?

CeciNEstPasUnProgrammeura at 2007-7-29 11:23:11 > top of Java-index,Java Essentials,Java Programming...
# 2

you never set password to anything, so when you check it against the password array, it is null, and .equals() throw an NPE.

for JFrames, you should always use setDefaultCloseOperation() to tell the system what to do when closing. Usually you would use JFrame.EXIT_ON_CLOSE;

Read the docs again.

There are many other problems here, and I am not about to list them all. Keep plugging away at it.

~Tim

SomeoneElsea at 2007-7-29 11:23:11 > top of Java-index,Java Essentials,Java Programming...
# 3

This is causing a problem -

In class User -> if(username.equals(userUsernameArray)){

because username is null.

You can fix it here -

In class Logon -

User u = new User();

u.doLogOn();

u.username = jtfUN.getText().trim();

u.password = new String(jtfPW.getPassword());

move the u.doLogon() after the username is set.

imran_ea at 2007-7-29 11:23:11 > top of Java-index,Java Essentials,Java Programming...
# 4

thanks for the reply.

i done what you suggested as much as i could. when i tried to log on to my system the first time, 2 main menus appeared instead of one. i removed the for loop code. but when i tried to compile the video library menu program the second time, i get the following errors (both the errors and the modified LogOn.java program are shown below). i must have done something stupid now. should i put the for loops back into the code + modify the User and Administrator classes so that only one main menu appears when i log on to my system?

-jGRASP exec: javac -g E:\CP4B Project\HomeEntertainment.java

LogOn.java:68: '.class' expected

if(username.equals(userUsernameArray[]) && password.equals(userPasswordArray[])){

^

LogOn.java:74: ')' expected

}

^

LogOn.java:75: illegal start of expression

}

^

LogOn.java:85: '.class' expected

if(username.equals(adminUsernameArray[]) && password.equals(adminPasswordArray[])){

^

LogOn.java:91: ')' expected

}

^

LogOn.java:92: illegal start of expression

}

^

LogOn.java:68: cannot find symbol

symbol : class userUsernameArray

location: class User

if(username.equals(userUsernameArray[]) && password.equals(userPasswordArray[])){

^

LogOn.java:68: unexpected type

required: value

found: class

if(username.equals(userUsernameArray[]) && password.equals(userPasswordArray[])){

^

LogOn.java:85: cannot find symbol

symbol : class adminUsernameArray

location: class Administrator

if(username.equals(adminUsernameArray[]) && password.equals(adminPasswordArray[])){

^

LogOn.java:85: unexpected type

required: value

found: class

if(username.equals(adminUsernameArray[]) && password.equals(adminPasswordArray[])){

^

10 errors

-jGRASP wedge2: exit code for process is 1.

-jGRASP: operation complete.

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.username = jtfUN.getText().trim();

u.doLogOn();

u.password = new String(jtfPW.getPassword());

Administrator a = new Administrator();

a.username = jtfUN.getText().trim();

a.doLogOn();

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(){

if(username.equals(userUsernameArray[]) && password.equals(userPasswordArray[])){

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(){

if(username.equals(adminUsernameArray[]) && password.equals(adminPasswordArray[])){

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);

}

}

}

james-mcfaddena at 2007-7-29 11:23:11 > top of Java-index,Java Essentials,Java Programming...
# 5

(userPasswordArray[])

is not correct syntax. Leave those brackets away.

CeciNEstPasUnProgrammeura at 2007-7-29 11:23:11 > top of Java-index,Java Essentials,Java Programming...
# 6

And All Because Learning To Interpret A StackTrace Is More Effort Than Simply Dumping Entire Codebases On The Forum And Saying "fix please"

At some point, you're just going to have to learn how to fix problems as a whole, rather than tackle each one as if it's totally new, and get outside help.

georgemca at 2007-7-29 11:23:11 > top of Java-index,Java Essentials,Java Programming...
# 7

> At some point, you're just going to have to learn how

> to fix problems as a whole, rather than tackle each

> one as if it's totally new, and get outside help.

Never thought about it that way. C is still like this for me, because I rarely need to program in it. A segfault is always a complete blocker for me; I have no idea what could have potentially caused it, because I can't remember what used to cause it before.

If I concentrate really hard, I can still remember when the java stack traces looked like a foreign language to me. Odd that now they make some bugs trivial to fix.

bheilersa at 2007-7-29 11:23:11 > top of Java-index,Java Essentials,Java Programming...
# 8

> Never thought about it that way. C is still like

> this for me, because I rarely need to program in it.

> A segfault is always a complete blocker for me;

I remember coding in C and constantly getting Segmentation Faults with little other information to enable tracking it down. I

just thought the compiler was being polite and didn't want to say "Your code is ****! Now go away and don't bother me

again until you have fixed it".

floundera at 2007-7-29 11:23:11 > top of Java-index,Java Essentials,Java Programming...
# 9

hello CeciNEstPasUnProgrammeur.

thanks for the advice. i done what you told me to do. the logon program works to a certain extent (modified logon program is shown below). when i try to log onto my system this time. a JOptionPane.ERROR_MESSAGE message appears on screen. when i press OK inside the message, the message disappears but the same one appears again. i press OK again + the JOptionPane messages stop appearing again. when i press ok after entering my login details in the text + password fields, a main menu (either an administrator menu or a user menu) should be displayed on screen. what should i do to make this problem go away?

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){

String username;

String password;

String[] userUsernameArray = {"Ann Smyth", "John Murphy"};

String[] userPasswordArray = {"1", "2"};

String[] adminUsernameArray = {"Administrator"};

String[] adminPasswordArray = {"0"};

username = jtfUN.getText().trim();

password = new String(jtfPW.getPassword());

if(username.equals(userUsernameArray) && password.equals(userPasswordArray)){

setVisible(false);

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);

}

if(username.equals(adminUsernameArray) && password.equals(adminPasswordArray)){

setVisible(false);

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);

}

}

});

jbCancel.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

setVisible(false);

}

});

}

}

james-mcfaddena at 2007-7-29 11:23:11 > top of Java-index,Java Essentials,Java Programming...