table.getSelectedRow()上的nullpointer异常

时间:2013-10-03 08:46:17

标签: java

我有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类中选定的表行。问题是我得到一个空指针异常,我不知道为什么。如果我不打电话给方法一切正常。

CLASS 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();

    }

}

CLASS 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);
    }



}

3 个答案:

答案 0 :(得分:0)

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

A results = new A();

table.getSelectedRow()中的值也会被新创建并指向null。

所以要确保你做了一些事情

  

结果=新A(selectedRow);

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

    getSelectedRow(selectedRow);

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

这是因为表对象没有被初始化 尝试在构造函数中初始化表对象....这是一个很好的做法