1 Reply Latest reply on Feb 13, 2003 6:13 PM by 807549

    Can't display from database to text area

    807549
      At public void displayResult(ResultSet rs) have managed to display results in the console window but really want to pass them to the theResult text area?

      Not sure what the problem is only that it keeps displaying:

      java.lang.NullPointerException

           at CatsApplication.MainMenu.displayResult(MainMenu.java:250)

           at CatsApplication.MainMenu.makeSelection(MainMenu.java:207)

           at CatsApplication.MainMenu.<init>(MainMenu.java:113)

           at CatsApplication.MainMenu.main(MainMenu.java:264)

      -------------------------------------

      import java.awt.*;
      import java.awt.event.*;
      import java.sql.*;

      public class MainMenu extends Frame implements ActionListener, WindowListener
      {

      static final String DB = "jdbc:odbc:CatsApplication";
      static final String USER = "";
      static final String PASSWORD = "";

      Connection theConnection;// object that will encapsulate the database

      private MenuBar myMenuBar;
      private Menu professional, account;
      private MenuItem newProfessional, updateProfessional, deleteProfessional;
      private MenuItem newAccount, updateAccount, deleteAccount;
      private NewProfessional professionals;
      private TextDialog dialog1;
      private AccountDialog dialog2;
      private String message, text, query;
      private TextArea theResult;
      public MainMenu()
      {
      setTitle("CATS Application");


      Panel detailsPane = new Panel();
      detailsPane.setLayout(new BorderLayout());
      TextArea theResult = new TextArea(20, 64);
      theResult.setEditable(false);
      detailsPane.add(theResult);
      add(detailsPane);

      myMenuBar = new MenuBar();
      professional = new Menu("Professional");
      myMenuBar.add(professional);

      newProfessional = new MenuItem ("New Professional");
      professional.add(newProfessional);
      newProfessional.addActionListener(this);

      professional.addSeparator();

      updateProfessional = new MenuItem ("Update Professional");
      professional.add(updateProfessional);
      updateProfessional.addActionListener(this);

      professional.addSeparator();

      deleteProfessional = new MenuItem ("Delete Professional");
      professional.add(deleteProfessional);
      deleteProfessional.addActionListener(this);

      /**
      * Report for later iteration
      *
      * professional.addSeparator();
      *
      * viewProfessional = new MenuItem ("View Professional");
      * professional.add(viewProfessional);
      * viewProfessional.addActionListener(this);
      **/

      account = new Menu("Account");
      myMenuBar.add(account);

      newAccount = new MenuItem ("New Account");
      account.add(newAccount);
      newAccount.addActionListener(this);

      account.addSeparator();

      updateAccount = new MenuItem ("Update Account");
      account.add(updateAccount);
      updateAccount.addActionListener(this);

      account.addSeparator();

      deleteAccount = new MenuItem ("Delete Account");
      account.add(deleteAccount);
      deleteAccount.addActionListener(this);

      /**
      * Report for later iteration
      *
      * account.addSeparator();
      *
      * viewAccount = new MenuItem ("View Account");
      * account.add(viewAccount);
      * viewAccount.addActionListener(this);
      **/

      setMenuBar(myMenuBar);
      dialog1=new TextDialog(this,"New Professional","Professional Profile");
      this.addWindowListener(this);

      dialog2 = new AccountDialog(this, "New Account", "Account Profile");
      this.addWindowListener(this);
      getConnection();
      makeSelection();
      }

      public void actionPerformed(ActionEvent event)
      {
      if (event.getSource() == newProfessional)
      {
      dialog1.show();
      message = dialog1.GetSubString();
      System.out.println(message);
      }
      if (event.getSource() == newAccount)
      {
      dialog2.show();
      message = dialog1.GetSubString();
      System.out.println(message);
      }
      }


      public void windowClosing(WindowEvent event)
      {
      System.exit(0);
      }

      public void windowOpened(WindowEvent e) {}
      public void windowClosed(WindowEvent e) {}
      public void windowIconified(WindowEvent e) {}
      public void windowDeiconified(WindowEvent e) {}
      public void windowActivated(WindowEvent e) {}
      public void windowDeactivated(WindowEvent e) {}

      public void getConnection()
      {
      try//opens a connection to the database
      {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//loads the jdbc-odbc driver
      theConnection = DriverManager.getConnection(DB, USER, PASSWORD);
      //method to open a connection to the database & stores in field theConnection
      /**
      // public class DriverManager
      // extends Object
      // The DriverManager provides a basic service for managing a set of JDBC drivers.
      //
      // As part of its initialization, the DriverManager class will attempt to load the
      // driver classes referenced in the "jdbc.drivers" system property. This allows a user
      // to customize the JDBC Drivers used by their applications. For example in your
      // ~/.hotjava/properties file you might specify: jdbc.drivers=foo.bah.Driver:
      // wombat.sql.Driver:bad.taste.ourDriver A program can also explicitly load JDBC drivers at any time.
      // For example, the my.sql.Driver is loaded with the following statement: Class.forName("my.sql.Driver");
      //
      // When getConnection is called the DriverManager will attempt to locate a suitable
      // driver from amongst those loaded at initialization and those loaded explicitly using the same class
      // loader as the current applet or application.
      //
      // getConnection
      // public static synchronized Connection getConnection(String url,Properties info) throws SQLException
      //
      // Attempt to establish a connection to the given database URL.
      // The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers.
      //
      // Parameters:
      // url - a database url of the form jdbc:subprotocol:subname
      // info - a list of arbitrary string tag/value pairs as connection arguments;
      // normally at least a "user" and "password" property should be included
      // Returns:
      // a Connection to the URL
      // Throws: SQLException
      // if a database-access error occurs.
      //
      //
      **/
      }
      catch (ClassNotFoundException ex1) { fatalError(ex1); }//?
      catch (SQLException ex2) { fatalError(ex2); } //?
      }
      public void fatalError(Exception ex) //?
      {
      ex.printStackTrace(); //?
      }

      public void makeSelection()
      {
      try
      {
      String query = "select * from Professional";
      Statement stmt = theConnection.createStatement();
      /**
      // public interface Statement
      // A Statement object is used for executing a static SQL statement and obtaining
      // the results produced by it.
      **/

      ResultSet rs = stmt.executeQuery(query);
      displayResult(rs);
      stmt.close();
      }
      catch (SQLException ex) { fatalError(ex); }
      }

      public void displayResult(ResultSet rs)
      {
      try
      {

      // public interface ResultSetMetaData
      // A ResultSetMetaData object can be used to find out about the types
      // and properties of the columns in a ResultSet.

      ResultSetMetaData rsmd = rs.getMetaData();

      // The number, types and properties of a ResultSet's columns are provided
      // by the getMetaData method.
      // Returns: the description of a ResultSet's columns

      int cols = rsmd.getColumnCount();



      while (rs.next())
      {
      System.out.println();
      // next()
      // A ResultSet is initially positioned before its first row;
      // the first call to next makes the first row the current row;
      // the second call makes the second row the current row, etc.
      // Returns: true if the new current row is valid; false if there are no more rows





      for (int i = 1; i <= cols; i++)
      {
      // System.out.print(rs.getString(i) + " : ");
      String text = rs.getString(i);
      if (text == null) text = "";
      theResult.append(text);
      // Appends the given text to the text area's current text.
      }
      }
      rs.close();

      // closing the statement object rs releases the database and the JDBC resources

      }
      catch (SQLException ex) { fatalError(ex); }
      }

      public static void main(String[] args)
      {
      MainMenu myFrame = new MainMenu();
      myFrame.setSize(1024,755);
      myFrame.setVisible(true);
      }
      }