0

Please take a look in my code as below:

        Vector<Vector<String>> AllSubjects = new Vector<>() ;  // PROBLEM IS HERE

        if ((AllSubjects = EMSSubjectDAO.SelectAllSubjectsByCourse(this.ActiveCourse.getCourseID())) != null)
        {
            SubjectTableModel = new DefaultTableModel(AllSubjects, SubjectTableHeader);

            SubjectTable.setModel(SubjectTableModel);

            //this.SubjectTable = new JTable(AllSubjects, SubjectTableHeader) ;
        }

On the // PROBLEM IS HERE line, if I init Vector<Vector<String>> AllSubjects = new Vector<>() ;, the new DefaultTableModel(AllSubjects, SubjectTableHeader); will run fine.

But if I write Vector<Vector<String>> AllSubjects = null, the new DefaultTableModel(AllSubjects, SubjectTableHeader); will erase all data in AllSubjects and nothing will be initialized

I really want to understand why this happen? I guess if we dont init Vector<Vector<String>> properly, Java would not treat the object as expected, is it right?

Hope you guys can help me on this, thanks in advanced

3
Contributors
3
Replies
15
Views
3 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • The API requires you to pass a Vector of Vectors. If you do that, it runs OK. If you pass a null it fails. Why is this surprising? Remember AllSubjects is a reference. It's supposed to point to a Vector of Vectors. If you init it to null then it … Read More

1

The API requires you to pass a Vector of Vectors. If you do that, it runs OK. If you pass a null it fails. Why is this surprising?
Remember AllSubjects is a reference. It's supposed to point to a Vector of Vectors. If you init it to null then it doesn't point to Vector of Vectors, so you have violated the conditions of the API, and the code doesn't work. It really is as simple as that!

Edited by JamesCherrill

0

@Tu Dinh - virtual -100 in compare with nice crying in last post

  1. read Oracle tutorial How to use Tables for working code examples, there are mentioned in all parts how the XxxTableModel works

  2. logics could be add Vector<Object> to Vector<Vector<Object>>,

  3. have to test if EMSSubjectDAO returns util.List or Vector

0

On a second reading, I'm less clear about this. The code as posted overwrites the initial value of AllSubjects before it is used, so the initialisation is irrelevant. Presumably this code was exctracted from the real program, and isn't quite right?

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.