GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Oct-2005, 20:25
bdwinsalt bdwinsalt is offline
New Member
 
Join Date: Oct 2005
Posts: 1
bdwinsalt is on a distinguished road

Null Pointer Errors!


I followed the books code, the code will compile, but it gets null pointer errors when i type "java cm" please help. Here is the code:

JAVA Code:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

class cm extends Frame implements ActionListener
{
private Vector contacts = new Vector(100);
private java.awt.List names = new java.awt.List();
private Button delete;
private Button update;
public cm()
{
super("Contact Manager");
addWindowListener(new WindowAdapter ()
{
public void windowClosing
(WindowEvent e)
{
saveContacts();
System.exit(0);
}
});
Label l = new Label();
add("North", l);
add("Center", names);
Panel p = new Panel();
Button b;
p.add(b = new Button("Insert"));
b.addActionListener(this);
p.add(b = new Button("Delete"));
delete.addActionListener(this);
delete.setEnabled(false);
p.add(update = new Button("Update"));
update.addActionListener(this);
update.setEnabled(false);
p.add(b = new Button("Finish"));
b.addActionListener(this);
add("South", p);
setBackground(Color.lightGray);
setSize(400, 200);
setResizable(false);
loadContacts();
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Delete"))
delete();
else
if(e.getActionCommand().equals("Finish"))
{
saveContacts();
System.exit(0);
}
else
if(e.getActionCommand().equals("Insert"))
insert();
else
update();
}
public Insets getInsets()
{
return new Insets(10, 10, 10, 10);
}
public static void main(String [] args)
{
new cm();
}
private void delete()
{
int index = names.getSelectedIndex();
if(index != -1)
{
names.remove(index);
contacts.remove(index);
if(contacts.size() == 0)
{
delete.setEnabled(true);
update.setEnabled(true);
}
else
names.select(0);
}
}
private void insert()
{
DataEntryForm def = new DataEntryForm(this, "Insert");
if(def.bOk)
{
Contact temp = new Contact();
temp.fname = new String(def.fname.getText());
temp.lname = new String(def.lname.getText());
temp.phone = new String(def.phone.getText());
temp.fax = new String(def.fax.getText());
temp.email = new String(def.phone.getText());
names.add(temp.lname + ", " + temp.fname);
contacts.add(temp);
delete.setEnabled(true);
update.setEnabled(true);
}
def.dispose();
names.select(0);
}
private void loadContacts()
{
FileInputStream fis = null;
try
{
fis = new FileInputStream("contacts.dat");
DataInputStream dis = new DataInputStream(fis);
int nContacts = dis.readInt();
for(int i = 0; i < nContacts; i++)
{
Contact temp = new Contact();
temp.fname = dis.readUTF();
temp.lname = dis.readUTF();
temp.phone = dis.readUTF();
temp.fax = dis.readUTF();
temp.email = dis.readUTF();
names.add(temp.lname + ", " + temp.fname);
contacts.add(temp);
}
if(nContacts > 0)
{
delete.setEnabled(true);
update.setEnabled(true);
}
}
catch(IOException e)
{
System.out.println("Error!");
}
finally
{
try
{
fis.close();
}
catch(IOException e){
System.out.println("Error");
}
}
names.select(0);
}
private void saveContacts()
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream("contacts.dat");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeInt(contacts.size());
for(int i = 0; i < contacts.size(); i++)
{
Contact temp = (Contact) contacts.elementAt(i);
dos.writeUTF(temp.fname);
dos.writeUTF(temp.lname);
dos.writeUTF(temp.phone);
dos.writeUTF(temp.fax);
dos.writeUTF(temp.email);
}
}
catch(IOException e)
{
MsgBox mb = new MsgBox(this, "CM Error",
                              e.toString());
                              mb.dispose();
                              }
                              finally
                              {
                              try
                              {
                              fos.close();
                              }
                              catch(IOException e){System.out.println("Error!");
}
                              }
                              }
                              private void update()
                              {
                              int index = names.getSelectedIndex();
                              if(index != -1)
                              {
                              Contact temp = (Contact) contacts.elementAt(index);
                              DataEntryForm def = new DataEntryForm (this, "Update",
                                                                           temp.fname,
                                                                           temp.lname,
                                                                           temp.phone,
                                                                           temp.fax,
                                                                           temp.email);
                                                                           if(def.bOk)
                                                                           {
                                                                           temp.fname = new String(def.fname.getText());
                                                                           temp.lname = new String(def.lname.getText());
                                                                           temp.phone = new String(def.phone.getText());
                                                                           temp.fax = new String(def.fax.getText());
                                                                           temp.email = new String(def.email.getText());
                                                                           names.replaceItem(temp.lname + ", " + temp.fname,
                                                                           index);
                                                                           }
                                                                           def.dispose();
                                                                           names.select(0);
                                                                           }
                                                                           }
                                                                           }
                                                                           class Contact
                                                                           {
                                                                           public String fname;
                                                                           public String lname;
                                                                           public String phone;
                                                                           public String fax;
                                                                           public String email;
                                                                           }
                                                                           class DataEntryForm extends Dialog implements ActionListener
                                                                           {
                                                                           public boolean bOk;
                                                                           public TextField fname;
                                                                           public TextField lname;
                                                                           public TextField phone;
                                                                           public TextField fax;
                                                                           public TextField email;
                                                                           public void actionPerformed(ActionEvent e)
                                                                           {
                                                                           if(e.getActionCommand().equals("Ok"))
                                                                           bOk = true;
                                                                           dispose();
                                                                           }
                                                                           public DataEntryForm(Frame parent, String title)
                                                                           {
                                                                           this(parent, title, "","","","","");
                                                                           }
                                                                           public DataEntryForm (Frame parent, String title, String fname, String lname, String phone, String fax, String email)
                                                                           {
                                                                           super(parent, title, true);
                                                                           setLayout(new GridBagLayout());
                                                                           addComponent(this, new Label("First name: "), 0, 0, 1, 1,
                                                                           GridBagConstraints.NONE,
                                                                           GridBagConstraints.WEST);
                                                                           this.fname = new TextField(15);
                                                                           addComponent(this, this.fname, 1, 0, 1, 1,
                                                                           GridBagConstraints.NONE,
                                                                           GridBagConstraints.CENTER);
                                                                           if(title.equals("Update"))
                                                                           this.fname.setText(fname);
                                                                           addComponent(this, new Label("Last name: "), 0, 1, 1, 1,
                                                                           GridBagConstraints.NONE,
                                                                           GridBagConstraints.WEST);
                                                                           this.lname = new TextField(15);
                                                                           addComponent(this, this.lname, 1, 1, 1, 1,
                                                                           GridBagConstraints.NONE,
                                                                           GridBagConstraints.CENTER);
                                                                           if(title.equals("Update"))
                                                                           this.lname.setText(lname);
                                                                           addComponent(this, new Label("Phone number: "), 0, 2, 1, 1,
                                                                           GridBagConstraints.NONE,
                                                                           GridBagConstraints.WEST);
                                                                           this.phone = new TextField(15);
                                                                           addComponent(this, this.phone, 1, 2, 1, 1,
                                                                           GridBagConstraints.NONE,
                                                                           GridBagConstraints.CENTER);
                                                                           if(title.equals("Update"))
                                                                           this.phone.setText(phone);
                                                                           addComponent(this, new Label("FAX number: "), 0, 3, 1, 1,
                                                                           GridBagConstraints.NONE,
                                                                           GridBagConstraints.WEST);
                                                                           this.fax = new TextField(15);
                                                                           addComponent(this, this.fax, 1, 3, 1, 1,
                                                                           GridBagConstraints.NONE,
                                                                           GridBagConstraints.CENTER);
                                                                           if(title.equals("Update"))
                                                                           this.fax.setText(fax);
                                                                           addComponent(this, new Label("Email Address: "), 0, 4, 1, 1,
                                                                           GridBagConstraints.NONE,
                                                                           GridBagConstraints.WEST);
                                                                           this.email = new TextField(25);
                                                                           addComponent(this, this.email, 1, 4, 1, 1,
                                                                           GridBagConstraints.NONE,
                                                                           GridBagConstraints.CENTER);
                                                                           if(title.equals("Update"))
                                                                           this.email.setText(email);
                                                                           addComponent(this, new Label(""), 0, 5, 1, 1,
                                                                           GridBagConstraints.NONE,
                                                                           GridBagConstraints.CENTER);
                                                                           addComponent(this, new Label(""), 1, 5, 1, 1,
                                                                           GridBagConstraints.NONE,
                                                                           GridBagConstraints.CENTER);
                                                                           Button b;
                                                                           addComponent(this, b = new Button ("Ok"), 0, 6, 1, 1,
                                                                           GridBagConstraints.NONE,
                                                                           GridBagConstraints.CENTER);
                                                                           b.addActionListener(this);
                                                                           addComponent(this, b = new Button("Cancel"), 1, 6, 1, 1,
                                                                           GridBagConstraints.NONE,
                                                                           GridBagConstraints.CENTER);
                                                                           b.addActionListener(this);
                                                                           setSize(250, 200);
                                                                           setResizable(false);
                                                                           setVisible(true);
                                                                           }
private void addComponent(Container con, Component com, int gridx, int gridy, int gridw, int gridh, int fill, int anchor){
LayoutManager lm = con.getLayout();
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridw;
gbc.gridheight = gridh;
gbc.fill = fill;
gbc.anchor = anchor;
((GridBagLayout) lm).setConstraints(com, gbc);
con.add(com);
}
}
class MsgBox extends Dialog implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
dispose();
}
public MsgBox(Frame parent, String title, String msg)
{
super(parent, title, true);
Label l = new Label(msg);
add("Center", l);
Button b = new Button("Ok");
add("South", b);
b.addActionListener(this);
b.requestFocus();
setResizable(false);
pack();
setVisible(true);
 }
}
Last edited by admin : 11-Oct-2005 at 21:12. Reason: Please insert your JAVA code between [java] & [/java] tags
  #2  
Old 13-Oct-2005, 01:33
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 890
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Null Pointer Errors!


Quote:
Originally Posted by bdwinsalt
I followed the books code, the code will compile, but it gets null pointer errors when i type "java cm" please help. Here is the code:

Hello, bdwinsalt.

Since it would be really hard for me to build all the graphic objects and compile and run your program for myself, I suggest you do the following:
  • put everything that might cause this Exception in a try - catch block and treat the NullPointerException case;
  • pop a message box on the screen with the error details - use the toString method of the Exception.
This should help you determine at what point in the program the exception is raised. If you don't figure out the solution, then please post the code you identified as "bad", so we can look at that particular piece.

Note: this exception usually occurs because the programmer(you) has passed an un-initialized object reference to a function and failed to check it in the receiving function.

Also, please read the Code formatting Tutorial - it is C++ - oriented, but it applies fairly well to Java too. I find it pretty hard to read:
JAVA Code:
public cm()
{
super("Contact Manager");
addWindowListener(new WindowAdapter ()
{
//...

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 13-Oct-2005, 03:21
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Null Pointer Errors!


Hi bdwinsalt,

What version of java do you have?
If you use jdk1.5, you will receive a lot of warnings.

To the program:
When you run the program by typing java cm, you will receive the error message probably like this:
Quote:
Exception in thread "main" java.lang.NullPointerException
at cm.<init>(cm.java:32)
at cm.main(cm.java:6

There. It displays where you have the error. It is in line 32.
So think what is the error in the line:
JAVA Code:
delete.addActionListener(this);

Here is a page in the sun java documentation about NullPointerException:
Quote:
public class NullPointerException
extends RuntimeException

Thrown when an application attempts to use null in a case where an object is required. These include:
Calling the instance method of a null object.
Accessing or modifying the field of a null object.
Taking the length of null as if it were an array.
Accessing or modifying the slots of null as if it were an array.
Throwing null as if it were a Throwable value.

Applications should throw instances of this class to indicate other illegal uses of the null object.

So can you find what is the problem in your case?

The answer is:
Check where you have created an instance for the delete button before you use it.

The error you made is that you should have written your line31 as :
JAVA Code:
p.add(delete = new Button("Delete"));
delete.addActionListener(this);

instead of
JAVA Code:
p.add(b = new Button("Delete"));
delete.addActionListener(this);

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Reading non ASCII with read() Atomical C Programming Language 8 13-Sep-2005 14:30
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 09:25
linked list error message Krandygrl00 C++ Forum 4 22-Jun-2005 14:13
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:09.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.