Results 1 to 8 of 8
  1. #1
    sebo is offline Member
    Join Date
    Oct 2008
    Posts
    9
    Rep Power
    0

    Default adding values from file to multi-dimensional ArrayList

    the question is simple: what is wrong in here ?

    FILE:
    Java Code:
    one two three
    four five six
    seven eight nine
    when i click the button i want this values to be written into ArrayList[3][3]:
    Java Code:
    readTransactionButton.addActionListener(new ActionListener() {
            	public void actionPerformed(ActionEvent e){
            		String line = "";
            		ArrayList<String> data[][] = new ArrayList[3][3];
            		try {
            		   FileReader fr = new FileReader("c:/file.txt");
            		   BufferedReader br = new BufferedReader(fr);
            		   int i=0;
            		   while((line = br.readLine()) != null) {
            			   String[] theline = line.split(" ");
            			   	   [B]data[i][0].add(theline[0]);
            				   data[i][1].add(theline[1]);
            				   data[i][2].add(theline[2]);[/B]
            				   i++;
            			   }
            		   }
            		   catch(FileNotFoundException fN) {
            		    fN.printStackTrace();
            		   }
            		   catch(IOException exc) {
            		    System.out.println(exc);
            		   }
            	}
            });
    the values of 'theline' are being read correctly, but what i get is:
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at frames.AccountsFrame$4.actionPerformed(AccountsFrame.java:174)
    	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.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)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default

    Java Code:
    ArrayList<String> data[][] = new ArrayList[3][3];
    That's not how you declare an ArrayList. Here you want an ArrayList of an ArrayList of String. Something like
    Java Code:
    ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
    data.add(new ArrayList<String>());
    data.get(0).add("String");
    etc.

    Remember that in Java, just as a two dimensional array is an array of arrays, a two dimensional ArrayList is an ArrayList of ArrayLists.

    If you want, you can have an ArrayList of arrays
    Java Code:
    ArrayList<String[]> data1 = new ArrayList<String[]>();
    data1.add(new String[3]);
    data1.get(0)[0] = "String";
    or an array of ArrayList
    Java Code:
    ArrayList[] data2 = new ArrayList[3];
    data2[0] = new ArrayList<String>();
    data2[0].add("String");
    Note that in the last example you cannot create a generic array, so it's simply new ArrayList[3] and not ArrayList<String>[3].

    db

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default

    In fact, this
    Java Code:
    ArrayList<String> data[][] = new ArrayList[3][3];
    better written as
    Java Code:
    ArrayList<String>[][] data = new ArrayList[3][3];
    is a two dimensional array of ArrayList. The generic declaration of <String> will only generate a compiler (or is it runtime?) warning as generic arrays are not supported in Java.

    db

  4. #4
    sebo is offline Member
    Join Date
    Oct 2008
    Posts
    9
    Rep Power
    0

    Default

    k, i tried to use the first possiblity you mentioned in ur post... but there's still sth what i don't know how to do...
    Java Code:
    private ArrayList<String[]> data;
    
    readTransactionButton.addActionListener(new ActionListener() {
            	public void actionPerformed(ActionEvent e){
            		String line = "";
            		[B]data = new ArrayList<String[]>();[/B]
            		try {
            		   FileReader fr = new FileReader("c:/file.txt");
            		   BufferedReader br = new BufferedReader(fr);
            		   int i=0;
            		   while((line = br.readLine()) != null) {
            			   String[] theline = line.split(" ");
            			   [B]data.add(theline);[/B]
            			   System.out.println([B]data.get(0)[i][/B]+"\n");
            			   i++;
            			   }       			   
            		   }
            		   catch(FileNotFoundException fN) {
            		    fN.printStackTrace();
            		   }
            		   catch(IOException exc) {
            		    System.out.println(exc);
            		   }
            	}
            });
    this is the output i get:
    Java Code:
    one
    two
    three
    so only the first line - it's obvious because now i can't add a single String from the line (theline[0] etc.)... i just add 3 columns from the first line and can't go further... or am i wrong??
    i don't wanna be too stupid for this :p still need some help

    is there no possibility to get just like:
    data[0][0] = one, data[0][1]=two, data[0][2]=three
    data[1][0] = four, etc.?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default

    > i tried to use the first possiblity you mentioned in ur post
    That wasn't the first, that was the second.

    Use the first and you'll be fine. I'm repeating it so you don't mix up again.
    Java Code:
    ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
    data.add(new ArrayList<String>());
    data.get(0).add("String");
    Just remember what I said: it's an ArrayList of ArrayList.

    ArrayList<ArrayList<String>> contains elements of type ArrayList<String>

    Each element must be initialised

    These elements contain elements of type String

    To get back the String "String" in the 3-line example, you would use
    Java Code:
    String getValue = data.get(0).get(0);
    Take some time Think. Take a paper and sketch it out.

    You'll get it.

    db

  6. #6
    sebo is offline Member
    Join Date
    Oct 2008
    Posts
    9
    Rep Power
    0

    Default

    this is what i'm talking about... idd that was the second one!
    i'm already lil' bit tired - sitting on this and lot more for the whole day... but now it works for me!

    thx for your help!

  7. #7
    lakepacific is offline Member
    Join Date
    Jun 2009
    Posts
    3
    Rep Power
    0

    Default Re: adding values from file to multi-dimensional ArrayList

    Darryl,

    I stumbled across your post and this is exactly what I was looking for. However, I now have my two dimensional ArrayList (or ArrayList of ArrayList) and would like to sort it on [i,2] (type string) and then on [i,5] which is a string that I would need to convert to a date for it to properly sort. Any suggestions on how to sort your ArrayList of ArrayList? Thanks.

    Joe

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: adding values from file to multi-dimensional ArrayList

    Lakepacific: please ask your question in a new thread in the "new to java" forum rather than re-awaken this old thread. You can link to this thread easily by adding [thread=12694]This thread[/thread] anywhere in your post, like so: This thread

Similar Threads

  1. trying to set() values of in list of arraylist
    By alvations in forum New To Java
    Replies: 15
    Last Post: 10-13-2008, 09:35 PM
  2. Multi dimensional Array
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-09-2008, 03:34 PM
  3. Replies: 2
    Last Post: 01-07-2008, 07:45 PM
  4. Adding numbers in a 2 dimensional array
    By j0shizabeast in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 04:31 AM
  5. Help with array multi-dimensional
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:00 PM

Posting Permissions

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