0

Im new to Java and I completely stuck on it. I have to implement "summator" class, to summ all Numbers in it. For example this class holds a number

public class NumericNode <N extends Number>{    
    private N nodeValue = null;
    public NumericNode(N initValue){
        this.nodeValue = initValue;
    }
    public N getNodeValue() {
        return nodeValue;
    }
}

second class need to do some summ

public class NumericSummatorNode<NumericNode, T> {    
    private T nodeValue;
    private NumericNode[] inlets;
public NumericSummatorNode(NumericNode...inlets) {
        this.summ(inlets);
    }//constructor
public void summ(NumericNode... summValues) {
        ArrayList<NumericNode> numericList = new ArrayList<>();       
        int count = summValues.length;
        for (int i = 0; i < count; i++){
           numericList.add(summValues[i]);
        }
       for (int j = 0; j < count; j++){
           Method method = numericList.get(j).getClass().getMethod("getNodeValue", null);
           method.invoke(numericList.get(j), null);
        }    
     }

And here is the main:

public static void main(String[] args){
        NumericNode n1 = new NumericNode(5);
        NumericNode n2 = new NumericNode(4.3f);
        NumericNode n3 = new NumericNode(24.75d);
        NumericNode n5 = new NumericNode((byte)37);
        NumericNode n6 = new NumericNode((long)4674);
        NumericSummatorNode s1 = new NumericSummatorNode(5, 4.6f, (double)4567);
        NumericSummatorNode s2 = new NumericSummatorNode(n1, n2);
        NumericSummatorNode s3 = new NumericSummatorNode();        
        s2.summ(n1, n2);        
    }//main

So I have an issue to call getNodeValue() method from an NumericNode object in my array list. How do I make this work?

1 Answers1

1

You need to look at what the exception says, it tells you what is wrong. The exception probably says:

java.lang.NoSuchMethodException: java.lang.Integer.getNodeValue()

So the list apparently contains Integers, even though it appears that ArrayList<NumericNode> should contain only NumericNode. How can this happen? If I run it in eclipse, it also shows this warning:

The type parameter NumericNode is hiding the type NumericNode

This is caused because the class is declared as

public class NumericSummatorNode<NumericNode, T>

NumericNode is a type argument that unfortunately shares the same name as the NumericNode class. This means it hides the real NumericNode class and you can't use it anymore. This is also the reason why new NumericSummatorNode(5, 4.6f, (double) 4567) even compiles. You can't just pass any Number to a constructor that actually takes NumericNode.

So restructure it to NumericSummatorNode<T> or maybe NumericSummatorNode<N extends NumericNode, T> or whatever your intention was there, so it won't hide any classes. It won't compile any more, so you will also need to adapt the types of your constructor and sum method. Also it's nice to use generics, but are they kind of useless if you are using raw types anyway.

kapex
  • 28,903
  • 6
  • 107
  • 121
  • Thank your for this explicit explanation. Exactly you was right. I remake my code in "summator" class, it is now work with ArrayList and invoking methods from list. The problem redused to simple reflection example))) – Алексей Алекс Sep 15 '14 at 15:55