-1

I am developing some code in Java but facing a problem with a NPE. It happens when I try to use a method in another class. Here is my code :

public class MyClass{

    private Attributes attr = null;

public static void main(String [ ] args) throws ParseException, SAXException, IOException, ParserConfigurationException {
         MyClass main = new MyClass();
         Options options = main.parseCommandLine();

        CommandLineParser parser = new BasicParser();
        CommandLine cl = parser.parse(options, args);
        main.configureAttributes(main);
    }

private void configureAttributes(MyClass main, CommandLine cl) throws ParserConfigurationException, SAXException, IOException {         
        String[] attributes = cl.getOptionValues("a");
        Integer tag = null;

        for(int i = 0; i < attributes.length ; i++) {
            String[] parts = attributes[i].split("=");
            String tagName = parts[0];
            String value = parts[1]; 
            tag = Integer.parseInt(tagName,16);                             
            this.attr.setString(tag, DICT.vrOf(tag), value);
        }
    }           
}   

But I am getting an NPE on the last line : this.attr.setString(tag, DICT.vrOf(tag), value);

I have all the right imports. It is a Maven project so I also added the dependency to my pom.xml.

Thanks a lot for your help !

V.

Vaclav
  • 57
  • 1
  • 5

2 Answers2

1

Looks like you forgot to initialize your attr field. According to your source here is only one assignment to this field: private Attributes attr = null;. So, I think, you need to initialize your field somewhere in your code.

Vasyl Moskalov
  • 4,242
  • 3
  • 20
  • 28
0

And where are you initializing Attributes attr?

You need to something like Attributes attr = new Attributes(); because right know your variable does contain nothing - null

Suule
  • 2,197
  • 4
  • 16
  • 42