Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Getting Selected Text From Combo Box

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Getting Selected Text From Combo Box

    I cant see why i am getting errors, I believe i did everything correct. I only seem to to get errors when i try to get the text out the combobx. Can someone please hep me do this well. I know how to insert data from a text field to a data with microsoft access . But not when it comes to combo boxes.

    Theses errors

    java.lang.NullPointerException at delete_me1.connection(delete_me1.java:66) at delete_me1$2.actionPerformed(delete_me1.java:133) at java.awt.EventDispatchThread.run(Unknown Source) java.lang.NullPointerException at delete_me1.connection(delete_me1.java:90) at delete_me1$2.actionPerformed(delete_me1.java:133)

    Thanks


    Main Code

    import java.awt.EventQueue;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
     
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JComboBox;
    import java.awt.BorderLayout;
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JTextField;
    import javax.swing.JList;
    import javax.swing.AbstractListModel;
     
    public class delete_me1 {
     
    	private JFrame frame;
    	public JComboBox<?> comboBox;
     
    	String MSAccessDriver = "sun.jdbc.odbc.JdbcOdbc ".trim();
    	String MyDatabase = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\db\\Database.mdb";
     
    	Connection con ;
    	Statement st = null;
    	ResultSet rs;
    	private JTextField textField;
     
    	/**
    	 * Launch the application.
    	 */
     
     
     
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					delete_me1 window = new delete_me1();
    					window.frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
     
     
    	public void connection() throws Exception {
     
     
    		/*	String sql = "INSERT INTO animal (first_name, last_name) VALUES(?,?)";
    		PreparedStatement preparedStatement = connection.prepareStatement(sql);
    		preparedStatement.setString(1,animal);
    		preparedStatemnt.setString(2,animalType);
    		preparedStatemnt.executeUpdate();*/
     
    		try {
     
    			String com2 = comboBox.getSelectedItem().toString();
    			String com = textField.getText();
    			String sql = "INSERT INTO client (first_name, last_name) VALUES (?,?)";
    			PreparedStatement preparedStatement = con.prepareStatement(sql);
    			preparedStatement.setString(1,com);
    			preparedStatement.setString(2,com2);
    			preparedStatement.executeUpdate();
     
     
    			Class.forName(MSAccessDriver);
    			String db = "jdbc:odbc:Food"; // db = database string stored
    											// in the database
    			con = DriverManager.getConnection(db);
    			st = con.createStatement();
    			st.execute(sql);
     
    		}
     
    		catch (Exception ex) {
    			ex.printStackTrace();
    		}
     
    		finally {
    			try {
    				st.close();
    			} catch (SQLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			try {
    				con.close();
    			} catch (SQLException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
     
    			JOptionPane.showMessageDialog(null, "Save Complete Successfully");
    		}
     
    	}
     
    	/**
    	 * Create the application.
    	 */
    	public delete_me1() {
    		initialize();
    	}
     
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {
    		frame = new JFrame();
    		frame.setBounds(100, 100, 450, 300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().setLayout(null);
     
    		final JComboBox comboBox = new JComboBox();
    		comboBox.setModel(new DefaultComboBoxModel(new String[] { "Dog", "Cat",
    				"Cow", "Sheep" }));
    		comboBox.setBounds(57, 54, 245, 46);
    		frame.getContentPane().add(comboBox);
     
    		JButton btnNewButton = new JButton("New button");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				try {
    					connection();
     
    				} catch (Exception e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		});
    		btnNewButton.setBounds(57, 143, 89, 23);
    		frame.getContentPane().add(btnNewButton);
     
    		textField = new JTextField();
    		textField.setBounds(172, 144, 118, 20);
    		frame.getContentPane().add(textField);
    		textField.setColumns(10);
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,534
    My Mood
    Hungover
    Thanks
    145
    Thanked 647 Times in 552 Posts

    Default Re: Getting Selected Text From Combo Box

    You're going to have to step through this with a debugger, or at least add some print statements, to figure out what's going on. What is selected in the JComboBox when you're trying to access it?

    If you can't figure it out, I suggest providing an SSCCE- we can't run this because of all the extra stuff you have here, so it's a little hard to help you.

    Hint: When do you initialize the JComboBox you're trying to access? Are you sure? There's a difference between variables declared at the class level and variables declared inside a method.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Getting Selected Text From Combo Box

    Use code tags.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,534
    My Mood
    Hungover
    Thanks
    145
    Thanked 647 Times in 552 Posts

    Default Re: Getting Selected Text From Combo Box

    Quote Originally Posted by llowe29 View Post
    Use code tags.
    He did.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Getting Selected Text From Combo Box

    That's weird because usually when you use code tags Java specific vocab is highlited

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,097
    My Mood
    Mellow
    Thanks
    244
    Thanked 754 Times in 740 Posts

    Default Re: Getting Selected Text From Combo Box

    There are two variables named comboBox. The first (in order of appearance) is declared at line # 24, but is never initialized before its contents are fetched at line 66. That's the source of the error. The second comboBox is declared and initialized at line 124. I think you're confusing the two comboBoxes, thinking they're the same, but I could be wrong.

  7. The Following User Says Thank You to GregBrannon For This Useful Post:

    tommyacton (August 6th, 2013)

  8. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,534
    My Mood
    Hungover
    Thanks
    145
    Thanked 647 Times in 552 Posts

    Default Re: Getting Selected Text From Combo Box

    Quote Originally Posted by llowe29 View Post
    That's weird because usually when you use code tags Java specific vocab is highlited
    Code tags simply preserve formatting, which is what the OP did. Using highlight=java tags also adds syntax highlighting, but either one is fine.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 2
    Last Post: March 12th, 2013, 07:16 AM
  2. Making an input dialog box appear after closing a combo box
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 14
    Last Post: November 11th, 2012, 06:22 PM
  3. ClassCastException with Enum Combo Box
    By Diplo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 26th, 2011, 02:22 PM
  4. Adding another related combo box ?
    By shad3d in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 19th, 2011, 02:53 PM
  5. Loading contents of a Database into a Combo box
    By tyolu in forum JDBC & Databases
    Replies: 3
    Last Post: June 22nd, 2009, 09:14 AM