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

  • Home
  • SEARCH
  • 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 6071585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:03:51+00:00 2026-05-23T10:03:51+00:00

I’m getting an error and the gui does not show up correctly when I

  • 0

I’m getting an error and the gui does not show up correctly when I add an object TestData to an ArrayList, here is my code:

(The error appears at lstTest.add(new TestData("Jon", 0));)

The Main class:

import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import javax.swing.event.*; 
import javax.swing.*;
import java.util.*;
import javax.swing.border.*;
public class Test extends JFrame implements ActionListener, ListSelectionListener
{
    private ArrayList<TestData> lstTest = null;
    private TestData objTest;
    public TestTable panTestTable;
    private ListSelectionModel lsmTest;
    private JButton btnAdd;
    private static final String Command_Add = "Add";

    public Test()
    {
        super("Test");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        try{  UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); }
        catch (Exception e){  System.out.println("Unable to load Windows look and feel"); }
        setPreferredSize(new Dimension(500, 400));
        ((JPanel) getContentPane()).setBorder(new EmptyBorder(13, 13, 13, 13) );
        JPanel panMain = (JPanel) getContentPane();

        lstTest = new ArrayList<TestData>();
        lstTest.add(new TestData("Jon", 0));
        panTestTable = new TestTable(lstTest);
        lsmTest = panTestTable.tabTest.getSelectionModel();
        lsmTest.addListSelectionListener(this);
        panTestTable.setBorder(new EmptyBorder(1, 1, 1, 1));

        panMain.add(panTestTable, "Center");
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(false);
    }

    public void actionPerformed(ActionEvent e){}

    public void valueChanged(ListSelectionEvent e){}

    public static void main(String[] args)
    {
        new Test();
    }
}

TestTable class:

import java.util.*; 
import javax.swing.table.*; 
import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;

public class TestTable extends JPanel
{
    public ArrayList<TestData> lstTestData;
    public JTable tabTest;
    public BillTableModel absTest;
    public JScrollPane scrollTest;

    public TestTable(ArrayList<TestData> lstTest)
    {
        lstTestData = lstTest;
        absTest = new BillTableModel(lstTestData);
        tabTest = new JTable(absTest);

        tabTest.setFillsViewportHeight(true);
        tabTest.setAutoCreateRowSorter(true);
        tabTest.setRowSelectionAllowed(true);
        tabTest.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tabTest.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        tabTest.setPreferredScrollableViewportSize(new Dimension(250, 159));

        scrollTest = new JScrollPane(tabTest);
        scrollTest.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        add(scrollTest);

        setOpaque(true);
    }

    public void SetData(ArrayList<TestData> lstTestData)
    {  
        this.lstTestData = lstTestData;
        absTest.SetData(lstTestData); 
    }
    public int SelectRow()
    {
        return tabTest.getSelectedRow();
    }
    public int GetSelectedRow()
    {
        return tabTest.getSelectedRow();
    }
    public void DeleteSelectedRow()
    {
        try
        {
           lstTestData.remove(tabTest.getSelectedRow());
           SetData(lstTestData);
        }
        catch(Exception ex){}
    }
    public void DeleteAllRows()
    {
         for( int i = absTest.getRowCount() - 1; i >= 0; i-- )
         {
              lstTestData.remove(i);
              SetData(lstTestData);
         }
    }


    public class BillTableModel extends AbstractTableModel
    {  
        protected String[] columnNames = new String[ ] {"Name", "Payment"};
        protected ArrayList<TestData> lstTestData;
        protected Class[] types = new Class[]{String.class, double.class};

        public BillTableModel(ArrayList<TestData> lstTestData)
        {    this.lstTestData = lstTestData;    }

        public void SetData(ArrayList<TestData> lstTestData)
        {    this.lstTestData = lstTestData; fireTableDataChanged();    }

        @Override
        public String getColumnName(int columnIndex)
        {    return columnNames[columnIndex];   }

        @Override
        public Class getColumnClass(int columnIndex)
        {    return types [columnIndex];   }

        @Override
        public boolean isCellEditable(int row, int columnIndex)
        {    if (columnIndex != 1) return false;  else return true;    }

        public Object getValueAt(int row, int column)
        {
            if (row < 0 || row > lstTestData.size()) return null;
            TestData obj = lstTestData.get(row);
            switch(column)
            {
                case 0: return obj.getName();
                case 1: return obj.getPayment();
                default: return null;
            }
        }

        public int getRowCount() {  return lstTestData.size();   }
        public int getColumnCount() {  return columnNames.length;   }
    }
}

TestData class:

public class TestData implements Comparable <TestData>
{
    private String name;
    private double payment;

    public TestData(String name, double payment)
    {
        this.name = name;
        this.payment = payment;
    }

    public String getName() {return name;}
    public double getPayment() { return payment; }

    public void setName(String s) {name = s;}
    public void setPayment(double d) { payment = d; }

    @Override
    public int compareTo(TestData obj)
    {
      return name.compareTo(obj.getName());
    }

    @Override
    public boolean equals(Object obj)
    {
         if (obj instanceof TestData == false){ return false;}
         return name.equals(((TestData)obj).getName());
    }

}

Could you help me to figure out the problem?

EDIT:

Here is the error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.JTable.prepareRenderer(JTable.java:5720)
    at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2072)
    at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1974)
    at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1770)
    at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143)
    at javax.swing.JComponent.paintComponent(JComponent.java:752)
    at javax.swing.JComponent.paint(JComponent.java:1029)
    at javax.swing.JComponent.paintChildren(JComponent.java:862)
    at javax.swing.JComponent.paint(JComponent.java:1038)
    at javax.swing.JViewport.paint(JViewport.java:747)
    at javax.swing.JComponent.paintChildren(JComponent.java:862)
    at javax.swing.JComponent.paint(JComponent.java:1038)
    at javax.swing.JComponent.paintChildren(JComponent.java:862)
    at javax.swing.JComponent.paint(JComponent.java:1038)
    at javax.swing.JComponent.paintChildren(JComponent.java:862)
    at javax.swing.JComponent.paint(JComponent.java:1038)
    at javax.swing.JComponent.paintChildren(JComponent.java:862)
    at javax.swing.JComponent.paint(JComponent.java:1038)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
    at javax.swing.JComponent.paintChildren(JComponent.java:862)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1479)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1410)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
    at javax.swing.JComponent.paint(JComponent.java:1015)
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
    at java.awt.Container.paint(Container.java:1780)
    at java.awt.Window.paint(Window.java:3375)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

EDIT2:

and here is the gui that poped up when I run the program:

enter image description here

EDIT3:

and here is the gui without errors after removing lstTest.add(new TestData("Jon", 0)); from the code:

enter image description here

  • 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-05-23T10:03:51+00:00Added an answer on May 23, 2026 at 10:03 am

    in BillTableModel where you create an array of Class, you should not use primitive types.
    use Double.class instead of double.class.

    this fixes your problem.

    public class BillTableModel extends AbstractTableModel {
        protected String[] columnNames = new String[] { "Name", "Payment" };
        protected ArrayList<TestData> lstTestData;
        protected Class[] types = new Class[] { String.class, Double.class };
        ....
        ....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.