Place labels

I am attaching some code I am trying to make work.  The goal is to be able to read a set of labels from a mysql database along with a key for each label.  The key is the position I would like the label and a text field positioned.  I posted a question about this problem earlier which has been closed and got some of the code from and expert (gordon).  I have inserted the code but because I closed the question I am opening a new question.  The first problem is an error in the line:

private List<PropertuyDisplay> ordered PropertyDisplays;

The error states:

;expected
cannot find symbol
  symbol:  class PropertyDisplays
  location: class jclerk.MainScreen

<identifierr> expected

A couple of hints were provided to correct the problem but I'm not sure where it would go or what would go in it.

Create Class "PropertyDisplays" in package jclerk
Create Class "PropertyDisplays" in jclerk.MainScreen


Need a had for this beginner.
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainScreen.java
 *
 * Created on Mar 27, 2011, 8:57:25 PM
 */

package jclerk;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author al
 */

public class MainScreen extends javax.swing.JFrame {
    
    ConnectDatabase connectDatabase = new ConnectDatabase();
    int multidraftFlag = 0;
    int weightDisplayFlag = 0;
    int zeroBalanceFlag = 0;
    Color defaultButtonColor = new Color(238, 238, 238);
    String [][] columnData;

//    public Connection con;
    String[] order;
    
    private List<PropertyDisplay> ordered PropertyDisplays;
    private Map<String, PropertyDisplay> propertyDisplayMap;


private JPanel populateClerkScreen() {
    Connection con = connectDatabase.getConnection();
    
    // PropertyDisplay objects will be stored in both a
    // List, for access to the correct ordering, and a
    // Map, so you can get the PropertyDisplay for a
    // particular property key
    orderedPropertyDisplays = new ArrayList<PropertyDisplay>();
    propertyDisplayMap = new HashMap<String, PropertyDisplay>();
    try {
        Statement stmt = (Statement) con.createStatement();
        // let database sort the values for you
        ResultSet rec = stmt.executeQuery("SELECT skey, svalue FROM setup WHERE skey LIKE 'cOrder%' ORDER BY svalue");
        String key;
        String value;
        PropertyDisplay prop;
        while (rec.next()) {
            key = rec.getString("skey");
            value = rec.getString("svalue");
            prop = new PropertyDisplay(key, value);
            orderedPropertyDisplays.add(prop);
            propertyDisplayMap.put(key, prop);
        }
        rec.close();
        stmt.close();
    } catch (SQLException sqe) {
        JOptionPane.showMessageDialog(null,
            "populateClerkScreen: " + sqe.getMessage());
    } finally {
        // attempt to close the connection since you are done with it
        try {
            con.close();
        } catch (SQLException e) {
            // can ignore, but better to log
            e.printStackTrace(System.err);
        }
    }

    // now create property panel GUI
    JPanel propertyPanel = new JPanel();
    propertyPanel.setLayout(new GridLayout(orderedPropertyDisplays.size(), 2));
    for (PropertyDisplay prop : orderedPropertyDisplays) {
        propertyPanel.add(prop.getLabel());
        propertyPanel.add(prop.getTextField());
    }
    return propertyPanel;
}

// slight change to PropertyDisplay, adding 'value' to constructor
// and set/getValue() methods
    private static class PropertyDisplay {
        private final String property;
        private final JLabel label;
        private final JTextField textField;

        public PropertyDisplay(String property, String value) {
            this.property = property;
            this.label = new JLabel(property);
            this.textField = new JTextField(value);
        }

        public String getProperty() {
            return property;
        }

        public JLabel getLabel() {
            return label;
        }

        public JTextField getTextField() {
            return textField;
        }

        public void setValue(String value) {
            textField.setText(value);
        }

        public String getValue() {
            return textField.getText();
        }
    }

  
     
    
    /** Creates new form MainScreen */
    public MainScreen() {
        initComponents();
        

        populateClerkScreen();
    }

Open in new window

clerkCode
sargent240Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sargent240Author Commented:
Well, there does not seem to be many takers on this post.  I'll add another post with some results to see if anyone can lend a hand.  I am embedding the latest code where some errors have cleaned up and I can compile the code now.  When I execute the code I get a NullPointerException in line 72.  As you can see I have added a couple of debug print statements to try and see what the problem is but I don't understand what I am seeing.  The print statements produced the following.

key = cOrderWeight  value = 1
prop = jclerk.MainScreen$PropertyDisplay@1911540

any help will be appreciated

Cheers - Al
 
/*
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainScreen.java
 *
 * Created on Mar 27, 2011, 8:57:25 PM
 */

package jclerk;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.awt.Color;
import java.awt.GridLayout;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author al
 */

public class MainScreen extends javax.swing.JFrame {
    
    ConnectDatabase connectDatabase = new ConnectDatabase();
    int multidraftFlag = 0;
    int weightDisplayFlag = 0;
    int zeroBalanceFlag = 0;
    Color defaultButtonColor = new Color(238, 238, 238);
    String [][] columnData;

//    public Connection con;
    String[] order;

	

private List<PropertyDisplay> orderedPropertyDisplays;
private Map<String, PropertyDisplay> propertyDisplayMap;

private JPanel populateClerkScreen() {
    Connection con = connectDatabase.getConnection();

    
    
    
    // PropertyDisplay objects will be stored in both a
    // List, for access to the correct ordering, and a
    // Map, so you can get the PropertyDisplay for a
    // particular property key
    try {
        Statement stmt = (Statement) con.createStatement();
        // let database sort the values for you
        ResultSet rec = stmt.executeQuery("SELECT skey, svalue FROM setup WHERE skey LIKE 'cOrder%' ORDER BY svalue");
        String key;
        String value;
        PropertyDisplay prop;
        while (rec.next()) {
            key = rec.getString("skey");
            value = rec.getString("svalue");
System.out.println("key = " + key + "  value = " + value);            
            prop = new PropertyDisplay(key, value);
System.out.println("prop = " + prop);            
            orderedPropertyDisplays.add(prop);
            propertyDisplayMap.put(key, prop);
        }
        rec.close();
        stmt.close();
    } catch (SQLException sqe) {
        JOptionPane.showMessageDialog(null,
            "populateClerkScreen: " + sqe.getMessage());
    } finally {
        // attempt to close the connection since you are done with it
        try {
            con.close();
        } catch (SQLException e) {
            // can ignore, but better to log
            e.printStackTrace(System.err);
        }
    }

    // now create property panel GUI
    JPanel propertyPanel = new JPanel();
    propertyPanel.setLayout(new GridLayout(orderedPropertyDisplays.size(), 2));
    for (PropertyDisplay prop : orderedPropertyDisplays) {
        propertyPanel.add(prop.getLabel());
        propertyPanel.add(prop.getTextField());
    }
    return propertyPanel;
}

// slight change to PropertyDisplay, adding 'value' to constructor
// and set/getValue() methods
    private static class PropertyDisplay {
        private final String property;
        private final JLabel label;
        private final JTextField textField;

        public PropertyDisplay(String property, String value) {
            this.property = property;
            this.label = new JLabel(property);
            this.textField = new JTextField(value);
        }

        public String getProperty() {
            return property;
        }

        public JLabel getLabel() {
            return label;
        }

        public JTextField getTextField() {
            return textField;
        }

        public void setValue(String value) {
            textField.setText(value);
        }

        public String getValue() {
            return textField.getText();
        }
    }

  
     
    
    /** Creates new form MainScreen */
    public MainScreen() {
        initComponents();
        

        populateClerkScreen();
    }

Open in new window

0
sargent240Author Commented:
I stated the error appeared at line 72 and actually it is 73.  Sorry 'bout that.
0
RobOwner (Aidellio)Commented:
Java is not javascript.  It should not be in the javascript zone
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

zzynxSoftware engineerCommented:
Well, you have this:

private List<PropertyDisplay> orderedPropertyDisplays;

That's saying: I'm gonna use a variable with the name "orderedPropertyDisplays" and it represents a List of objects of the type PropertyDisplay.

Then in line 73 you have:

orderedPropertyDisplays.add(prop);

So, here you try to add a PropertyDisplay object to that list.
But...

that variable orderedPropertyDisplays is still null. You haven't initialized it.
So to solve your problem that line 47 should read:

private List<PropertyDisplay> orderedPropertyDisplays = new ArrayList<PropertyDisplay>();

This way you initialize your variable as an empty list. (which you fill up afterwards at line 73)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sargent240Author Commented:
That cured the error in the line:

orderedPropertyDisplays.add(prop);

and now there is a null pointer exception in the line that follows:

propertyDisplayMap.put(key, prop);


Thank you for the explanation prior to the solution.  Very helpful.



0
zzynxSoftware engineerCommented:
Try replacing

key = rec.getString("skey");
value = rec.getString("svalue");

Open in new window

by

key = rec.getString(1);
value = rec.getString(2);

Open in new window

0
zzynxSoftware engineerCommented:
maybe

key = rec.getString(1);
if (key==null) continue; // You don't want your key to be null, do you?
value = rec.getString(2);

Open in new window


is even better
0
sargent240Author Commented:
It does not make any difference in the debug output.  My debug print statements return the following result whether I use key = rec.getString("skey") and value = rec.getString("svalue") or key = rec.getString(1) and value = rec.getString(2).  The debut print statement prints the following either way.

key = cOrderWeight   value = 1
prop = jclerk.MainScreen$PropertyDisplay@4488
0
zzynxSoftware engineerCommented:
You're right.
You can user getString(int columnIndex) as well as getString(String columnName)
Sorry for the confusion.

Well, that null pointer has the same reason:

propertyDisplayMap isn't initialized yet.

Why did you remove

propertyDisplayMap = new HashMap<String, PropertyDisplay>();

Open in new window


between your first and your second code snippet?

Before calling
propertyDisplayMap.put(key, prop);

Open in new window


you should have called

propertyDisplayMap = new HashMap<String, PropertyDisplay>();

Open in new window


or you should do the initialization right away in the definition:

propertyDisplayMap = new HashMap<String, PropertyDisplay>();

Open in new window

0
zzynxSoftware engineerCommented:
There's a typo in that last line of code.
I meant:

or you should do the initialization right away in the definition:

private Map<String, PropertyDisplay> propertyDisplayMap =  new HashMap<String, PropertyDisplay>();

Open in new window

0
sargent240Author Commented:
That's a keeper my friend.  It compiles and the main frame comes up.  This solves the problem posed in my question.  I do have one more problem involving this code and I don't know whether I should open another question or not.  I think one of my problems posing questions is I don't get to the right area to find the right experts.

I have a frame  (JFrame) that I created with NetBeans and it has some buttons on it.  The original reason for the code you just helped me get going was to populate a panel on my frame with the labels and text boxes from the database.  The code compiles and runs bringing up the frame and all the buttons created with Netbeans but does not display the labels and text boxes in a panel on the JFrame.

If you think I need to open another question I will, however if you can see something I need to do I would appreciate a suggestion.

Thank you!
0
zzynxSoftware engineerCommented:
Hi sargent240,

>> If you think I need to open another question
Yes, I do. The original question is answered. And this is a totally new one.

If you want to draw my attention to your new question, just post a link to that question in a next comment here (then I'm automatically mailed)
0
sargent240Author Commented:
Thank you very much.
0
sargent240Author Commented:
Problem Solved!
0
sargent240Author Commented:
I assume the address below is what you were suggesting I send to you.  Thanks

http://www.experts-exchange.com/Programming/Languages/Java/Q_27499162.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.