Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Java AbstractTableModel NullPointerException.

 
Ranch Hand
Posts: 302
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have an application that show a JTable by button click but I am receiving NullPointerException.
Below my AbstractTableModel:

Definition of JTable:

And my method to generate the table:

The error are ocurring in line 11 of the method:

Thanks in advance.
 
Cezar Apulchro
Ranch Hand
Posts: 302
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, log of the error:
 
Cezar Apulchro
Ranch Hand
Posts: 302
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, after instantiate JTable the nullpointerexception disapear but JTable don't show data of db.

 
Saloon Keeper
Posts: 12825
279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm assuming you initially declared tabela in EXM_Eletrocardiograma, which I assume is the form that the user interacts with.

You are making two mistakes:

  • You are declaring a new tabela variable locally in your event handler. This hides the existing tabela field.
  • You are creating a new JTable instance and updating it with the data from the database. You are not doing anything with the existing table that was referenced by the tabela field.

  • Your original pesquisarActionPerformed() method was fine. Your NullPointerException was caused by something else.

    The line you told us the error was occurring on could not have been the line that threw the exception, going by the stack trace you gave us, because the stack trace omits the constructor invocation of your table model.

    Please tell us what was really on line 606 of your original code.
     
    Cezar Apulchro
    Ranch Hand
    Posts: 302
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi, I instantiate JTable only to test.
    This is my original pesquisar:

    The error are in "tabela.setModel(modeloM03)" line 12
    Last stack trace:
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 12825
    279
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Then tabela is null.

    I think the problem is caused here:

    As in your attempted fix, instead of referring to the field EXM_Eletrocardiograma.tabela, you have declared a new local variable tabela which hides the instance field.
     
    Cezar Apulchro
    Ranch Hand
    Posts: 302
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi, nullpointerexception are solved but records is not show in the table.
     
    Rancher
    Posts: 3232
    30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    First you create a JTable and add it to the scroll pane:



    Then later in your code you create another table:



    But you never add the table to the viewport of the scroll pane. so the table is just sitting in memory and will never be visible.

    Don't create a new JTable.

    Clean up your code. You only ever need to create a reference to the JTable once and add the reference to the viewport of the scroll pane.

    Then, if you want to change the data in the table, then create a new TableModel and set it on the table.
     
    Cezar Apulchro
    Ranch Hand
    Posts: 302
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi, I don't know how I do that. I changed my scrollpane but the problem still the same the records are not shown.
    New scrollpane:

     
    Rob Camick
    Rancher
    Posts: 3232
    30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    I don't know how I do that.


    Do what?

    You know how to:

    1. create a table model
    2. use the setModel(...) method to add the model to a table

    Don't:

    1. create a new JViewport
    2. create a new JScrollPane
    3. create a new JTable.

    You never need to create the viewport. You create the JTable once and you create the JScrollPane once and add the scollpane to your frame when you create your other components.

    Then to change the data in the table you change the model.

    If you need more help then create a SSCCE to demonstrate your problem.

    So you will need a JTable with a dummy DefaultTableModel. You can create the model with a specified number of row/colums.

    Add the table to a scrollpane and add the scroll pane to the frame.

    Then you create a JButton and add it to the frame.

    When you click the button, create a new DefaultTableModel with a different value for the row/columns and then set the model to the table.

    Then entire class will be about 20 lines of code.
     
    Cezar Apulchro
    Ranch Hand
    Posts: 302
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi, I changed my class to use defaulttablemodel but the table don't show the records.
    Below my table:

    Method to show:

    Thanks.
     
    Rob Camick
    Rancher
    Posts: 3232
    30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    As per my suggestion above:

    1. where is the frame?
    2. where is the JButton?
    3. where is the main() method?

    That is not a SSCCE:

    1. we can't copy/paste/compile and test the code
    2. we don't have access to your database


    To test the concept of loading data into a table model you can hard code the data.

    So you use the addRow(...) method with hard coded data.

    Once you understand the concept, then you get the data from the database.

     
    Cezar Apulchro
    Ranch Hand
    Posts: 302
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi,

    1. where is the frame?

    2. where is the JButton?

    3. where is the main() method?


    My JFrame and JInternalFrame are working fine except to show data in JTable.
    In line 13 I have the addRow method.
    Thanks.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 12825
    279
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You misunderstand what Rob means.

    We don't want to see the parts separately. We want to see one whole thing, so we can see for ourselves how all things relate, and also so we can copy and paste it and execute it.

    In short, we want you to write an SSCCE, which Rob has mentioned twice. Please read this: http://sscce.org/

    Seriously, if you want help, read it and then give us an SSCCE. In particular, don't include any methods or fields that aren't needed to demonstrate your problem, but DO include anything required for us to run the program ourselves without modifying it.
     
    Cezar Apulchro
    Ranch Hand
    Posts: 302
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi, if i understand correctly here are the clas to test.
    AbstractTableModel:

    JFrame that use my abstractable:

    In my jframe has only the necessary to test:

    And to see if my ArrayList was loaded:

    Thanks.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 12825
    279
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    No, this is not an SSCCE. Your code is not Short and it is not Self-Contained. From the article that I linked to:

    if the app. has a GUI but the problem is the processing, trim the GUI to a minimal version.


    We don't care that your real table has 6 columns and that none of them are resizable. Trim it down and get rid of the unnecessary layout-code.

    If the code performs I/O to files, replace the file I/O with dummy data structures in problems that are unrelated to input/output.


    We don't have the classes Conectar_Banco_W00 and Login_A00 and we don't want to perform any configuration or set up any databases. It is not necessary to demonstrate your problem. Instead of connecting to a database, mock the data returned from the database by using a hard-coded collection instead of a database connection.

    Remember, the purpose of an SSCCE is that we can copy and paste the code in a text editor, compile it and run it immediately without having to configure anything. Make it easy for us to help you.
     
    Rob Camick
    Rancher
    Posts: 3232
    30
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here is an example of a SSCCE:


    Note:

    1. There is no need for a custom TableModel. The DefaultTableModel will work just fine.
    2. How to create hard coded data to test a concept.

    If you do want to create a custom TableModel then I suggest you follow the steps provided in this example:

    Row Table Model


    If you want generic code to load data from a database then check out:

    Table From Database

    The "TableFromDatabase" should be able to load any data given a proper SQL statement.
     
    Cezar Apulchro
    Ranch Hand
    Posts: 302
    3
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi, problem is solved. My AbstractTableModel was with error in definition. Below is correct version and the method that call of the AbstractTableModel.

    Method:

    Thanks and best regards.
     
    Ranch Hand
    Posts: 41
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In your constructor the List being added to
    new ArrayList
    should be a Collection not List.

    "pacConsulta" should be a Collection object or cast
    List to a Collection

    This is where
    public TabelaModelo_M03(List<Pacientes_A01> pacConsulta)
      {
          this.linhaDePacientes = new ArrayList<>(pacConsulta);
      }

     
    If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
    SKIP - a book about connecting industrious people with elderly land owners
    https://coderanch.com/t/skip-book
    reply
      Bookmark Topic Watch Topic
    • New Topic