-1

I'm trying to make a very basic software simulation of a router that reads in a text file and acts on the commands and other information given to it. I made a new class called groupclass to hold an ArrayList

package router;

import java.util.ArrayList;

public class groupclass 
{
    public ArrayList<String> member;
}

Made a HashMap with it as the value

static Map<Integer, groupclass> groupmap = new HashMap<Integer, groupclass>();

And tried this code

private static void groupadd(int groupnum, String address)
{
    out.println("debug groupadd");

    try
    {
        groupmap.get(groupnum).member.add(address);
    }
    catch(NullPointerException e)
    {
        groupmap.put(groupnum, new groupclass());
        groupmap.get(groupnum).member.add(address);
    }
}

Which throws a NullPointerException at

groupmap.get(groupnum).member.add(address);

The idea was to make a map and associate a new groupclass object with each group number, and each groupclass would have a list of IPs stored as strings. I'm at a complete loss here, and any tweaks I do cause weirder problems and build errors I don't understand.

Thanks in advance!

xG33Kx
  • 25
  • 3
  • You never initialize `member` in `groupclass`; it's `null`. What is the confusion? – Brian Roach Feb 05 '14 at 00:53
  • possible duplicate of [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – Brian Roach Feb 05 '14 at 00:54
  • Just a minor overlook. I haven't used ArrayList before this code and didn't think about it. Thank you! – xG33Kx Feb 05 '14 at 00:58

1 Answers1

1

Your member variable "member" is not initialized. Add

public member=new ArrayList<>();

brummfondel
  • 1,202
  • 1
  • 8
  • 11
  • 1
    minus the word `public` of course. Or even `member = new ArrayList();` if you don't have Java 7. – Dawood ibn Kareem Feb 05 '14 at 01:00
  • This is technically correct, however it is best practice to parametrize the `<` and `>` area with the data type (in the OP's condition, you would want to use `String` in the middle). – Mike Koch Feb 05 '14 at 01:32