Results 1 to 2 of 2
  1. #1
    mathidioticz is offline Member
    Join Date
    Jan 2012
    Posts
    48
    Rep Power
    0

    Default Retrieving data from database using arrayList and display on JList

    Hi everyone, I am a java beginner.

    I wants the user to input data into a textfield and click the JButtonGo. It will be using arrayList to searchPhrase the keyword that user input and retrieve relevant data from database and later display the result on the JList.
    But when i run the program, it always come out an error.

    Please help me to solve my problems..

    Java Code:
     
    package OOPJ.ui;
    
    
    import javax.swing.JPanel;
    import java.awt.Color;
    import javax.swing.JLabel;
    
    import java.awt.Container;
    import java.awt.Rectangle;
    import java.awt.Font;
    import java.sql.ResultSet;
    import java.util.ArrayList;
    
    import javax.swing.DefaultListModel;
    import javax.swing.JScrollPane;
    import javax.swing.ListSelectionModel;
    import javax.swing.SwingConstants;
    import javax.swing.ImageIcon;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    
    import OOPJ.database.DBController;
    import Signup.entity.SignUp;
    import cancer.entity.CancerDetails;
    import javax.swing.JList;
    
    
    
    
    public class CancerPanel extends JPanel {
    
    	private static final long serialVersionUID = 1L;
    	private JLabel jLabelLogo3 = null;
    	private JLabel jLabelSearch = null;
    	private JTextField jTextField1 = null;
    	private JLabel jLabelCancer = null;
    	private JButton jButtonGo = null;
    	private JLabel jLabelResults = null;
    	private JButton jButtonMenu = null;
    	private JLabel jLabelCancerImage1 = null;
    	private MainFrame myFrame = null;
    	private JButton jButtonEdit = null;
    	private DefaultListModel model;
    	private JList dbList = null;
    	private JScrollPane jScrollPane = null;
    	private JScrollPane pane = null;
    	private JScrollPane jScrollPane1 = null;
    	private JList jList = null;
    	
    	
    	/**
    	 * This is the default constructor
    	 */
    	public CancerPanel() {
    		super();
    		initialize();
    
    		//DefaultListModel listModel = new DefaultListModel();  
    		//JList jList = new JList(listModel);
    		//JScrollPane pane = new JScrollPane();
    	    // pane.getViewport().add(jList);
    	     
    	}
    	     
    	 	public boolean retrieveCancer() { 
    	 		
    	 		  String c_articleTitle = null;
    			//declare local variables
    			boolean success = false; 
    			ResultSet rs = null; 
    			DBController db =  new DBController();
    			
    			db.setUp("CFDatabase");
    			
    			String dbQuery = "SELECT * FROM CANCER WHERE C_ARTICLETITLE ='" + c_articleTitle + "' and C_ARTICLEDETAILS'";
    			
    			rs = db.readRequest(dbQuery); 
    			
    			try { 
    				if(rs.next()) { 
    					c_articleTitle = rs.getString("c_articleTitle");
    			
    					success = true; 
    				} 
    			}
    				catch(Exception e) { 
    					e.printStackTrace();
    				}
    				db.terminate(); 
    				return success;
    			}
    
    	public CancerPanel(MainFrame f) { 
    		this(); 
    		myFrame = f; 
    		
    		// find out  the current login user
    		SignUp curUser = myFrame.getApp().getCurLoginUser();
    		
    		// hide or display button depend on whether user is admin
    		if(curUser.isAdmin())
    			getJButtonEdit().setVisible(true);
    		else
    			getJButtonEdit().setVisible(false);
    		
    		
    	}
    
    
    	/**
    	 * This method initializes this
    	 * 
    	 * @return void
    	 */
    	private void initialize() {
    		jLabelCancerImage1 = new JLabel();
    		jLabelCancerImage1.setBounds(new Rectangle(364, 134, 326, 170));
    		jLabelCancerImage1.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/cancer.jpg")));
    		jLabelCancerImage1.setText("JLabel");
    		jLabelResults = new JLabel();
    		jLabelResults.setBounds(new Rectangle(42, 149, 229, 30));
    		jLabelResults.setFont(new Font("Dialog", Font.BOLD, 18));
    		jLabelResults.setHorizontalAlignment(SwingConstants.CENTER);
    		jLabelResults.setHorizontalTextPosition(SwingConstants.CENTER);
    		jLabelResults.setText("Recommended Articles:");
    		jLabelCancer = new JLabel();
    		jLabelCancer.setBounds(new Rectangle(176, 8, 431, 70));
    		jLabelCancer.setFont(new Font("Dialog", Font.BOLD, 24));
    		jLabelCancer.setHorizontalAlignment(SwingConstants.CENTER);
    		jLabelCancer.setHorizontalTextPosition(SwingConstants.CENTER);
    		jLabelCancer.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/Cancer Title.jpg")));
    		jLabelCancer.setText("");
    		jLabelSearch = new JLabel();
    		jLabelSearch.setBounds(new Rectangle(142, 88, 68, 26));
    		jLabelSearch.setHorizontalAlignment(SwingConstants.CENTER);
    		jLabelSearch.setHorizontalTextPosition(SwingConstants.CENTER);
    		jLabelSearch.setText("Search:");
    		jLabelLogo3 = new JLabel();
    		jLabelLogo3.setBounds(new Rectangle(14, 8, 137, 66));
    		jLabelLogo3.setFont(new Font("Dialog", Font.BOLD, 24));
    		jLabelLogo3.setHorizontalAlignment(SwingConstants.LEFT);
    		jLabelLogo3.setHorizontalTextPosition(SwingConstants.RIGHT);
    		jLabelLogo3.setIcon(new ImageIcon(getClass().getResource("/images/ui/images/logo.jpg")));
    		jLabelLogo3.setText("");
    		this.setLayout(null);
    		this.setSize(700, 500);
    		this.setBackground(Color.white);
    		this.add(jLabelLogo3, null);
    		this.add(jLabelSearch, null);
    		this.add(getJTextField1(), null);
    		this.add(jLabelCancer, null);
    		this.add(getJButtonGo(), null);
    		this.add(jLabelResults, null);
    		this.add(getJButtonMenu(), null);
    		this.add(jLabelCancerImage1, null);
    		this.add(getJButtonEdit(), null);
    		//this.add(getDbList(), null);
    		this.add(getDbList(), null);
    		this.add(getPane(), null);
    		//this.add(getPane(), null);
    	}
    
    	
    	/**
    	 * This method initializes jTextField1	
    	 * 	
    	 * @return javax.swing.JTextField	
    	 */
    	private JTextField getJTextField1() {
    		if (jTextField1 == null) {
    			jTextField1 = new JTextField();
    			jTextField1.setBounds(new Rectangle(235, 85, 326, 28));
    		}
    		return jTextField1;
    	}
    
    	/**
    	 * This method initializes jButtonGo	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getJButtonGo() {
    		if (jButtonGo == null) {
    			jButtonGo = new JButton();
    			jButtonGo.setBounds(new Rectangle(576, 85, 53, 26));
    			jButtonGo.setText("GO");
    			jButtonGo.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					String searchPhrase = jTextField1.getText();
    					ArrayList<CancerDetails> result = CancerDetails.searchCancer(searchPhrase);
    					DefaultListModel model = new DefaultListModel();
    					dbList.setModel(model);
    				}
    			});
    		}
    		return jButtonGo;
    	}
    	/**
    	 * This method initializes jButtonMenu	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getJButtonMenu() {
    		if (jButtonMenu == null) {
    			jButtonMenu = new JButton();
    			jButtonMenu.setBounds(new Rectangle(562, 447, 85, 27));
    			jButtonMenu.setText("Menu");
    			jButtonMenu.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					JPanel panel = new MenuPanel(myFrame);
    					myFrame.getContentPane().removeAll();
    					myFrame.getContentPane().add(panel);
    					myFrame.getContentPane().validate();
    					myFrame.getContentPane().repaint();
    				}
    			});
    		}
    		return jButtonMenu;
    	}
    
    	/**
    	 * This method initializes jButtonEdit	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getJButtonEdit() {
    		if (jButtonEdit == null) {
    			jButtonEdit = new JButton();
    			jButtonEdit.setBounds(new Rectangle(601, 15, 94, 32));
    			jButtonEdit.setText("Edit");
    			jButtonEdit.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					JPanel panel = new CancerBackPanel(myFrame); 
    					myFrame.getContentPane().removeAll(); 
    					myFrame.getContentPane().add(panel);
    					myFrame.getContentPane().validate(); 
    					myFrame.getContentPane().repaint(); 
    				
    				}
    			});
    		}
    		return jButtonEdit;
    	}
    
    
    	
    	private JList getDbList() {
    		if (dbList == null) {
    			dbList = new JList(model);
    			 model = new DefaultListModel(); 			 
    			dbList.setVisibleRowCount(5); 
    			 dbList.setBounds(new Rectangle(39, 199, 201, 106));
    			 dbList.setFixedCellHeight(27); 
    			 dbList.setFixedCellWidth(130); 
    			 dbList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		}
    		return dbList;
    	}
    
    	private JScrollPane getPane() {
    		if (pane == null) {
    			pane = new JScrollPane();
    			pane.setBounds(new Rectangle(49, 211, 180, 88));
    			pane.getViewport().add(dbList);
    		}
    		return pane;
    	} 
    	
    	    
    
    
    	
     
    }
    the error message:
    Java Code:
     
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: dataModel must be non null
    	at javax.swing.JList.<init>(Unknown Source)
    	at OOPJ.ui.CancerPanel.getDbList(CancerPanel.java:249)
    	at OOPJ.ui.CancerPanel.initialize(CancerPanel.java:158)
    	at OOPJ.ui.CancerPanel.<init>(CancerPanel.java:56)
    	at OOPJ.ui.CancerPanel.<init>(CancerPanel.java:94)
    	at OOPJ.ui.MenuPanel$3.actionPerformed(MenuPanel.java:154)
    	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$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$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)
    Last edited by mathidioticz; 01-25-2012 at 05:09 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Retrieving data from database using arrayList and display on JList

    'model' is null at that point.
    Ensure it isn't null at that point.

    It's not as if the exception isn't saying exactly what the problem is...

    Java Code:
    String dbQuery = "SELECT * FROM CANCER WHERE C_ARTICLETITLE ='" + c_articleTitle + "' and C_ARTICLEDETAILS'";
    rs = db.readRequest(dbQuery);
    This is an aside, and you may have no control over it, but I see this quite a lot. It does encapsulate the db interaction to some extent, but it means you can't use PreparedStatements leaving you open to SQL injection and other potential errors (what if the article title had a single quote in it?). Your database layer should have accessors for the specific data you need, returning objects (ie a model) based on that data.
    In this case you'd have something like a method:
    Java Code:
    public Article getArticle(String articleName)
    which would return an Article object containing what you need. Of course your requirements will affect exactly how this works, but that's the general structure.

Similar Threads

  1. problem in display the data in database using j2me
    By jamil in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-20-2011, 08:16 PM
  2. Replies: 1
    Last Post: 04-05-2011, 08:28 AM
  3. Retrieving data from database every 10 secs.
    By vnsuresh.abi in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 03-31-2011, 11:14 AM
  4. how to display data from oracle database?
    By deru4jesus in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 04-16-2009, 08:46 AM
  5. Replies: 3
    Last Post: 01-29-2009, 10:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •