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