Results 1 to 3 of 3
  1. #1
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default arrg! back again with a List[].add() problem.

    Problem is the line list[i].add(line);
    Stack Trace:
    java.lang.NullPointerException
    at beta.pro.Utility.MakeLS1ComboBox(Utility.java:69)
    at beta.pro.AlphaBest.<init>(AlphaBest.java:157)
    at beta.pro.AlphaBest$1.run(AlphaBest.java:31)
    at java.awt.event.InvocationEvent.dispatch(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.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    Java Code:
    	public static List[] MakeLS1ComboBox(String path){
    		List[] list = new List[15];
    		try {
    			InputStream inputStream = Utility.class.getResourceAsStream(path);
    			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    			try {
    				String line = reader.readLine();
    				int i = 0;
    				while (line != null) {
    					list[i].add(line);     // <---------- line 69
    					line = reader.readLine();
    					i++;
    				}
    			}
    			finally {
    				reader.close();
    			}
    		}
    		catch (IOException e){
    			e.printStackTrace();
    		}
    		return list;
    	}
    I know the file is there because the code below doesn't fail. The only thing different about these two methods is the return type and ReadFile adds "line" to a StringBuilder, not a List[]. What am I missing please??
    Java Code:
    String e = Utility.ReadFile("LS1LessonList.txt");
    		System.out.println(e);

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: arrg! back again with a List[].add() problem.

    List[] list = new List[15];
    This creates an array that can hold 15 Lists. But you never actually put any lists into it. It's like writing "Socks" on a drawer and being surprised that there are no socks in it :)
    Loop over the array and initilize it.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: arrg! back again with a List[].add() problem.

    Quote Originally Posted by PhHein View Post
    List[] list = new List[15];
    This creates an array that can hold 15 Lists. But you never actually put any lists into it. It's like writing "Socks" on a drawer and being surprised that there are no socks in it :)
    Loop over the array and initilize it.
    So... I didn't need a list at all, I just needed a String[]. Thank you much for your speedy reply. All good with this code.
    Java Code:
    	public static String[] MakeLS1ComboBox(String path){
    		String[] list = new String[15];
    		try {
    			InputStream inputStream = Utility.class.getResourceAsStream(path);
    			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    			try {
    				String line = reader.readLine();
    				int i = 0;
    				while (line != null) {
    					list[i] = line + ", ";
    					line = reader.readLine();
    					i++;
    				}
    			}
    			finally {
    				reader.close();
    			}
    		}
    		catch (IOException e){
    			e.printStackTrace();
    		}
    		return list;
    	}
    That bit gets called to populate this JComboBox.
    Java Code:
    String[] cmBox = Utility.MakeLS1ComboBox("LS1LessonList.txt");
    	@SuppressWarnings({ "rawtypes", "unchecked" })
    	JComboBox LS1comboBox = new JComboBox(cmBox);
    	getContentPane().add(LS1comboBox, BorderLayout.NORTH);
    Thankfully the JComboBox defaults to only show the first 8 lessons, and adds a scrollbar for the rest... it would be kinda unsightly to have it drop-down all 15.
    Last edited by NotTooOld; 04-23-2013 at 01:46 PM.

Similar Threads

  1. Java applet back buffering problem
    By gaj7 in forum Java Applets
    Replies: 7
    Last Post: 01-12-2012, 04:06 AM
  2. Scriptlet Not Running on JSP after coming back from back button of browser
    By jason.3dmagic in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 06-23-2011, 07:44 AM
  3. Problem with mozilla back button.
    By shivakumari in forum Java Servlet
    Replies: 3
    Last Post: 05-07-2009, 04:19 AM
  4. problem with back button of the browser.
    By shivakumari in forum Java Servlet
    Replies: 2
    Last Post: 03-25-2009, 07:41 AM
  5. Problem with Midlet back button
    By Poonam in forum CLDC and MIDP
    Replies: 5
    Last Post: 11-17-2008, 09:14 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
  •