Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8668583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:15:00+00:00 2026-06-12T18:15:00+00:00

I have one class extending JScrollPane, that’s creating an object of another class that

  • 0

I have one class extending JScrollPane, that’s creating an object of another class that extends JTable. Basically it looks like this:

class CustomScrollPane{
   private CustomTable table

   public CustomScrollPane(..){
   table = new CustomTable(this);
   ..
   }
   public void scrollToBottom(){
      ...
   }
}

In the CustomTable class I override tableChanged:

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();
}

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

     if (scrollPane != null)

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

        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();
        }
    };

directly in the CustomScrollPane constructor also works. Why can’t a break it out into a seperate class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:15:01+00:00Added an answer on June 12, 2026 at 6:15 pm

    It looks like the JTable constructor invokes method tableChanged(...) – which means it is invoked before you’re able to initialize the scrollPane instance variable.

    First, I suggest you take a look at some puzzlers in the book Java Puzzlers – specifically puzzle 51: What’s the Point and maybe puzzle 53: Do Your Thing. They should help you understand what’s going on. Basically, the first line of your CustomTable constructor invokes the JTable constructor (via super()). The JTable constructor is trying to invoke tableChanged – which has been overriden. The overriden tableChanged tries to manipulate scrollPane… but all this is happening on line 1 of your constructor (super()) – before the line this.scrollPane = scrollPane has executed, so scrollPane is still null.

    Next, I suggest using the observer pattern. Here you have two objects – your scroll pane and the custom table – and one needs to be notified when the other is changed. That is textbook observer pattern. Here’s the rough idea:

    File CustomTable.java

    public class CustomTable extends JTable {
    
        // No more scroll pane; only observers
        private List<ChangeListener> listeners = [];
    
        // no more scroll pagne
        public CustomTable(){
            super();
        }
    
        @Override
        public void tableChanged(TableModelEvent e) {
            super.tableChanged(e);
            this.fireChangeEvent();
        }
    
        /* new methods */
    
        public void addChangeListener(ChangeListener listener) {
            listeners.add(listener);
        }
    
        public void removeChangeListener(ChangeListener listener) {
            // ...
        }
    
        private void fireChangeEvent() {
            for(String l : listeners ){
                l.onChange();
            }
        }
    }
    

    File CustomScrollPane.java

    class CustomScrollPane implements ChangeListener{
       private CustomTable table
    
       public CustomScrollPane(/*...*/){
           table = new CustomTable();
           table.addChangeListener(this);
           //...
       }
    
       public void scrollToBottom(){
          //...
       }
    
       /* new methods */
    
      @Override
      public void onChange() {
          scrollToBottom();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Thread -extending class that is supposed to run only one instance
I have a design like the own shown below, with one interface extending multiple
I have a custom class that extends the in built WPF Canvas called VisualCanvas
I would like to know if creating a class like the one below would
I have a Class extending JFrame that is watching for a mouse click anywhere:
I have used the draw() method of my SitesOverlay class that is extending the
I am extending an existing C++ project. I have a base class that derives
I have one class. Class First { private Second second; public First(int num, String
I have one class called Global and two other activities. In each activity I
I'm making a program on the HUFFMAN ALGORITHM where I have one class of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.