0

in my Network class on line 10 i am creating a TreeMap, and then giving it the values of Network device and Channel so it would add the Networkdevice to a new Channel..

on line 21 in the Network class i am creating a method called AddDevice which takes in a NetworkDevice as a parameter so this would be an AccessPoint object because AccessPoint extends NetworkDevice and this method takes the NetworkDevice thats been passed in and then adds it to the TreeMap by saying join.put...

Within Network i am also creating a private field called Channel

in the NetworkDevice class i am creating a new object on line 9

public void join(Network network){
network.addDevice(this);
}

which takes a network as a parameter and calls its .addDevice method to add the device its called upon so example i am trying to test in my main method is:

accessPoint.join(network);

however this is giving me a null pointer exception when i try to test it

The Three main places I am having this exception is occuring from the following chunks of code:

In Network class i have

public class Network {
private TreeMap<NetworkDevice, Channel> join;
private Map<AccessPoint, Channel> records; 
private static List<Channel> channels = new ArrayList<Channel>();
private Channel channel;

public Network(Channel channel){
        this.channel = channel;
    }

    public void addDevice(NetworkDevice device){
        channel = new Channel(0);
        join.put(device, channel);
        channels.add(channel);
    }
}

In my NetworkDevice class i have:

public class NetworkDevice {
protected String address;
protected Channel channel;

public void join(Network network){
        network.addDevice(this);
    }

}

My main class is for testing purposes only however, it does return a null pointer exception

public class Main {
public static void main(String[] args) {
Channel channel = new Channel(1);
Network network = new Network(channel);
AccessPoint accessPoint = new AccessPoint("168.0.0.1");
//Client client = new Client("192.1.1.1");
accessPoint.join(network);  
}
}

my output and problem is:

Exception in thread "main" java.lang.NullPointerException
    at Network.addDevice(Network.java:23)
    at NetworkDevice.join(NetworkDevice.java:9)
    at Main.main(Main.java:10)
Kaan Kolcu
  • 11
  • 1

0 Answers0