JTable Problem

mywaymylife

Grünschnabel
Hallo,

ich wünsch euch Frohes Fest :D.

Nun zu meinen Problem...

Ich habe das Tutorial JTable Access mir angeschaut und in meinen Programm übernommen.
das Problem ist nur das er mir einen Fehler ausgibt.
Er findet angeblich nichts!

Code:
java.lang.NullPointerException
        at mealContent.MealList.loadData(MealList.java:120)
        at mealContent.MealList.<init>(MealList.java:33)
        at frames.AdminFrame.actionPerformed(AdminFrame.java:212)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
        at javax.swing.AbstractButton.doClick(AbstractButton.java:302)
        at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1000)
        at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1041)
        at java.awt.Component.processMouseEvent(Component.java:5488)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
        at java.awt.Component.processEvent(Component.java:5253)
        at java.awt.Container.processEvent(Container.java:1966)
        at java.awt.Component.dispatchEventImpl(Component.java:3955)
        at java.awt.Container.dispatchEventImpl(Container.java:2024)
        at java.awt.Component.dispatchEvent(Component.java:3803)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
        at java.awt.Container.dispatchEventImpl(Container.java:2010)
        at java.awt.Window.dispatchEventImpl(Window.java:1774)
        at java.awt.Component.dispatchEvent(Component.java:3803)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)


Die zeile wo der Fehler auftritt:
Code:
rs = state.executeQuery(sql);

Die Methode:
Code:
private void loadData()
  {
      try {
            openDBConnection();
            String sql = "SELECT * FROM Nahrungsmittel;";
            rs = state.executeQuery(sql);            
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            closeDBConnection();
        }
      
        showData(rs, dtm);        
        table_food.setModel(dtm);        
        table_food.updateUI();
        closeDBConnection();
  }

Die Tabelle ist vorhanden. Die DB ist offen und eine Connection ist auch offen. Da ich ncoh ein zimelicher neuling bin, weiß ich nicht wo der Fehler ist. Es sind auch leider keine Kommentare beim Tutorial.

Hier nochmal die Variabeln:

Code:
    private ResultSet rs;
    private Statement state;
    private DefaultTableModel dtm;
    private static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
    private static final String URL = "jdbc:odbc:nahrung";
    private Connection con;

MFG
 
Zuletzt bearbeitet:
Ist die Variable 'state' eine Membervariable?
Wird sie in 'openDBConnection();' initialisiert?

Es muss nämlich fast sein dass diese Null ist wenn es genau in dieser Zeile geworfen wird. Laut dieser Doku wirft die Methode 'executeQuery' niemals eine NullPointerException.

Ich würde an deiner Stelle mal den Debugger drüber laufen lassen und dir die Eigenschaften der Objekte in den kritischen Bereichen ansehen.

Lg, Yanick
 
also die wird nicht in openDBConnection initialisiert. in openDBConnection wird nur die verbingund zwischen programm und DB hergestellt.

Ansonsten, kann es ja sein das er nichts aus der Datenbank herraus bekommt, aber es kann nicht sein da die tabelle gefüllt ist.

Und die SQL anweisung ist auch richtig mit der richtigen Tabelle.
 
Also...

Du sagst die NullPointerException wird in dieser:
Code:
rs = state.executeQuery(sql);
Zeile geworfen.

Da 'Statement.executeQuery(String)' keine NullPointerException wirft (laut doku) kann es nur sein, dass die Variable 'state' auf Null zeigt, ansonsten ist es unmöglich dass die Exception in dieser Zeile geworfen wird...

Lg, Yanick
 
Ich versteh das voll nicht. Ich habe die Sachen in 1 übernommen nichts verändert außer das er die SQL anweisung nicht ausm Textfeld holt sondern das ich es vorher festlege.
Ich habe mal ein paar System.out.println dazwischen geklatscht, und naja es sieht so aus:
Code:
  private void loadData() {
      try {
          openDBConnection();
          state = con.createStatement();
          String sql = "SELECT * FROM Nahrungsmittel;";
          System.out.println("state 1: "+state);
          rs = state.executeQuery(sql);
          System.out.println("state 2: "+state);
          System.out.println("result set 1: "+rs);
          closeDBConnection();
          
      } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          closeDBConnection();
      }
      
      showData(rs, dtm);
      System.out.println("state 3: "+state);
      table_food.setModel(dtm);
      System.out.println("state 4: "+state);
      table_food.updateUI();
      System.out.println("state 5: "+state);
      closeDBConnection();
  }

Code:
state 1: sun.jdbc.odbc.JdbcOdbcStatement@192c8d9
state 2: sun.jdbc.odbc.JdbcOdbcStatement@192c8d9
result set 1: sun.jdbc.odbc.JdbcOdbcResultSet@1c05ffd
state 5: sun.jdbc.odbc.JdbcOdbcResultSet@1c05ffd
java.sql.SQLException: ResultSet is closed
        at sun.jdbc.odbc.JdbcOdbcResultSet.checkOpen(Unknown Source)
        at sun.jdbc.odbc.JdbcOdbcResultSet.getMetaData(Unknown Source)
        at mealContent.MealList.showData(MealList.java:146)
        at mealContent.MealList.loadData(MealList.java:130)
        at mealContent.MealList.<init>(MealList.java:26)
        at frames.AdminFrame.actionPerformed(AdminFrame.java:153)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.AbstractButton.doClick(Unknown Source)
        at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
        at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: rsmd is null
        at mealContent.MealList.showData(MealList.java:155)
        at mealContent.MealList.loadData(MealList.java:130)
        at mealContent.MealList.<init>(MealList.java:26)
        at frames.AdminFrame.actionPerformed(AdminFrame.java:153)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.AbstractButton.doClick(Unknown Source)
        at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
        at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
 
Zurück