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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:59:49+00:00 2026-06-13T12:59:49+00:00

My application throws NullPointerException. I created connection between MySQL database and my second class.

  • 0

My application throws NullPointerException. I created connection between MySQL database and my second class.

I can’t call DefaultTableModel by method in my second class.
How can I solve this problem?

public class MySQL extends javax.swing.JFrame {
    private DefaultTableModel modelTabeli;

    public MySQL(){
        initComponents();
        BazaDana bd = new BazaDana();

        try{
            modelTabeli = bd.map();
            jTable1.setModel(modelTabeli);
        }
        catch(Exception e){
            System.out.println(e.toString());
        }
    }

    public static void main(String args[]) throws Exception{
        BazaDana bd = new BazaDana();
        bd.readDataBase();
    }
}

Second class

import java.sql.*;
import javax.swing.table.DefaultTableModel;

public class BazaDana{    
    public DefaultTableModel map() throws SQLException
    {
        defaultTableModel = new DefaultTableModel();
        int numberOfColumns = resultSetMetaData.getColumnCount();
        while (resultSet.next())
        {
            Object [] rowData = new Object[numberOfColumns];
            for (int i = 0; i < rowData.length; ++i)
            {
                rowData[i] = resultSet.getObject(i+1);
            }
            defaultTableModel.addRow(rowData);
        }
        return defaultTableModel;
    }
}
  • 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-13T12:59:51+00:00Added an answer on June 13, 2026 at 12:59 pm

    Ok, may I give you an alternative solution if your goal is to separate GUI class from class which queries database. Don’t return DefaultTableModel, return just values from ResultSet through some collection, like this:

    Create MySql GUI class:

    import java.awt.EventQueue;
    import java.util.List;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    
    
    public class MySql extends JFrame{
        JTable table = new JTable();
        DefaultTableModel model = new DefaultTableModel(new Object[][]{},new String[]{"First column","Second column"});
    
        public MySql(){
            table.setModel(model);
            add(new JScrollPane(table));
    
            //Populate table
            BazaDana bd = new BazaDana();
            List<Value> values = bd.selectAll();
            for(Value v : values){
                model.addRow(new Object[]{v.getFirstValue(),v.getSecondValue()});
            }
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable(){
                public void run() {
                    MySql ms = new MySql();
                    ms.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    ms.pack();
                    ms.setVisible(true);
                }});
        }
    }
    

    Then create Java bean class:

    public class Value {
        private int id;
        private String firstValue;
        private String secondValue;
    
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getFirstValue() {
            return firstValue;
        }
        public void setFirstValue(String firstValue) {
            this.firstValue = firstValue;
        }
        public String getSecondValue() {
            return secondValue;
        }
        public void setSecondValue(String secondValue) {
            this.secondValue = secondValue;
        }   
    }
    

    And finally your BazaDana class which queries database:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
    
    public class BazaDana {
    
        public List<Value> selectAll(){
            Connection conn = null;
            Statement st = null;
            ResultSet rs = null;
            //Create list of values
            List<Value> values = new ArrayList<Value>();
    
            try{
                Class.forName("com.mysql.Driver");
                conn =     DriverManager.getConnection("jdbc:mysql://localhost:3306/nameofdb","user","pass");
                st = conn.createStatement();
                rs = st.executeQuery("SELECT * FROM mytable");
    
                while(rs.next()){
                    Value v = new Value();
                    v.setFirstValue(rs.getString("first_column"));
                    v.setSecondValue(rs.getString("second_column"));
                    values.add(v);
                }
            }
            catch(Exception e){
                e.printStackTrace();
            }
            finally{
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
    
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
    
            }
            return values;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following simple method works well on my local machine but throws a NullPointerException
I have an application which throws this error (happens only with xlsx-files): java.lang.NullPointerException at
My application throws 'Access denied' errors when writing temporary files in the installation directory
One of my VB 6.0 application throws an error Out of memory when loading
Whenever I specify a resource using the Spring Data Hadoop namespace, by application throws
I've written an application that runs flawlessly in windows, and throws this error on
i'am using JSF and Spring in a web application. If the Spring Controller throws
I've got a web application that, for performance reasons, throws any data sent into
I created a plain servlet within a seam-gen (2.1.2) application, now I would like
I'm trying to call contacts through the default contacts application in a simple list

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.