Method called before constructor in overridden JTable
<p>I have one class extending  JScrollPane, that's creating an object of another class that extends JTable. Basically it looks like this:</p>

<pre><code>class CustomScrollPane{
   private CustomTable table

   public CustomScrollPane(..){
   table = new CustomTable(this);
   ..
   }
   public void scrollToBottom(){
      ...
   }
}
</code></pre>

<p>In the CustomTable class I override tableChanged:</p>

<pre><code>public class CustomTable extends JTable{

private CustomScrollPane scrollPane;

public CustomTable(CustomScrollPane scrollPane){
    super();
    this.scrollPane = scrollPane;
}

@Override
public void tableChanged(TableModelEvent e) {
    super.tableChanged(e);
    scrollPane.scrollToBottom();
}
</code></pre>

<p>When I run this I get a NullPointerException on scrollPane in tableChanged(), how is that possible? How can scrollPane be null when it was set in the constructor? Running it in the debugger shows that tableChanged() is called before the constructor. Adding the condition </p>

<pre><code>     if (scrollPane != null)
</code></pre>

<p>actually fixes the problem, because later on the constructor is called. Also, defining the JTable as its constructed, like:</p>

<pre><code>        table = new JTable(){
        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
            final Component c = super.prepareRenderer(new CustomTableCellRenderer(), row, column);
            if (c instanceof JComponent){
                ((JComponent) c).setOpaque(true);
            }
            return c;
        }

        @Override
        public void paint(Graphics g) {
            int scrolling = scrollPane.getViewport().getViewPosition().y;
            super.paint(g);
            g.drawImage(image.getImage(), -30, -50 + scrolling, null, null);
        }

        @Override
        public void tableChanged(TableModelEvent e) {
            super.tableChanged(e);
            scrollPane.scrollToBottom();
        }
    };
</code></pre>

<p>directly in the CustomScrollPane constructor also works. Why can't a break it out into a seperate class?</p>
