1

I have a class Catalogue to store info about it. Here i had just use two methods:

public void setCataName(String n)
    {cataName = n;}

public String getCataName()
    {return cataName;}

This is the code for my JFrame:

public class AddCataFrame extends JFrame
{
    JLabel lname; 
    JTextField tname;
    Catalogue catalogue;

    AddCataFrame()
    {
        super("Add New Catalogue");
        setLayout(new FlowLayout());

        lname = new JLabel("Name:", SwingConstants.LEFT);

        tname = new JTextField(15);

        textListener t = new textListener();
        tname.addActionListener(t);

        add(lname);
        add(tname);
    }

    class textListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {   
            //get the name from the textField after entered by user
            //then set it to the name of catalogue.
            //This is the place give me NullPointerException error.
            catalogue.setCataName(tname.getText()); 

            JOptionPane.showMessageDialog(null,catalogue.getCataName());
        }
    }
}

I cannot figure it out why give me a NullPointerException. Please help me.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3368506
  • 133
  • 1
  • 8
  • 4
    Have you initialized `catalogue`? – vandale Jul 20 '14 at 15:01
  • 2
    Please see [What's a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it). It's probably the most common exception you'll face, and IME in most cases, one of the easiest to catch (fix). So learn to debug your own. – Paul Samsotha Jul 20 '14 at 15:04
  • What a kind of mistake I have made!! Sorry for wasting all your time. – user3368506 Jul 20 '14 at 15:15

4 Answers4

4

Where do you initialize your catalogue variable? You don't. Solution: initialize it first before trying to use it.

e.g.,

This only declares the variable:

Catalogue catalogue;

This declares and initializes it:

Catalogue catalogue = new Catalogue();

or this can initialize the variable if you want to use a Catalogue object passed into your class on creation:

AddCataFrame(Catalgogue catalogue)
{
    super("Add New Catalogue");
    this.catalogue = catalogue; // *************

    setLayout(new FlowLayout());

    lname = new JLabel("Name:", SwingConstants.LEFT);

    tname = new JTextField(15);

    textListener t = new textListener();
    tname.addActionListener(t);

    add(lname);
    add(tname);
}

More importantly, you need to learn the general concepts of how to debug a NPE (NullPointerException). You should inspect the line carefully that throws it, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
3

Your variable catalogue is defined but never initialized. That's why you get the exception.

You should have a line like catalogue = new Catalogue()

Jens
  • 67,715
  • 15
  • 98
  • 113
2

You didn't initialise catalogue. You should change

Catalogue catalogue;

to

Catalogue catalogue = new Catalogue();

Non-primitive fields are by default initialised to null, hence the NullPointerException on line

catalogue.setCataName(tname.getText());
bcsb1001
  • 2,834
  • 3
  • 24
  • 35
2

you have to initilaze cataloge

Cataloge  catalog= new Cataloge();
unfamous
  • 611
  • 1
  • 5
  • 14