Question

Hey guys I am trying to take data from a jdbc table I have created and display them on a GUI I created. It's a program that displays movie names. My table consists of 7 columns which are: MovieID, title, genre, year, mpaaRating, directors, company. My job is only to display the movieID title year and directors. The first GUI interface I have created consists of three buttons that each take you to a different interface and they work correctly. my code for the main interface is:

public class MovieGUI extends JFrame implements ActionListener {
 private static final int FRAME_WIDTH = 600;
 private static final int FRAME_HEIGHT= 600;
 private static final int FRAME_X_ORIGIN = 200;
 private static final int FRAME_Y_ORIGIN = 50;
 private static final String dbPrefix = "xxxxx";
 private static final String netID = "xxxxx";
 private static final String hostName = "xxxxx";
 private static final String databaseURL = "jdbc:mysql://"+hostName+"/"+dbPrefix+netID;
 private static final String password = "xxxxx";
 private Connection connection = null;
 private Statement statement = null;
 private ResultSet resultSet = null;
 private JLabel movies[] = new JLabel[20];
 private JLabel promptName;
 private JTextField fieldName;
 private JButton browseMoviesButton, findNameButton, findRatingButton;

 public MovieGUI(){
     setTitle("Menu");
     setResizable(false);
     setSize(FRAME_WIDTH, FRAME_HEIGHT);
     setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);

     Container contentPane = getContentPane();
     contentPane.setLayout(null);
     contentPane.setBackground(Color.red);

     promptName = new JLabel();
     promptName.setText("Movie Rating Services");
     promptName.setBounds(250, 35, 150, 100);
     contentPane.add(promptName);

     browseMoviesButton = new JButton("Browse Movies");
     browseMoviesButton.setBounds(240,120,150,50);
     contentPane.add(browseMoviesButton);
     browseMoviesButton.addActionListener(this);

     findNameButton = new JButton("Find movies given name");
     findNameButton.setBounds(230,220,180, 80);
     contentPane.add(findNameButton);
     findNameButton.addActionListener(this);

     findRatingButton = new JButton("Find movie by rating");
     findRatingButton.setBounds(230,350,180, 80);
     contentPane.add(findRatingButton);
     findRatingButton.addActionListener(this);

     setDefaultCloseOperation(EXIT_ON_CLOSE);
 }

 public static void main(String args[]){
        MovieGUI obj = new MovieGUI();
        try{
            //connects to database
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("databaseURL =" + databaseURL);
            obj.connection = (Connection) DriverManager.getConnection(databaseURL, netID, password);
            System.out.println("Connection Established");
            obj.statement = (Statement) obj.connection.createStatement();
            String sql;
        }
        catch(SQLException e){
            System.out.println("command did not go through");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     MovieGUI frameObj = new MovieGUI();
     frameObj.setVisible(true);



     }

 public void actionPerformed (ActionEvent event){
     if (event.getSource() instanceof JButton){

            FindName nameObj = new FindName();
            BrowseMovies browseObj = new BrowseMovies();
            FindRating ratingObj = new FindRating();
            JButton clickedButton = (JButton) event.getSource();
            JRootPane rootPane = clickedButton.getRootPane();
            JFrame frame = (JFrame) rootPane.getParent();
            String buttonText = clickedButton.getText();
            if (buttonText.equals("Browse Movies")){
                 browseObj.setVisible(true);
                 frame.setVisible(false);
                 try {
                    statement = (Statement) connection.createStatement();
                    String sql;
                    sql = "SELECT movieID, title, year, directors FROM MOVIE";
                    resultSet = statement.executeQuery(sql);
                    int columns = resultSet.getMetaData().getColumnCount();
                    StringBuilder message = new StringBuilder();
                    while (resultSet.next()) {                          
                        for (int i = 0; i < movies.length; i++){
                            for (int j = 1; j <= columns; j++) {                        
                                message.append(resultSet.getString(j) + "\t\t");    
                        }//end nested for loop
                        message.append("\n");
                        movies[i].setText(message.toString());
                    }//end for loop

                    }//end while loop
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }//end catch

                     for (int i = 0; i < movies.length; i++){
                         movies[i].setBounds(150,60,300,50);
                         browseObj.add(movies[i]);

                     }

             }
            else if(buttonText.equals("Find movies given name")){
                nameObj.setVisible(true);
                frame.setVisible(false);

            }
            else if (buttonText.equals("Find movie by rating")){
                ratingObj.setVisible(true);
                frame.setVisible(false);
            }
     }

 }




}

When I click the browse movies button, it's supposed to go to the next interface and display the above attributes of the movies in my table, there is 20 of them.

my BrowseMovies interface code is:

public class BrowseMovies extends JFrame {
 private static final int FRAME_WIDTH = 600;
 private static final int FRAME_HEIGHT= 600;
 private static final int FRAME_X_ORIGIN = 200;
 private static final int FRAME_Y_ORIGIN = 50;

 private JLabel promptName;
 public BrowseMovies(){
     setTitle("Movie List");
     setResizable(false);
     setSize(FRAME_WIDTH, FRAME_HEIGHT);
     setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);

     Container contentPane = getContentPane();
     contentPane.setLayout(null);
     contentPane.setBackground(Color.blue);

     promptName = new JLabel();
     promptName.setText("Movie Rating Services -- Browse Movies");
     promptName.setBounds(200, 35, 250, 100);
     contentPane.add(promptName);




 }//GUI constructor
}

The problem I'm having is that the browseMovies interface is only displaying promptName and nothing else and I am getting a very long error in the console. The error is:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at project5.MovieGUI.actionPerformed(MovieGUI.java:111)
    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.plaf.basic.BasicButtonListener.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.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)

I have no idea what's wrong. I am guessing the problem is somewhere in the try block of the actionPerformed method but I can't seem to find it. Any help will be appreciated.

Was it helpful?

Solution

You have to initialize your array with an actual JLabel object. The following gives you a null pointer in the first iteration

public class TestSwing {
    public static void main(String[] args) {
        JLabel movies[] = new JLabel[20];
        for (int j = 0; j < movies.length; j++) {
            System.out.println("Loop Counter : " + j);
            movies[j].setText("Counter : " + j);
        }
    }
}

You have to do something similar to this

public class TestSwing {
        public static void main(String[] args) {
            JLabel movies[] = new JLabel[20];
            for (int j = 0; j < movies.length; j++) {
                System.out.println("Loop Counter : " + j);
                movies[j] = new JLabel(); // initialize your JLable
                movies[j].setText("Counter : " + j);
            }
        }
    }

NOTE: this is just an extract from your code, i have removed some of your code just to clarify it better

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top