I am having a problem with an address book program. The problem is that when I try to set the fnTextField, lNTextField, ...etc I get null in all the JTextEdits. I've went through the debugger and before the I called Edit edit = new Edit(1) the values are in the variable, but afterwards they all say null. I'm thinking it because I'm calling "new" and it's reinitializing the values with null. If that's the problem how could I fix it?

Here is some of the code:

public class Edit
{

private JTextField fnTextEdit;
private String fn, ln, a, c, s, z, pn;

Edit()
{
    //First Panel
    //User enters name of contact to edit
    //When user clicks editButton the "find" method is called
}

Edit(int x)
{
    //Second Panel
    //User edits the contact details
    fnTextField = new JTextField();
    //...etc
    setAll();
}

public void find()
    {
        File file;

        BufferedReader br;
        String edit = lNTextField1.getText() + "& " + fNTextField1.getText()+ "& ";
        String line = null, newLine = "";
        try
        {
            file = new File(fileName);
            br = new BufferedReader(new FileReader(file));

            while ((line = br.readLine()) != null)
            {
                if(line.startsWith(edit))
                {
                    newLine = line;
                }
            }

            editContact = newLine.split("&");

            ln=editContact[0];
            fn=editContact[1];
            a=editContact[2];
            c=editContact[3];
            s=editContact[4];
            z=editContact[5];
            pn=editContact[6];

        }
        catch(IOException e)
        {

        }
    }

    public void setAll()
    {
        fnTextLabel.setText(fn); //This is where I get the null pointer exception
    }

        @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == editButton)
        {
            if(fNTextField1.getText().trim().equals("") || lNTextField1.getText().trim().equals(""))
            {
                message.setText("Please enter First Name and Last Name");
            }
            else
            {
                find();

                //calling the new panel
                Edit edit = new Edit(1);
                setVisible(false);
                edit.setVisible(true);
            }
        }
}

Recommended Answers

All 3 Replies

I think the problem is because you havent closed your last stream of the file so the new panel cannot read the file as its in use by the other try::

   try
        {
            file = new File(fileName);
            br = new BufferedReader(new FileReader(file));
            while ((line = br.readLine()) != null)
            {
                if(line.startsWith(edit))
                {
                    newLine = line;
                }
            }
            editContact = newLine.split("&");
            ln=editContact[0];
            fn=editContact[1];
            a=editContact[2];
            c=editContact[3];
            s=editContact[4];
            z=editContact[5];
            pn=editContact[6];
        }
        catch(IOException e)
        {
        }finally {//executes this whether exception is thrown or not
        br.close();
        }

Please post the full text of the error message and a complete source a program that compiles, executes and shows the problem.

@cOrRuPtG3n3t!x that didn't work but thanks for your help

What I had to do was

call the second panel which I put in a method then setting the original panel to false and the second panel to true.

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == editButton)
        {
            if(fNTextField1.getText().trim().equals("") || lNTextField1.getText().trim().equals(""))
            {
                message.setText("Please enter First Name and Last Name");
            }
            else
            {

                if(find() == true)
                {
                    secondScreen();
                    contentPane.setVisible(false); //firstPanel
                    contentPane2.setVisible(true); //secondPanel
                }
                if(find() == false)
                {
                    message.setText(fNTextField1.getText() + " " + lNTextField1.getText() + " is not in the contact list");
                }



            }
        }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.