-3
public class SecureSystem {
    final int low = 0;
    final int high = 1;
    HashMap<String, int[]> subject;
    HashMap<String, int[]> object;

    public SecureSystem() {
    subject = new HashMap<String, int[]>();
    object = new HashMap<String, int[]>();
    }
    ReferenceMonitor rm = new ReferenceMonitor();
    rm.ReferenceMonitor(subject,object);

    System.out.println(this.subject.get("a")); // how to make it print [1,2]?
    System.out.println(this.object.get("b")); // how to make it print [3,4]?
}
class ReferenceMonitor{
    ReferenceMonior(HashMap<String, int[]> subject, HashMap<String, int[]> object) {
        SecureSystem ss = new SecureSystem();
        ss.subject.put("a", new int{1,2});
        ss.object.put("a", new int{3,4})
    }
}

how do I make it do that? If I pass HashMaps to ReferenceMonitor class, and try to read contents, I get NullPointerError.

Thank you so much.

Nayana
  • 1,321
  • 3
  • 18
  • 32

3 Answers3

0

You have not initialized the HashMaps. They are null because you have never created them.

HashMap<String, int[]> map = new HashMap<String, int[]>();

leigero
  • 3,127
  • 10
  • 39
  • 60
  • I actually do... it's just a simplified version of my code. The point of this post was to ask how to pass by a reference. – Nayana Sep 13 '13 at 21:51
0

The problem is here:

class ReferenceMonitor{
    ReferenceMonior(HashMap<String, int[]> subject, HashMap<String, int[]> object) {
        //these three lines are the culprit
        SecureSystem ss = new SecureSystem();
        ss.subject.put("a", new int{1,2});
        ss.object.put("a", new int{3,4})
    }
}

You're putting the data in the subject and object maps inside the new SecureSystem ss local variable which will be unusable after the completion of the constructor call. You should put the data in the subject and object parameters so they're contents will be modified as well:

class ReferenceMonitor{
    ReferenceMonior(HashMap<String, int[]> subject, HashMap<String, int[]> object) {
        //code fixed
        //also, there's no need to create a new SecureSystem instance
        subject.put("a", new int[] {1,2});
        object.put("b", new int[] {3,4});
    }
}

This code can even be enhanced to pass the SecureSystem object reference instead:

class ReferenceMonitor{
    ReferenceMonior(SecureSystem secureSystem) {
        secureSystem.getSubject().put("a", new int[] {1,2});
        secureSystem.getObject().put("b", new int[] {3,4});
    }
}

Also, please note that Java is NOT pass by reference.

Community
  • 1
  • 1
Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306
-1

Did you in any place use a new() to initialize your Hashmaps? In case not. There is your answer.

What you intended was something like

class ReferenceMonitor{
    ReferenceMonior(HashMap<String, int[]> subject, HashMap<String, int[]> object) {
        SecureSystem ss = new SecureSystem();
        ss.subject=subject;
        ss.object=object;
        ss.subject.put("a", new int{1,2});
        ss.object.put("a", new int{3,4})
    }
}

Besides, that is awful code.

1) You should consider using patterns like builder pattern

2) Your use of public data-members is a violation of OOP's information hiding principle and is considered bad style. Please use setter methods.

Thomas Junk
  • 5,296
  • 2
  • 25
  • 39