0

When I run my program I get this error nullPointerException: null.

import model.*;

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;

public class ButtonPanel extends JPanel implements View
{
private Prison prison;
private LeftInputPanel leftInput;
private DaysPanel days;
private MonthsPanel months;
private YearsPanel years;
private CrimePanel crime;
private AllocateListener aListener;

public ButtonPanel()
{
    setup();
    build();
}

public void setup()
{
}

public void build()
{
    JButton button = new JButton("Allocate Cell");
    Dimension size = new Dimension(240, 70);

    button.setPreferredSize(size);
    button.setMinimumSize(size);
    button.setMaximumSize(size);
    button.addActionListener(aListener);
    add(button);
    update();
}

public void update()
{
    leftInput.update(); //ERROR ON THIS LINE
}

private class AllocateListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
    Criminal criminal = new Criminal(leftInput.name());
    Period period = new Period(days.days(), months.months(), years.years());
    criminal.set(new Crime(crime.getCrime()));
    prison.add(criminal);
}
}
}

My LeftInputPanel - where I call the update method from.

import model.*;
import java.awt.*;
import java.text.*;
import javax.swing.*;

public class LeftInputPanel extends JPanel
{    
public JTextField field = new JTextField();

public LeftInputPanel()
{
    setup();
    build();
}

public void setup()
{
    //setLayout(new GridLayout());
    Dimension size = new Dimension(100, 190);

    setPreferredSize(size);
    setMinimumSize(size);
    setMaximumSize(size);
}

public void build()
{
    setLayout(new FlowLayout(FlowLayout.LEFT));
    JLabel label = new JLabel("          Name");
    field.setPreferredSize(new Dimension(90, 20));
    add(label);
    add(field);

    add(new DaysPanel());
    add(new MonthsPanel());
    add(new YearsPanel());
}

public String name()
{
    return field.getText();
}

public void update()
{
    field.setText("");
}
}

Errors

java.lang.NullPointerException
at ButtonPanel.update(ButtonPanel.java:43)
at ButtonPanel.build(ButtonPanel.java:38)
at ButtonPanel.<init>(ButtonPanel.java:21)
at RightInputPanel.build(RightInputPanel.java:30)
at RightInputPanel.<init>(RightInputPanel.java:14)
at InputPanel.build(InputPanel.java:24)
at InputPanel.<init>(InputPanel.java:13)
at Panel.build(Panel.java:30)
at Panel.<init>(Panel.java:13)
at Window.build(Window.java:31)
at Window.<init>(Window.java:11)
at Root.main(Root.java:9)
2
  • 3
    Please look at the stack-trace and/or use a debugger. This is something you must learn to find/solve yourself. Commented Oct 24, 2009 at 8:39
  • Karen, just check your stacktrace. it gives you [almost]complete information. It will tell you the line number from that you can judge wht is causing the problem Commented Oct 24, 2009 at 9:10

2 Answers 2

4

aListener hasn't been initialized (ie it is still null) and you're trying to add it as an action listener. I suspect all that's required is:

private AllocateListener aListener = new AllocateListener();
Sign up to request clarification or add additional context in comments.

1 Comment

This appears to be spot on, cletus, from the stack trace and following through the code. +1.
2

When you get an exception, you generally get a stack trace, including the full call sequence - that's an invaluable help in tracking down the problem.

If you cannot figure out the problem from that stack trace, you need to supply it to us along with the relevant code, with line numbers.

Then we'll be better able to help you out by finding the problem and, more importantly, showing you how it's done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.