views:

98

answers:

2

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)
+4  A: 

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();
cletus
This appears to be spot on, cletus, from the stack trace *and* following through the code. +1.
paxdiablo
+2  A: 

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.

paxdiablo