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 8987333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:45:54+00:00 2026-06-15T21:45:54+00:00

Good evening I am working with a JTable, taking code from a previous creation

  • 0

Good evening
I am working with a JTable, taking code from a previous creation for this one. What I would like to do is dynamically update the table. How I am doing it is I am storing information in a linkedlist and then going through the list pulling the information to fill the table. The problem is that I am getting a null pointer and I am confused as to why. The start of my stack trace is:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at main$CoverSelection.actionPerformed(main.java:225)
   at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)

Obviously it is happening at line 225 which is reads as:

CartTable.setModel(new DefaultTableModel(data,ColumnNames));

Looking through past questions that are similar I keep seeing people mentioning to make sure that everything is initialized, which I have. The full code:

data = new Object[1][4];
            //CartTable.setModel(new DefaultTableModel(data,ColumnNames));
            while(abc<OrderList.size())
            {
                System.out.println(OrderList.get(abc).getIsbn());
                System.out.println(OrderList.get(abc).GetTitle());
                System.out.println(OrderList.get(abc).getQuantity());
                System.out.println(Double.toString(OrderList.get(abc).getPrice()));
                //data = new Object[1][4];
                //CartTable.setModel(new DefaultTableModel(data,ColumnNames));
                if (data== null)
                {
                    data = new Object[1][4];
                    data[0][0] = OrderList.get(abc).getIsbn();
                    data[0][1] = OrderList.get(abc).GetTitle();
                    data[0][2] = OrderList.get(abc).getQuantity();
                    data[0][3] = Double.toString(OrderList.get(abc).getPrice());
                    CartTable.setModel(new DefaultTableModel(data,ColumnNames));
                }
                else
                {
                    System.out.println("In the else part");

                    Object [][] temp = new Object[data.length+1][4];
                    for (int rowCt =0; rowCt < data.length; rowCt++)
                    {
                        for (int colCt=0; colCt< 4; colCt++)
                        {
                            temp[rowCt][colCt] = data[rowCt][colCt];
                        }
                    }
                    temp[data.length][0] = OrderList.get(abc).getIsbn();
                    temp[data.length][1] = OrderList.get(abc).GetTitle();
                    temp[data.length][2] = OrderList.get(abc).getQuantity();
                    temp[data.length][3] = OrderList.get(abc).getPrice();
                    data = temp;
                    System.out.println("data length in else: "+data.length);
                    CartTable.setModel(new DefaultTableModel(data,ColumnNames));//line 225
            }

The code works and will cycle through the first if statement were the data is null, but then when it goes through it again to get another row through the else statement is when it gives me the error. My initializers which are:

private Object[][] data = null;
private JTable CartTable;
String[] ColumnNames=new String[4];

Any help or suggestions? I think I have looked and covered all my bases, and just cant see it.

  • 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-15T21:45:56+00:00Added an answer on June 15, 2026 at 9:45 pm

    The problem with the line is,

    CartTable.setModel(new DefaultTableModel(data,ColumnNames));
    

    You are just declaring a JTable CartTable and you are not creating object for it any where. Before you set the model just create the table object like,

    JTable CartTable = new JTable();
    

    No need to set data and column names immediately you can later create a model with data and column names and then set this model to the table.

    P.S: Please read the answer post @Vincent Ramdhanie for What is a Null Pointer Exception?. NPE occurring was explained very well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good evening, I'm having an issue with Masonry. This is all my relevant code:
Good evening, I would like to have a navigation bar which is centralised to
Good evening. I need to organize an HashMap, and it is something like this:
Good evening people, I would like some help to compare a big-O and a
Good evening. This code works. It sorts an array of cards by both Suit
Good evening :-)! I have this code to use Drag & Drop method for
Good Evening, I can't seem to get my code to only add one instance
Good evening all, I've been working on an MD5 tool in C# that takes
Good evening, I am working on a program where some application config info is
Good evening, experts I am working on the exercise in programming in generic way.

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.