2

How do I turn a single column of an 2d array into an ArrayList?

I have this code:

        for (int i = 0; i < lordStats[0].length; i++)
        troopList.add (lordStats[i][2]);
//trooplist is a list and lordstats is a 2d array, they are both Strings

but it gives me a lot of errors, how do I do this.

These are my errors, all I can figure out is that something in my array is messed up. Note that without these 2 lines of code, the program runs fine.

Line 32-33

    for (int i = 0; i < lordStats[0].length; i++)
    troopList.add (lordStats[i][2]);

errors:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at arsenalLord.arsenalLordGui(arsenalLord.java:32)
    at arsenalLord.loader(arsenalLord.java:18)
    at siegeText$1.actionPerformed(siegeText.java:41)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6373)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6138)
    at java.awt.Container.processEvent(Container.java:2085)
    at java.awt.Component.dispatchEventImpl(Component.java:4735)
    at java.awt.Container.dispatchEventImpl(Container.java:2143)
    at java.awt.Component.dispatchEvent(Component.java:4565)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4621)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
    at java.awt.Container.dispatchEventImpl(Container.java:2129)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4565)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:679)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:638)
    at java.awt.EventQueue$1.run(EventQueue.java:636)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:652)
    at java.awt.EventQueue$2.run(EventQueue.java:650)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:649)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
2
  • Well from the error above, it seems like the code has errors on AWT component. Could you tell or post exactly what is in line 32? Commented Feb 5, 2012 at 7:48
  • 1
    The array must be initialized, of course. Arrays allow values, but some lists do not. Commented Feb 5, 2012 at 7:52

1 Answer 1

2

No need to do a loop, you can directly call Arrays.asList(array[]) method. eg: this should work:

 troopList = Arrays.asList(lordStats[2]);

where troopList is List<String>

Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't this entirely miss the fact that it was the second column that the OP wanted in the list? This gives you the second row, which is obviously much easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.