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 3 of 3

Thread: applet + swing + arraylist = exception??

  1. #1
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default applet + swing + arraylist = exception??

    ok , i now that i finaly fixed the applet not loading problem i was able to move
    on to the actual making of it . but now i've hit a brick wall with trying to add
    stuff into my array list

    the 1st comment is where the problem is at

    package ernya;
     
    import java.awt.event.InputMethodEvent;
    import java.awt.event.InputMethodListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.io.DataInputStream;
    import java.net.URL;
    import java.util.ArrayList;
     
    import javax.swing.BorderFactory;
    import javax.swing.DefaultListModel;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.JSeparator;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.border.EtchedBorder;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
     
    import org.dyno.visual.swing.layouts.Bilateral;
    import org.dyno.visual.swing.layouts.Constraints;
    import org.dyno.visual.swing.layouts.GroupLayout;
    import org.dyno.visual.swing.layouts.Leading;
     
    public class Applet extends JApplet {
     
    	private static final long serialVersionUID = 1L;
    	private JTextField jTextField0;
    	private JList jList0;
    	private JScrollPane jScrollPane0;
    	private JSeparator jSeparator0;
    	private JList jList1;
    	private JScrollPane jScrollPane1;
    	private JLabel jLabel0;
    	private JLabel jLabel1;
    	private JLabel jLabel2;
    	private JLabel jLabel3;
    	private JLabel jLabel4;
    	private JCheckBox jCheckBox0;
    	private JList jList2;
    	private JScrollPane jScrollPane2;
    	private JButton jButton0;
    	private JButton jButton1;
    	private JPicturePanel jPicturePanel0;
    	private JPicturePanel jPicturePanel1;
    	private JTextArea jTextArea0;
    	private JScrollPane jScrollPane3;
     
    	DefaultListModel cartList = new DefaultListModel();
    	DefaultListModel colorList = new DefaultListModel();
    	DefaultListModel itemList = new DefaultListModel();
     
    	final String store = "Store Price: ";
    	final String myBuy = "My Buy Price: ";
    	final String mySale = "My Sale Price: ";
    	final String stock = "Current Stock: ";
    	final String total = "Total: ";
     
    	public ArrayList<String> items;
    	public ArrayList<Integer> price;
    	public ArrayList<String[]> color;
     
    	@SuppressWarnings("deprecation")
    	public void init() {
    		try {
    			URL itemUrl = new URL(
    					"http://etopsirhc.110mb.com/Java/Applets/Resources/TCC/items.txt");
    			DataInputStream itemIn = new DataInputStream(itemUrl.openStream());
    			try {
    				String in = itemIn.readLine();
    				while (in != null) {
    					String[] content = in.split("~");
    					items.add(content[0]);                  // this is where i get a null pointer exception as
    					price.add(Integer.parseInt(content[1]));    // well as the the 2 ".add" things below
    					String[] colors = content[2].split(":");    // it . i also tried having this in a separate 
    					color.add(colors);                          // class , but i still got it , any idea whats wrong ?
    					System.out.println(in);
    				}
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		initComponents();
     
    	}
     
    	private void initComponents() {
    		setLayout(new GroupLayout());
    		add(getJTextField0(), new Constraints(new Leading(12, 192, 10, 10),
    				new Leading(12, 12, 12)));
    		add(getJSeparator0(), new Constraints(new Bilateral(12, 12, 376),
    				new Leading(265, 10, 12, 12)));
    		add(getJScrollPane1(), new Constraints(new Bilateral(209, 12, 22),
    				new Leading(11, 120, 10, 10)));
    		add(getJLabel0(), new Constraints(new Bilateral(209, 13, 41),
    				new Leading(143, 12, 12)));
    		add(getJLabel1(), new Constraints(new Bilateral(209, 13, 41),
    				new Leading(165, 12, 12)));
    		add(getJLabel2(), new Constraints(new Bilateral(209, 13, 41),
    				new Leading(187, 12, 12)));
    		add(getJLabel3(), new Constraints(new Bilateral(209, 13, 41),
    				new Leading(209, 12, 12)));
    		add(getJLabel4(), new Constraints(new Bilateral(209, 13, 41),
    				new Leading(231, 12, 12)));
    		add(getJScrollPane0(), new Constraints(new Leading(13, 190, 12, 12),
    				new Leading(39, 220, 12, 12)));
    		add(getJScrollPane2(), new Constraints(new Leading(12, 192, 108, 108),
    				new Bilateral(278, 12, 22)));
    		add(getJButton0(), new Constraints(new Leading(216, 170, 12, 12),
    				new Leading(277, 12, 12)));
    		add(getJButton1(), new Constraints(new Leading(216, 170, 12, 12),
    				new Leading(309, 12, 12)));
    		add(getJCheckBox0(), new Constraints(new Leading(216, 170, 8, 8),
    				new Leading(343, 8, 8)));
    		//add(getJPicturePanel0(), new Constraints(new Leading(228, 55, 12, 12),
    		//		new Leading(369, 55, 12, 12)));
    		//add(getJPicturePanel1(), new Constraints(new Leading(317, 55, 10, 10),
    		//		new Leading(369, 55, 12, 12)));
    		add(getJScrollPane3(), new Constraints(new Leading(215, 172, 12, 12),
    				new Bilateral(431, 12, 22)));
    		setSize(400, 541);
    	}
     
    	private JScrollPane getJScrollPane3() {
    		if (jScrollPane3 == null) {
    			jScrollPane3 = new JScrollPane();
    			jScrollPane3.setViewportView(getJTextArea0());
    		}
    		return jScrollPane3;
    	}
     
    	private JTextArea getJTextArea0() {
    		if (jTextArea0 == null) {
    			jTextArea0 = new JTextArea();
    			jTextArea0.setText("jTextArea0");
    			jTextArea0.addMouseListener(new MouseAdapter() {
     
    				public void mouseReleased(MouseEvent event) {
    					jTextArea0MouseMouseReleased(event);
    				}
    			});
    		}
    		return jTextArea0;
    	}
     
    //	private JPicturePanel getJPicturePanel1() {
    //		if (jPicturePanel1 == null) {
    //			jPicturePanel1 = new JPicturePanel();
    //			jPicturePanel1.setBorder(BorderFactory.createEtchedBorder(
    //					EtchedBorder.LOWERED, null, null));
    //		}
    //		return jPicturePanel1;
    //	}
    //
    //	private JPicturePanel getJPicturePanel0() {
    //		if (jPicturePanel0 == null) {
    //			jPicturePanel0 = new JPicturePanel();
    //			jPicturePanel0.setBorder(BorderFactory.createEtchedBorder(
    //					EtchedBorder.LOWERED, null, null));
    //		}
    //		return jPicturePanel0;
    //	}
     
    	private JButton getJButton1() {
    		if (jButton1 == null) {
    			jButton1 = new JButton();
    			jButton1.setText("Remove From Cart");
    			jButton1.addMouseListener(new MouseAdapter() {
     
    				public void mouseReleased(MouseEvent event) {
    					jButton1MouseMouseReleased(event);
    				}
    			});
    		}
    		return jButton1;
    	}
     
    	private JButton getJButton0() {
    		if (jButton0 == null) {
    			jButton0 = new JButton();
    			jButton0.setText("Add To Cart");
    			jButton0.addMouseListener(new MouseAdapter() {
     
    				public void mouseReleased(MouseEvent event) {
    					jButton0MouseMouseReleased(event);
    				}
    			});
    		}
    		return jButton0;
    	}
     
    	private JScrollPane getJScrollPane2() {
    		if (jScrollPane2 == null) {
    			jScrollPane2 = new JScrollPane();
    			jScrollPane2.setViewportView(getJList2());
    		}
    		return jScrollPane2;
    	}
     
    	private JList getJList2() {
    		if (jList2 == null) {
    			jList2 = new JList();
    			jList2.setModel(cartList);
    			jList2.addListSelectionListener(new ListSelectionListener() {
     
    				public void valueChanged(ListSelectionEvent event) {
    					jList2ListSelectionValueChanged(event);
    				}
    			});
    		}
    		return jList2;
    	}
     
    	private JCheckBox getJCheckBox0() {
    		if (jCheckBox0 == null) {
    			jCheckBox0 = new JCheckBox();
    			jCheckBox0.setText("Check For Sale Setup");
    		}
    		return jCheckBox0;
    	}
     
    	private JLabel getJLabel4() {
    		if (jLabel4 == null) {
    			jLabel4 = new JLabel();
    			jLabel4.setText(total);
    		}
    		return jLabel4;
    	}
     
    	private JLabel getJLabel3() {
    		if (jLabel3 == null) {
    			jLabel3 = new JLabel();
    			jLabel3.setText(stock);
    		}
    		return jLabel3;
    	}
     
    	private JLabel getJLabel2() {
    		if (jLabel2 == null) {
    			jLabel2 = new JLabel();
    			jLabel2.setText(myBuy);
    		}
    		return jLabel2;
    	}
     
    	private JLabel getJLabel1() {
    		if (jLabel1 == null) {
    			jLabel1 = new JLabel();
    			jLabel1.setText(mySale);
    		}
    		return jLabel1;
    	}
     
    	private JLabel getJLabel0() {
    		if (jLabel0 == null) {
    			jLabel0 = new JLabel();
    			jLabel0.setText(store);
    		}
    		return jLabel0;
    	}
     
    	private JScrollPane getJScrollPane1() {
    		if (jScrollPane1 == null) {
    			jScrollPane1 = new JScrollPane();
    			jScrollPane1.setViewportView(getJList1());
    		}
    		return jScrollPane1;
    	}
     
    	private JList getJList1() {
    		if (jList1 == null) {
    			jList1 = new JList();
    			jList1.setModel(colorList);
    			jList1.addListSelectionListener(new ListSelectionListener() {
     
    				public void valueChanged(ListSelectionEvent event) {
    					jList1ListSelectionValueChanged(event);
    				}
    			});
    		}
    		return jList1;
    	}
     
    	private JSeparator getJSeparator0() {
    		if (jSeparator0 == null) {
    			jSeparator0 = new JSeparator();
    		}
    		return jSeparator0;
    	}
     
    	private JScrollPane getJScrollPane0() {
    		if (jScrollPane0 == null) {
    			jScrollPane0 = new JScrollPane();
    			jScrollPane0.setViewportView(getJList0());
    		}
    		return jScrollPane0;
    	}
     
    	private JList getJList0() {
    		if (jList0 == null) {
    			jList0 = new JList();
    			jList0.setModel(itemList);
    			jList0.addListSelectionListener(new ListSelectionListener() {
     
    				public void valueChanged(ListSelectionEvent event) {
    					jList0ListSelectionValueChanged(event);
    				}
    			});
    		}
    		return jList0;
    	}
     
    	private JTextField getJTextField0() {
    		if (jTextField0 == null) {
    			jTextField0 = new JTextField();
    			jTextField0.setText("jTextField0");
    			jTextField0.addInputMethodListener(new InputMethodListener() {
     
    				public void caretPositionChanged(InputMethodEvent event) {
    					jTextField0InputMethodCaretPositionChanged(event);
    				}
     
    				public void inputMethodTextChanged(InputMethodEvent event) {
    					jTextField0InputMethodInputMethodTextChanged(event);
    				}
    			});
    		}
    		return jTextField0;
    	}
     
    	public Applet() {
    		initComponents();
    	}
     
    	private void jTextField0InputMethodCaretPositionChanged(
    			InputMethodEvent event) {
    	}
     
    	private void jTextField0InputMethodInputMethodTextChanged(
    			InputMethodEvent event) {
    	}
     
    	private void jList0ListSelectionValueChanged(ListSelectionEvent event) {
    	}
     
    	private void jList1ListSelectionValueChanged(ListSelectionEvent event) {
    	}
     
    	private void jList2ListSelectionValueChanged(ListSelectionEvent event) {
    	}
     
    	private void jButton1MouseMouseReleased(MouseEvent event) {
    	}
     
    	private void jButton0MouseMouseReleased(MouseEvent event) {
    	}
     
    	private void jTextArea0MouseMouseReleased(MouseEvent event) {
    	}
     
    }

    the error
    java.lang.NullPointerException
    	at ernya.Applet.init(Applet.java:79)
    	at sun.applet.AppletPanel.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)

    btw , sry about the failed post / edited saying to delete it in the other thread ><;
    Last edited by wolfgar; April 1st, 2010 at 11:28 PM.
    Programming: the art that fights back


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: applet + swing + arraylist = exception??

    Applet's don't initialize anything in their constructor. The init method must act as a pseudo-constructor (hint: initialize the ArrayLists)

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    wolfgar (April 1st, 2010)

  4. #3
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: applet + swing + arraylist = exception??

    how do i initialize something when it needs to be empty for the stuff being loaded into them ?

    edit
    nvm >< blond moment XP
    Last edited by wolfgar; April 1st, 2010 at 11:45 PM.
    Programming: the art that fights back

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 03:44 AM
  2. applet fail
    By wolfgar in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 21st, 2010, 05:24 AM
  3. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM
  4. [SOLVED] applet vs. gui app
    By rptech in forum AWT / Java Swing
    Replies: 3
    Last Post: August 27th, 2009, 09:13 AM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM