2016-05-01 7 views
1

Ich möchte eine Java Swing-Anwendung, die für die Suche einige Daten (extrahiert aus hierarchischen Datenbank über JDBC) verwendet werden soll. Für Zweck zu testen, habe ich es so gemacht: -Java Swing-Anwendung JList null Ausnahme

Stufe 1: 1, 2, 3 Level-2-: {1.1, 1.2, 1.3}, {2.1, 2.2, 2.3}, {3.1, 3.2 , 3.3} Level-3: {1.1.1, 1.1.2}, {1.2.1, 1.2.2}, ..., {3.3.1, 3.3.2}

UI ist wie folgt: - Level-1 wird in JComboBox angezeigt. Level-2 ist auch eine JComboBox, wird aber über ComboBoxModel gefüllt, abhängig von level-1 selectedIndex. Level-3 Ich möchte in einer JList abhängig von Level-1 anzeigen & Level-2 wie in Level-3.

level-1 & level-2 funktioniert einwandfrei. Aber level-3 gibt 'NullPointerException', wenn ich versuche Level3values ​​Attribute zu initiieren. Hier

ist der Code:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Iterator; 

public class TestSwing extends JPanel implements Runnable, ActionListener { 

    private JComboBox combo1; 
    private JComboBox combo2 = new JComboBox(); 
    private ComboBoxModel[] models ; 
    private JList<String> list; 
    private DefaultListModel<String>[][] level3values; 
    //TODO:add displaying level3, copy method 
    public TestSwing(String[] level1candidates, String[][] level2candidates, String[][][] level3candidates){ 

     this.models = new ComboBoxModel[level1candidates.length]; 
     this.combo1 = new JComboBox(level1candidates); 
     for (int i=0; i<level1candidates.length; i++) 
      models[i] = new DefaultComboBoxModel(level2candidates[i]); 
     for (int i=0; i<level1candidates.length; i++) { 
      for (int j=0; j<level2candidates[i].length; j++) { 
       level3values[i][j] = new DefaultListModel<String>(); 
       for (int k=0; k<level3candidates[i][j].length; k++) { 
        this.level3values[i][j].addElement(level3candidates[i][j][k]); 
       } ;//TODO:DEBUG-how to initiate list model 
      } 
     } 
     combo2.setModel(models[0]); 
     this.list = new JList<String>(new String[]{}); 
     this.add(combo1); 
     this.add(combo2); 
     this.add(list); 
     this.combo1.addActionListener(this); 
     this.combo2.addActionListener(this); 
    } 
    @Override 
    public void actionPerformed(ActionEvent e) { 

     int i = combo1.getSelectedIndex(); 
     combo2.setModel(models[i]); 
     int j = combo2.getSelectedIndex(); 
     list.setModel(level3values[i][j]); 
    } 

    @Override 
    public void run() { 

     JFrame f = new JFrame("CPBG-Store_Material-Finder"); 
     f.setSize(400,400); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel controlPane = new JPanel(); 
     GridLayout gridLayout = new GridLayout(3,2, 10, 10); 
     controlPane.setLayout(gridLayout); 
     controlPane.add(new JLabel("Level-1")); 
     controlPane.add(this.combo1); 
     controlPane.add(new JLabel("Level-2")); 
     controlPane.add(this.combo2); 
     controlPane.add(new JLabel("Level-3")); 
     //controlPane.add(new JLabel("Level-4")); 
     controlPane.add(this.list); 
     f.add(controlPane); 
     //f.pack(); 
     //f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) { 
     String[] level1candidates = new String[]{"1", "2", "3"}; 
     String[][] level2candidates = {new String[]{"1.1", "1.2", "1.3"}, 
       new String[]{"2.1", "2.2", "2.3"}, new String[]{"3.1", "3.2", "3.3"}}; 
     String[][][] level3candidates = { 
       {new String[]{"1.1.1", "1.1.2"},new String[]{"1.2.1", "1.2.2"}, new String[]{"1.3.1","1.3.2"}}, 
       {new String[]{"2.1.1", "2.1.2"},new String[]{"2.2.1", "2.2.2"}, new String[]{"2.3.1","2.3.2"}}, 
       {new String[]{"3.1.1", "3.1.2"},new String[]{"3.2.1", "3.2.2"}, new String[]{"3.3.1","3.3.2"}}, 
       }; 
     //creating and showing this application's GUI. 
     EventQueue.invokeLater(new TestSwing(level1candidates, level2candidates, level3candidates)); 
    } 
} 

Vielen Dank im Voraus für die Hilfe für das Problem debuggen.

Insbesondere comboboxmodel [] funktioniert gut, aber defaultlistmodel [] [] gibt null Zeiger Ausnahme, gibt es einen bestimmten Grund?

Hier ist der Arbeitscode für zwei Ebenen:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.sql.Connection; 
import java.sql.SQLException; 

public class MaterialFinder extends JPanel implements Runnable, ActionListener { 

    private JComboBox combo1; 
    private JComboBox combo2 = new JComboBox(); 
    private ComboBoxModel[] models ; 

    public MaterialFinder(String[] level1candidates, String[][] level2candidates, String[][][] level3candidates){ 

     this.models = new ComboBoxModel[level1candidates.length]; 
     this.combo1 = new JComboBox(level1candidates); 
     for (int i=0; i<level1candidates.length; i++) 
      models[i] = new DefaultComboBoxModel(level2candidates[i]); 
     combo2.setModel(models[0]); 
     this.add(combo1); 
     this.add(combo2); 
     this.combo1.addActionListener(this); 
     this.combo2.addActionListener(this); 
    } 
    @Override 
    public void actionPerformed(ActionEvent e) { 

     int i = combo1.getSelectedIndex(); 
     combo2.setModel(models[i]); 
     int j = combo2.getSelectedIndex(); 

    } 

    @Override 
    public void run() { 

     JFrame f = new JFrame("CPBG-Store_Material-Finder"); 
     f.setSize(400,400); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     f.add(this); 
     //f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) { 

     JDBCUtilities jdbcUtilities = new JDBCUtilities(); 
     jdbcUtilities.dbName = "cpbg"; 
     jdbcUtilities.userName = "root"; 
     jdbcUtilities.password = "root"; 
     jdbcUtilities.serverName = "localhost"; 
     Connection connection = null; 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      System.out.println("Driver Loaded"); 
      connection = jdbcUtilities.getConnection(); 
      //TODO:copy SQL code here 
     } catch (SQLException se) { 

      jdbcUtilities.printSQLException(se); 
     } catch (Exception e) { 

      e.printStackTrace(System.err); 
     } finally { 

      jdbcUtilities.closeConnection(connection); 
     } 
     //TODO:remove the following dummyLevelCandidate 
     String[] level1candidates = new String[]{"1", "2", "3"}; 
     String[][] level2candidates = {new String[]{"1.1", "1.2", "1.3"}, 
       new String[]{"2.1", "2.2", "2.3"}, new String[]{"3.1", "3.2", "3.3"}}; 
     String[][][] level3candidates = { 
       {new String[]{"1.1.1", "1.1.2"},new String[]{"1.2.1", "1.2.2"}, new String[]{"1.3.1","1.3.2"}}, 
       {new String[]{"2.1.1", "2.1.2"},new String[]{"2.2.1", "2.2.2"}, new String[]{"2.3.1","2.3.2"}}, 
       {new String[]{"3.1.1", "3.1.2"},new String[]{"3.2.1", "3.2.2"}, new String[]{"3.3.1","3.3.2"}}, 
       }; 
     //creating and showing this application's GUI. 
     EventQueue.invokeLater(new MaterialFinder(level1candidates, level2candidates, level3candidates)); 
    } 
} 

Antwort

2

Das Problem in dieser Linie ist:

level3values[i][j] = new DefaultListModel<String>(); 

seit level3values ist null. Sie sollten es also richtig mit den richtigen Dimensionen initialisieren, so wie es Ihre Daten wären. Also, bevor die for-Schleifen verschachtelt die Modelle für die Initialisierung sollten Sie es initialisieren:

this.level3values = new DefaultListModel[level2candidates.length][level2candidates.length]; 

** level2candidates.length ist nur ein Beispiel, das zeigt, wie das Problem gelöst werden würde.