I'm pretty new to Java, and I'm trying to populate my JList with data from a database. The data is retrieved from the database via the method visKunder() in the Database class.
public ArrayList<Kunde> visKunder(){
String s1 = "select kundenavn, adresse, telefon from kunde";
Statement setning = null;
String kundenavn = null;
String adresse = null;
int telefon = 0;
ResultSet res = null;
ArrayList<Kunde> kunder = new ArrayList<>();
try{
forbindelse.setAutoCommit(false);
setning = forbindelse.createStatement();
res = setning.executeQuery(s1);
while(res.next()){
kundenavn = res.getString("kundenavn");
adresse = res.getString("adresse");
telefon = res.getInt("telefon");
Kunde kunde = new Kunde(kundenavn, telefon, adresse);
kunder.add(kunde);
}
forbindelse.commit();
}catch(SQLException error){
Opprydder.rullTilbake(forbindelse);
Opprydder.skrivMelding(error, "visKunder()");
}finally{
Opprydder.lukkSetning(setning);
Opprydder.lukkResSet(res);
try {
forbindelse.setAutoCommit(true);
} catch (SQLException ex) {
Opprydder.skrivMelding(ex, "setAutoCommit()");
}
}
return kunder;
}
The problem occurs when I invoke visKunder() in the GUI class RegVedKjøp. printStackTrace() lists visKunder() as the problem for the NullPointerException.
public class RegVedKjøp extends javax.swing.JFrame {
private static Database d1;
public RegVedKjøp(Database d1) {
initComponents();
this.d1 = d1;
}
private void initComponents(){
...
JList jList2 = new JList();
fyllListe(jList2);
...
}
private void fyllListe(JList list){
ArrayList<Kunde> kunder = d1.visKunder();
list = new JList(kunder.toArray());
}
I don't know if it's relevant, but I used NetBeans' GUI Builder to design the GUI. Any tips on how to fix this issue would be very appreciated.
EDIT:
private void initComponents() {
scrollbar1 = new java.awt.Scrollbar();
lagreKnapp = new javax.swing.JButton();
tKnapp = new javax.swing.JButton();
label1 = new java.awt.Label();
jLabel2 = new javax.swing.JLabel();
txtType = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
txtAntall = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
txtPris = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
jList2 = new JList();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Registrer kjøp av ved - Dagsverket");
setAlwaysOnTop(true);
setResizable(false);
lagreKnapp.setFont(new java.awt.Font("sansserif", 0, 14)); // NOI18N
lagreKnapp.setText("Lagre");
lagreKnapp.setPreferredSize(new java.awt.Dimension(95, 50));
lagreKnapp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lagreKnappActionPerformed(evt);
}
});
tKnapp.setFont(new java.awt.Font("sansserif", 0, 14)); // NOI18N
tKnapp.setText("Tilbake");
tKnapp.setPreferredSize(new java.awt.Dimension(75, 30));
tKnapp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tKnappActionPerformed(evt);
}
});
label1.setText("label1");
jLabel2.setFont(new java.awt.Font("sansserif", 1, 14)); // NOI18N
jLabel2.setText("Vedtype:");
jLabel2.setFocusable(false);
jLabel2.setPreferredSize(new java.awt.Dimension(70, 25));
jLabel1.setFont(new java.awt.Font("sansserif", 1, 14)); // NOI18N
jLabel1.setText("Antall sekker:");
jLabel1.setFocusable(false);
jLabel1.setName(""); // NOI18N
jLabel1.setPreferredSize(new java.awt.Dimension(70, 25));
jLabel3.setFont(new java.awt.Font("sansserif", 1, 14)); // NOI18N
jLabel3.setText("Pris pr/sekk:");
jLabel4.setFont(new java.awt.Font("SansSerif", 1, 14)); // NOI18N
jLabel4.setText("Kunde:");
jButton1.setFont(new java.awt.Font("SansSerif", 0, 14)); // NOI18N
jButton1.setText("Registrer Ny Kunde");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
This code is generated by the GUI Builder. I should also state that the this class is one of several that helps display the GUI. Every class is responsible for one operation requested by the user. They are all collected in a class called Mainpage which is used in the class System which runs the whole program. The first thing done in System is the creation of a Database object, and the connection to the database. This Database object is then what is then used as a parameter in every GUI class.
d1
is obviously null
. Why? Well, because you call fyllListe()
method at the initComponents()
method, which is prior to the initialization of d1
field at the constructor, so please just change the order, the proper constructor is:
public RegVedKjøp(Database d1) {
this.d1 = d1;
initComponents();
}