0

I keep getting a null point exception in here and I don't know what's wrong with my code. I'm having Constructor that parses a String s like { x=-1 y=-2.0 z=3d } into the internal HashMap representation of the Vector(_hmVar2Value).

public Vector(String s) {
    String[] splitString = s.split(" ");
    for (int i=0;i<splitString.length-2;i++) {
        String[] str=splitString[i+1].split("=");
        double x = Double.parseDouble(str[1]);
        _hmVar2Value.put(str[0],x);
    }
}
acconrad
  • 3,201
  • 1
  • 22
  • 31
kim_98
  • 1

1 Answers1

0

I have run your code and its works fine. Have you initialize your hash map yet?

Try something like this

Yes you have declare it however didnt initialize it yet. Try something like this

public OtherClass(String s) {
            _hmVar2Value = new HashMap<String,Double>();
            String[] splitString = s.split(" ");
            for (int i=0;i<splitString.length-2;i++) {
                String[] str=splitString[i+1].split("=");
                double x = Double.parseDouble(str[1]);
                _hmVar2Value.put(str[0],x);

            }
    System.out.print(_hmVar2Value);



  }
duongthaiha
  • 855
  • 6
  • 17
  • private HashMap _hmVar2Value; although I have this and also a constructor for initially empty vector I keep getting an error – kim_98 Oct 26 '17 at 20:42
  • Yes you have declare it however didnt initialize it yet. I have updated my answer with some code. Hope this helps – duongthaiha Oct 26 '17 at 20:49
  • I added the initialization. The nullpointerror is gone now but I keep getting ArrayIndexOutOfBoundsException. – kim_98 Oct 26 '17 at 21:10
  • Well without the input then I cant help you. But i guess it from oin of line String[] str=splitString[i+1].split("="); double x = Double.parseDouble(str[1]); You have to debug to find out :) – duongthaiha Oct 26 '17 at 21:12