Note that there are some explanatory texts on larger screens.

plurals
  1. POnullpointer exception on table.getSelectedRow()
    primarykey
    data
    text
    <p>I have a class A and a class B. In class A there is a constructor:</p> <pre><code>public A() { getSelectedRow(); } </code></pre> <p>This constructor calls:</p> <pre><code>public int getSelectedRow() { System.out.println("The row is : " + table.getSelectedRow()); return table.getSelectedRow(); } </code></pre> <p>Up to here everything works fine! The class B then calls the method <code>getSelectedRow()</code> like that:</p> <pre><code> A results = new A(); System.out.println("YEAH! IT'S: " + results.getSelectedRow()); </code></pre> <p>I just want to find out the selected table row from class A. The problem is that I am getting a null pointer exception and i dont know why. if I dont call the method everything works fine.</p> <p>CLASS A:</p> <pre><code>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 &lt;= 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 &lt;= 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 &lt; 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(); } } </code></pre> <p>CLASS B:</p> <pre><code>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) &gt; 0) { fos.write(buffer); } fos.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null &amp;&amp; !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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload