0

我有一个 A 类和一个 B 类。在 A 类中有一个构造函数:

public  A() {
        getSelectedRow();
    }

此构造函数调用:

public int getSelectedRow() {
    System.out.println("The row is : " + table.getSelectedRow());
    return table.getSelectedRow();

}

到这里一切正常!然后类 B 调用这样的方法getSelectedRow()

        A results = new A();
        System.out.println("YEAH! IT'S: " + results.getSelectedRow());

我只想从 A 类中找出选定的表行。问题是我得到一个空指针异常,我不知道为什么。如果我不调用该方法,一切正常。

A类:

public class AllResultsFromDB extends JFrame {

    @SuppressWarnings("compatibility:9056676689615464658")
    private static final long serialVersionUID = 188850508334531506L;


    GUI ins = new GUI();
    JTable table;

    public AllResultsFromDB(GUI x) {
        final Vector columnNames = new Vector();
        final Vector data = new Vector();
        this.ins = x;


        try {


            /** Initializing GUI class
             * in order to call
             * getSelectedTable() method. **/

            Login sgui = new Login();


            String dburl = "jdbc:oracle:thin:@localhost:1521:ORCL";
            Connection connection = DriverManager.getConnection(dburl, sgui.getUsername(), sgui.getPassword());


            //  Fetch data from table specified by user

            String query = "SELECT * FROM " + ins.getSelectedTable() + " ORDER BY id";
            System.out.println(query);
            Statement stmt = connection.createStatement();
            ResultSet rset = stmt.executeQuery(query);
            ResultSetMetaData metad = rset.getMetaData();
            int columns = metad.getColumnCount();


            //  This loop gets the names of the columns

            for (int i = 1; i <= columns; i++) {
                columnNames.addElement(metad.getColumnName(i));
            }


            //  This loop gets the data inside the rows

            while (rset.next()) {
                final Vector row = new Vector(columns);


                for (int i = 1; i <= columns; i++) {
                    row.addElement(rset.getObject(i));

                }

                data.addElement(row);
            }

            rset.close();
            stmt.close();
            connection.close();


            //  Create table with results

            table = new JTable(data, columnNames) {

                    public boolean isCellEditable(int row, int col) {

                        return false;
                    }


                    public Class getColumnClass(int column) {

                        for (int row = 0; row < getRowCount(); row++) {
                            Object obj = getValueAt(row, column);
                            if (obj != null) {

                                return obj.getClass();

                            }
                        }

                        return Object.class;
                    }


                };


            JScrollPane scroll = new JScrollPane(table);
            getContentPane().add(scroll);


            JPanel panel = new JPanel();
            getContentPane().add(panel, BorderLayout.SOUTH);


            table.addMouseListener(new MouseListener() {

                public void mousePressed(MouseEvent e) {

                    //System.out.println(table.getSelectedRow());

                }

                public void mouseReleased(MouseEvent e) {
                    //System.out.println(table.getSelectedRow());

                }

                public void mouseEntered(MouseEvent e) {
                    //System.out.println(table.getSelectedRow());

                }

                public void mouseExited(MouseEvent e) {
                    //System.out.println(table.getSelectedRow());
                }

                public void mouseClicked(MouseEvent e) {

                    getSelectedRow();
                    if (e.getClickCount() == 2) {


                        //System.out.println(table.getSelectedRow());

                        Profile profile = new Profile();

                        try {

                            profile.getData();
                            //wait(500000);
                            profile.getImage();

                        } catch (Exception f) {
                        }
                        profile.setVisible(true);
                    }

                }
            });


        } catch (SQLException e) {
        }

    }

   public  AllResultsFromDB(int x) {
        x = getSelectedRow();
        System.out.println(table.getSelectedRow());
    }


    public int getSelectedRow() {
        System.out.println("The row is : " + table.getSelectedRow());
        return table.getSelectedRow();

    }

}

B类:

public class Profile extends JFrame {


    AllResultsFromDB results = new AllResultsFromDB();

    public Profile(AllResultsFromDB x) {
        this.results=x;
        try {
            getData();
            getImage();

        } catch (Exception e) {
            e.printStackTrace();
        }


        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getImage() throws Exception {
        JLabel label;
        Image img;
        ImageIcon pic;
        JPanel panel;
        img = new ImageIcon("java.jpg").getImage();
        pic = new ImageIcon(img);
        label = new JLabel("", pic, JLabel.CENTER);
        panel = new JPanel(new BorderLayout());
        panel.setBounds(new Rectangle(0, 0, 340, 310));
        panel.add(label, null);
        panel.add(label, BorderLayout.CENTER);
        this.getContentPane().setLayout(null);
        this.setSize(new Dimension(1148, 336));
        this.getContentPane().add(panel, null);


    }

    public void getData() throws Exception {
        String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
        String username = "c##lambros";
        String password = "16111111";
        Connection conn = null;

        try {



            System.out.println("YEAH! IT'S: " + results.getSelectedRow());



            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection(url, username, password);

            String sql = "SELECT foto FROM criminals WHERE id = 5";
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet resultSet = stmt.executeQuery();
            while (resultSet.next()) {
                //String name = resultSet.getString(1);
                //System.out.println("Name        = " + name);


                File image = new File("java.jpg");
                FileOutputStream fos = new FileOutputStream(image);

                byte[] buffer = new byte[256];

                //
                // Get the binary stream of our BLOB data
                //
                InputStream is = resultSet.getBinaryStream(1);
                while (is.read(buffer) > 0) {
                    fos.write(buffer);
                }

                fos.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
        }
    }

    private void jbInit() throws Exception {
        this.setSize(new Dimension(816, 380));
        JLabel label;
        Image img;
        ImageIcon pic;
        JPanel panel;
        img = new ImageIcon("java.jpg").getImage();
        pic = new ImageIcon(img);
        label = new JLabel("", pic, JLabel.CENTER);
        panel = new JPanel(new BorderLayout());
        panel.setBounds(new Rectangle(0, 0, 340, 310));
        panel.add(label, null);
        panel.add(label, BorderLayout.CENTER);
        this.getContentPane().setLayout(null);
        this.setSize(new Dimension(1148, 336));
        this.getContentPane().add(panel, null);
    }



}
4

3 回答 3

0

在 classB 中,因为您正在创建一个新实例

A results = new A();

table.getSelectedRow() 中的值也是新创建的,并将指向 null。

所以确保你做某事

A 结果 = 新 A(selectedRow);

并在 A 的构造函数中,将参数传递给函数

    getSelectedRow(selectedRow);

请注意:确保“table.selectedRow”的值保持不变

于 2013-10-03T08:55:27.213 回答
0

如果 table 是您在 A 类中的实例变量,那么当您尝试在 A 的构造函数中访问它时,它可能不会被初始化。从构造函数调用 getSelectedRow 也没有任何意义。

尝试在构造函数中初始化表变量而不是调用该方法,它应该在它之后工作。

于 2013-10-03T08:57:33.990 回答
0

这是因为表对象没有被初始化。
尝试在构造函数中初始化表对象......这是一个好习惯

于 2013-10-03T09:58:05.817 回答