0

I am a Java novice building a multiple Frame TabbedPane application with multiple panels. On one of my one of panels I am using a JList and attempting to populate it using a query.

I am using SQLite as the DB.

private JList<String> listTTSearch;

public void loadList() {
    try {
        String query = "select tt_name from TriTiers";
        PreparedStatement pst = dbConn.prepareStatement(query);
        ResultSet rs = pst.executeQuery();
        DefaultListModel<String> DLM = new DefaultListModel<String>();

        while(rs.next()){
            DLM.addElement(rs.getString("tt_name"));
        }
        listTTSearch.setModel(DLM);
        pst.close();
        rs.close();

    } catch (Exception refreshTable) {

    }
}

No errors are thrown and the JList, listTTSearch is not being populated. Other aspects of my application are working well with the DB.

A similar posts seems to address this issue Java JList and list problems , but does not seem to plausible to fix my issue.

Any assistance or guidance would be greatly appreciated.

Community
  • 1
  • 1
EricG
  • 65
  • 9
  • 2
    `No errors are thrown` - how do you know? You have an empty catch block. After fixing that, the next thing to do is hardcode the data in your list and get that working. Once you get that working you can then work on getting the data dynamically from a database. It things work as you suggest, then you probably have multiple reference to the JList and you are setting the model to a JList that is not visible in the GUI. Also variable names ("DLM") should NOT start with an upper case character. Most of your names are correct. Be consistent!!! – camickr Dec 30 '15 at 03:31
  • I added `refreshTable.printStackTrace()` in the catch block. It was then, I was able to identify a NullPointer error. That lead me to add a line in the try block of `dbConn = sqliteConnection.dbConnedtor();` and the list populated. – EricG Dec 30 '15 at 04:11
  • i also change the `DefaultListModel DLM = new DefaultListModel();` to `DefaultListModel dlm = new DefaultListModel();` (lower case dlm) - Many thanks! – EricG Dec 30 '15 at 04:13
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Dec 30 '15 at 04:45
  • The original question was not about stack trace or null pointer exception, so I do not see why this would get flagged as a duplicate. The question was about "what am I missing". @camickr triggered my action to trace the stack, which helped me to find the null pointer issue. So what can be learned by others here? Make certain you catch exceptions, trace the stack for exception, and address one (1) issue at a time. – EricG Dec 30 '15 at 16:59
  • Is there a stackoverflow for newbies? Getting beat down is not fun, at all. – EricG Dec 30 '15 at 17:00

0 Answers0