no null pointer exception

Hi,

i m using the list<> type from the collection language. As described i do not get a null pointer exception when executing the statements ..

list<position> mylist;
mylist.add(new position());

although the list mylist was not initializied. How should i change this behavior? Somehow, i have to get an error message when working with uninitialized lists ....

Any suggestions?
Dan
6 comments
Comment actions Permalink
Hi,

I just tried the same here and i get an error-message saying: "Variable used before initialized"

So of course you get a compiler error and not a runtime-exception as you say.
0
Comment actions Permalink
Oh yes,

that is correct - just tried it....

But if you declare the list as class field (not as local variable), then you wont get an error message from mps ....

Is there a way to issue a warning for uninitialized class field collections?


Best,
Dan
0
Comment actions Permalink
This would only make sense in the constructor.

I just tested this in eclipse and there you don't get a warning neither.

So maybe it's not possible to say for sure, if a field is initialized...
0
Comment actions Permalink
I wrote a pre-processing script to check for uninitialized lists.
     
(model, genContext, operationContext)->void {
  list<node<VariableDeclaration>> variableDeclarations =
    model.nodes(VariableDeclaration);

  for (node<VariableDeclaration> v : variableDeclarations) {
    if (v.type.isInstanceOf(ClassifierType)) {
      string className = v.type : ClassifierType.classifier.name;
      
      if (className.equals("List") || className.equals("Set") ||
         className.equals("Collection")) {
        if (v.initializer.isNull) {
          throw new IllegalArgumentException("Collection " + v.name + " in " +
              v.ancestor<concept = ClassConcept>.name + " has to be initialized
              ... ");
            
        }
      }
    }
  }
}


However, do i really have to do string compares to get java collection classes checked?

Dan
0
Comment actions Permalink
Hi,

I don't know where to add a pre-processing script.

For testing I just created a language that extends the baselang and added an Non-Type-System-Rule. This worked pretty well.

Although you should consider only bringing a warning. Especially cause a field can be initialized in the constructor. There is no need for an immediate initializer.

About the string-comparison:
I don't know how to check if a ClassConcept implements a interface (recursive).

But you can take a look at "subtyping_classifier"-Rule (Press Ctrl+N). There is something with implementedInterfaces.

Good luck.
0
Comment actions Permalink
Yes,

it would be cool if i could write something like

if (v.type : ClassifierType.classifier instanceof List) { ...

but that does not work, since classifier is of time node<Classifier> and List is of type java.util.List. Does someone know, how i can compare this in the last generation step? If i just print v.type : ClassifierType.classifier it returns "List" ...

Dan
0

Please sign in to leave a comment.