0

Im currently working with JDBC and Swing to create a GUI and getting values from a local database.I'm fetching data from the database and want to put it in a JComboBox but there is some kind of problem with transfering the data from one class to another. I've checked and the data is coming from the db correctly so that's not the issue. When I run the program I get NullPointerException and I have no idea why. This is the code in Model class the creates problem:

this.ba = IO.getBetalningsansvarig();

DB-communication class:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class dbIO {

//Code for fetching data from the database and putting it into an ArrayList.
    public ArrayList<String> getBetalningsansvarig() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        ArrayList<String> asd = new ArrayList<String>();
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
        String connectionUrl = "jdbc:sqlserver://localhost;" + "database=INL5";
        Connection conn = DriverManager.getConnection(connectionUrl, "testadmin", "test");
        System.out.println("ANSLUTNINGEN LYCKADES");
        Statement stmt = conn.createStatement();
        String namn;
        ResultSet rs = stmt.executeQuery(
                "SELECT Namn "
                + "FROM Betalningsansvarig ");

        while (rs.next()) {
            namn = rs.getString("Namn");
            asd.add(namn);
        }
        for (String s : asd) {
            System.out.println(s);
        }
        stmt.close();
        conn.close();

        return (asd);
    }

}

Model class:

import java.sql.SQLException;
import java.util.ArrayList;

public class Model {
    private dbIO IO = new dbIO();
    private ArrayList<String> ba = new ArrayList<String>();

    public void setBa(ArrayList<String> ba) {
        this.ba = ba;
    }
    public ArrayList<String> getBa() {
        return ba;
    }

    public Model() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        this.ba = IO.getBetalningsansvarig();

    }
}

Main class:

import java.sql.SQLException;


public class Main {
    public static void main(String[] args) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        dbIO d = new dbIO();
        //d.start();
        Model m = new Model();
        View1 v = new View1();
        v.setModel(m);
        v.open();

    }

}

View class:

import java.sql.SQLException;
import java.util.ArrayList;

public class View1 extends javax.swing.JFrame {
    private Model m;
    private ArrayList<String> betalningsansvariga = new ArrayList<String>();
    private jComboBox1;

    public View1() {
        initComponents();
        betalningsansvariga = m.getBa();
        for(String s : betalningsansvariga) {
        JComboBox1.addItem(s);
     }
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        list1 = new java.awt.List();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(list1, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(526, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(list1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(272, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    public void setModel(Model m) { this.m = m; }
    public void open() {       
        setVisible(true);
    }

}

The error message:

Exception in thread "main" java.lang.NullPointerException
    at Model.<init>(Model.java:17)
    at Main.main(Main.java:9)
Java Result: 1
  • It might be a good idea to supply the stack trace of the Nullpointer and perhaps mark in your code via a comment where the lines references in that stack trace are. – Florian Schaetz Mar 19 '15 at 15:19
  • It could be a lot of things, the problem comes from getBetalningsansvarig() but we can' t know where exactly without the stack trace. please provide the whole error message. – Mel Mar 19 '15 at 15:31

3 Answers3

0

You are instantiating your dbIO object in main, but not in Model.

Troncoso
  • 2,343
  • 3
  • 33
  • 52
0

You have not initialized your member in class Model:

private dbIO IO; // effectively null

Maybe you want to better define it as global static constant using modifiers static final. If it is in the namespace of class Model then you can refer to it by the expression Model.IO (after having initialized it in your main-method).

And please try to follow the standard naming schemes in Java. Write class names starting with big letters for better readability.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • I changed it to private dbIO IO = new dbIO(); Didn't help though. – user3428812 Mar 19 '15 at 17:28
  • @user3428812 What does "didn't help" mean? Do you still observe the same exception or rather a new different exception? – Meno Hochschild Mar 19 '15 at 17:47
  • Exactly the same exception. No change. – user3428812 Mar 19 '15 at 17:56
  • @user3428812 Based on what you have given here as information so far your problem is NOT reproducible. It seems to me that you could profit very much from a tutorial course about basic Java. The link given above as "duplicate" might also help you. Good luck! – Meno Hochschild Mar 20 '15 at 11:27
0

In Model class:

Model() constructor is invoking method on dbIO object. dbIO object is null.

So you are are encountering NullPointerException.


In addition to above observation, there are couple of places where you encountering NullPointerException:

Inside View1() constructor, you are again invoking methods on null objects [ Model, JComboBox are null].

Correct your code accordingly.

Rajesh
  • 2,135
  • 1
  • 12
  • 14